-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlserver.ps1
More file actions
71 lines (65 loc) · 2.84 KB
/
Copy pathsqlserver.ps1
File metadata and controls
71 lines (65 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#create a VM using previous functions
New-PowerLabVM -Name 'Sqlserver'
#need an xml file--> change computername, ip
Copy-Item -Path 'C:\Program Files\WindoesPowerShell\Modules\PowerLab\LABDC.xml' -Destination 'C:\Program Files\WindowsPowerShell\Modules\PowerLab\Sqlserver.xml'
#create an os
Install-PowerLabOperationSystem -VmName 'Sqlserver'
Start-VM -Name Sqlserver
#check if Vm is online
$VmCred = Import-Clixml -Path 'C:\PowerLab\VMCredential.xml'
while (-not (Invoke-Command -VMName 'Sqlserver' -ScriptBlock { 1 } -Credential $VmCred -ErrorAction Ignore)) {
Start-Sleep -Seconds 10
Write-Host 'Waiting for Sqlserver to come up...'
}
#add to domain
$domainCred = Import-Clixml -Path 'C:\PowerLab\DomainCredential.xml'
$addParams = @{
DomainName = 'server1.com'
Credential = $domainCred
Restart = $true
Force = $true
}
Invoke-Command -VMName 'Sqlserver' -ScriptBlock {
Add-Computer @using:Addparams
} -Credential $domainCred
#wait for it to go down and come back up
while (Invoke-Command -VmName Sqlserver -Scriptblock {1} -Credential $domainCred -ErrorAction Ignore) {
Start-Sleep -Seconds 10 Write-Host 'Waiting for Sqlserver to go down..'
}
while (-Not (Invoke-command -VMName Sqlserver -ScriptBlock {1} -Credential $domainCred -ErrorAction Ignore)) {
Start-Sleep -Seconds 10 Write-Host 'Waiting for it come back up'
}
#installation
#copy files to server
$session = New-PSSession -VMName 'Sqlserver' -Credential $domainCred
$sqlServerAnswerfilePath = "C:\Program Files\WindowsPowerShell\Modules\PowerLab\SqlServer.ini"
$tempfile = Copy-Item -Path $sqlServerAnswerfilePath -Destination "C:\Program Files\.ini" -PassThru
#change config in tempfile
$configContents = Get-Content -Path $tempFile.FullName -Raw
$configContents = $configContents.Replace('SQLSVCACCOUNT=""', 'SQLSVCACCOUNT="PowerLabUser"')
$configContents = $configContents.Replace('SQLSVCPASSWORD=""', 'SQLSVCPASSWORD="P@$$w0rd12"')
$configContents = $configContents.Replace('SQLSYSADMINACCOUNTS=""', 'SQLSYSADMINACCOUNTS=
"PowerLabUser"')
Set-Content -Path $tempFile.FullName -Value $configContents
#copy tempfile and iso file to server
$copyParams = @{
Path = $tempFile.FullName
Destination = 'C:\'
ToSession = $session
}
Copy-Item @copyParams
Remove-Item -Path $tempFile.FullName -ErrorAction Ignore
Copy-Item -Path 'C:\PowerLab\ISOs\en_sql_server_2016_standard_x64_dvd_8701871.iso'
-Destination 'C:\' -Force -ToSession $session
#run the installer
$icmParams = @{
Session = $session
ArgumentList = $tempFile.Name
ScriptBlock = {
$image = Mount-DiskImage -ImagePath 'C:\en_sql_server_2016_standard_x64_dvd_8701871.iso' -PassThru
$installerPath = "$(($image | Get-Volume).DriveLetter):"
& "$installerPath\setup.exe" "/CONFIGURATIONFILE=C:\$($using:tempFile.Name)"
$image | Dismount-DiskImage
}
}
Invoke-Command @icmParams