forked from microsoft/nav-arm-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetupVm.ps1
381 lines (319 loc) · 16.3 KB
/
SetupVm.ps1
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
$ErrorActionPreference = "Stop"
$WarningActionPreference = "Continue"
$ComputerInfo = Get-ComputerInfo
$WindowsInstallationType = $ComputerInfo.WindowsInstallationType
$WindowsProductName = $ComputerInfo.WindowsProductName
try {
function AddToStatus([string]$line, [string]$color = "Gray") {
("<font color=""$color"">" + [DateTime]::Now.ToString([System.Globalization.DateTimeFormatInfo]::CurrentInfo.ShortTimePattern.replace(":mm",":mm:ss")) + " $line</font>") | Add-Content -Path "c:\demo\status.txt" -Force -ErrorAction SilentlyContinue
}
AddToStatus "SetupVm, User: $env:USERNAME"
function DockerDo {
Param(
[Parameter(Mandatory=$true)]
[string]$imageName,
[ValidateSet('run','start','pull','restart','stop')]
[string]$command = "run",
[switch]$accept_eula,
[switch]$accept_outdated,
[switch]$detach,
[switch]$silent,
[string[]]$parameters = @()
)
if ($accept_eula) {
$parameters += "--env accept_eula=Y"
}
if ($accept_outdated) {
$parameters += "--env accept_outdated=Y"
}
if ($detach) {
$parameters += "--detach"
}
$result = $true
$arguments = ("$command "+[string]::Join(" ", $parameters)+" $imageName")
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "docker.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.CreateNoWindow = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $arguments
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$outtask = $null
$errtask = $p.StandardError.ReadToEndAsync()
$out = ""
$err = ""
do {
if ($null -eq $outtask) {
$outtask = $p.StandardOutput.ReadLineAsync()
}
$outtask.Wait(100) | Out-Null
if ($outtask.IsCompleted) {
$outStr = $outtask.Result
if ($null -eq $outStr) {
break
}
if (!$silent) {
AddToStatus $outStr
}
$out += $outStr
$outtask = $null
if ($outStr.StartsWith("Please login")) {
$registry = $imageName.Split("/")[0]
if ($registry -eq "bcinsider.azurecr.io") {
AddToStatus -color red "You need to login to $registry prior to pulling images. Get credentials through the ReadyToGo program on Microsoft Collaborate."
} else {
AddToStatus -color red "You need to login to $registry prior to pulling images."
}
break
}
} elseif ($outtask.IsCanceled) {
break
} elseif ($outtask.IsFaulted) {
break
}
} while(!($p.HasExited))
$err = $errtask.Result
$p.WaitForExit();
if ($p.ExitCode -ne 0) {
$result = $false
if (!$silent) {
$err = $err.Trim()
if ("$err" -ne "") {
AddToStatus -color red $err
}
AddToStatus -color red "ExitCode: $($p.ExitCode)"
AddToStatus -color red "Commandline: docker $arguments"
}
}
return $result
}
if (Test-Path -Path "C:\demo\*\BcContainerHelper.psm1") {
$module = Get-Item -Path "C:\demo\*\BcContainerHelper.psm1"
Import-module $module.FullName -DisableNameChecking
} else {
Import-Module -name bccontainerhelper -DisableNameChecking
}
. (Join-Path $PSScriptRoot "settings.ps1")
if ($AddTraefik -eq "Yes") {
if (Test-Path "c:\myfolder\certificate.pfx") {
AddToStatus -color Red "Certificate specified, cannot add Traefik"
$AddTraefik = "No"
}
if (-not $ContactEMailForLetsEncrypt) {
AddToStatus -color Red "Contact EMail for LetsEncrypt not specified, cannot add Traefik"
$AddTraefik = "No"
}
if ($clickonce -eq "Yes") {
AddToStatus -color Red "ClickOnce specified, cannot add Traefik"
$AddTraefik = "No"
}
if ($AddTraefik -eq "Yes") {
AddToStatus "Creating custom Traefik image"
$traefikImage = Create-CustomTraefikImage
AddToStatus "Traefik Image $traefikImage created, Setup Traefik container"
Setup-TraefikContainerForNavContainers -overrideDefaultBinding -PublicDnsName $publicDnsName -ContactEMailForLetsEncrypt $ContactEMailForLetsEncrypt
}
else {
Get-VariableDeclaration -name "AddTraefik" | Add-Content $settingsScript
}
}
if ("$ContactEMailForLetsEncrypt" -ne "" -and $AddTraefik -ne "Yes") {
if (-not (Get-InstalledModule ACME-PS -ErrorAction SilentlyContinue)) {
AddToStatus "Installing ACME-PS PowerShell Module"
Install-Module -Name ACME-PS -RequiredVersion "1.5.2" -AllowPrerelease -Force
AddToStatus "Using Lets Encrypt certificate"
# Use Lets encrypt
# If rate limits are hit, log an error and revert to Self Signed
try {
$plainPfxPassword = [GUID]::NewGuid().ToString()
$certificatePfxFilename = "c:\ProgramData\bccontainerhelper\certificate.pfx"
New-LetsEncryptCertificate -ContactEMailForLetsEncrypt $ContactEMailForLetsEncrypt -publicDnsName $publicDnsName -CertificatePfxFilename $certificatePfxFilename -CertificatePfxPassword (ConvertTo-SecureString -String $plainPfxPassword -AsPlainText -Force)
# Override SetupCertificate.ps1 in container
('if ([int](get-item "C:\Program Files\Microsoft Dynamics NAV\*").Name -le 100) {
Write-Host "WARNING: This version doesn''t support LetsEncrypt certificates, reverting to self-signed"
. "C:\run\SetupCertificate.ps1"
}
else {
. (Join-Path $PSScriptRoot "InstallCertificate.ps1")
}
') | Set-Content "c:\myfolder\SetupCertificate.ps1"
('$CertificatePfxPassword = ConvertTo-SecureString -String "'+$plainPfxPassword+'" -AsPlainText -Force
$certificatePfxFile = "'+$certificatePfxFilename+'"
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certificatePfxFile, $certificatePfxPassword)
$certificateThumbprint = $cert.Thumbprint
Write-Host "Certificate File Thumbprint $certificateThumbprint"
if (!(Get-Item Cert:\LocalMachine\my\$certificateThumbprint -ErrorAction SilentlyContinue)) {
Write-Host "Import Certificate to LocalMachine\my"
Import-PfxCertificate -FilePath $certificatePfxFile -CertStoreLocation cert:\localMachine\my -Password $certificatePfxPassword | Out-Null
}
$dnsidentity = $cert.GetNameInfo("SimpleName",$false)
if ($dnsidentity.StartsWith("*")) {
$dnsidentity = $dnsidentity.Substring($dnsidentity.IndexOf(".")+1)
}
') | Set-Content "c:\myfolder\InstallCertificate.ps1"
# Create RenewCertificate script
('$CertificatePfxPassword = ConvertTo-SecureString -String "'+$plainPfxPassword+'" -AsPlainText -Force
$certificatePfxFile = "'+$certificatePfxFilename+'"
$publicDnsName = "'+$publicDnsName+'"
Renew-LetsEncryptCertificate -publicDnsName $publicDnsName -certificatePfxFilename $certificatePfxFile -certificatePfxPassword $certificatePfxPassword
Start-Sleep -seconds 30
Restart-NavContainer -containerName "'+$containerName+'" -renewBindings
') | Set-Content "c:\demo\RenewCertificate.ps1"
} catch {
AddToStatus -color Red $_.Exception.Message
AddToStatus -color Red "Reverting to Self Signed Certificate"
}
}
}
if ("$WinRmAccess" -ne "") {
if (Test-Path "c:\myfolder\InstallCertificate.ps1") {
# Using trusted certificate - install on host
. "c:\myfolder\InstallCertificate.ps1"
}
elseif (Test-Path "c:\myfolder\SetupCertificate.ps1") {
# Using trusted certificate - install on host
. "c:\myfolder\SetupCertificate.ps1"
}
else {
$certificateThumbprint = (New-SelfSignedCertificate -DnsName $publicDnsName -CertStoreLocation Cert:\LocalMachine\My).Thumbprint
}
AddToStatus "Enabling PS Remoting"
Enable-PSRemoting -Force
AddToStatus "Creating Firewall rule for WinRM"
New-NetFirewallRule -Name "WinRM HTTPS" -DisplayName "WinRM HTTPS" -Enabled True -Profile "Any" -Action "Allow" -Direction "Inbound" -LocalPort 5986 -Protocol "TCP"
AddToStatus "Creating WinRM listener"
$cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=""$publicDnsName""; CertificateThumbprint=""$certificateThumbprint""}"
cmd.exe /C $cmd
}
if ($sqlServerType -eq "SQLDeveloper") {
AddToStatus "Installing SQL Server Developer edition"
$securePassword = ConvertTo-SecureString -String $adminPassword -Key $passwordKey
$dbCredential = New-Object System.Management.Automation.PSCredential('sa', $securePassword)
cd c:\demo
$exeUrl = "https://go.microsoft.com/fwlink/?linkid=840945"
$boxUrl = "https://go.microsoft.com/fwlink/?linkid=840944"
$sqlExe = "c:\demo\SQL.exe"
$sqlBox = "c:\demo\SQL.box"
Download-File -sourceUrl $exeUrl -destinationFile $sqlExe
Download-File -sourceUrl $boxUrl -destinationFile $sqlBox
Start-Process -Wait -FilePath $sqlExe -ArgumentList /qs, /x:setup
.\setup\setup.exe /q /ACTION=Install /INSTANCENAME=MSSQLSERVER /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT='NT AUTHORITY\NETWORK SERVICE' /SQLSYSADMINACCOUNTS='BUILTIN\ADMINISTRATORS' /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS
Remove-Item -Recurse -Force $sqlExe, $sqlBox, setup
stop-service MSSQLSERVER
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.MSSQLSERVER\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpdynamicports -value ''
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.MSSQLSERVER\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql14.MSSQLSERVER\mssqlserver\' -name LoginMode -value 2
start-service MSSQLSERVER
$sqlcmd = "ALTER LOGIN sa with password='" + ([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($dbcredential.Password)).Replace('"','""').Replace('''','''''')) + "',CHECK_POLICY = OFF;ALTER LOGIN sa ENABLE;"
Invoke-SqlCmd -ServerInstance "localhost" -QueryTimeout 0 -ErrorAction Stop -Query $sqlcmd
New-NetFirewallRule -DisplayName "SQLDeveloper" -Direction Inbound -LocalPort 1433 -Protocol tcp -Action Allow
}
AddToStatus "Starting docker"
start-service docker
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Ssl3 -bor [System.Net.SecurityProtocolType]::Tls -bor [System.Net.SecurityProtocolType]::Ssl3 -bor [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12
AddToStatus "Enabling File Download in IE"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3" -Name "1803" -Value 0
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3" -Name "1803" -Value 0
AddToStatus "Enabling Font Download in IE"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3" -Name "1604" -Value 0
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3" -Name "1604" -Value 0
AddToStatus "Show hidden files and file types"
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' -Name "Hidden" -value 1
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' -Name "HideFileExt" -value 0
if ($WindowsInstallationType -eq "Server") {
AddToStatus "Disabling Server Manager Open At Logon"
New-ItemProperty -Path "HKCU:\Software\Microsoft\ServerManager" -Name "DoNotOpenServerManagerAtLogon" -PropertyType "DWORD" -Value "0x1" –Force | Out-Null
}
AddToStatus "Add Import bccontainerhelper to PowerShell profile"
$winPsFolder = Join-Path ([Environment]::GetFolderPath("MyDocuments")) "WindowsPowerShell"
New-Item $winPsFolder -ItemType Directory -Force -ErrorAction Ignore | Out-Null
'if (Test-Path -Path "C:\demo\*\BcContainerHelper.psm1") {
$module = Get-Item -Path "C:\demo\*\BcContainerHelper.psm1"
Import-module $module.FullName -DisableNameChecking
} else {
Import-Module -name bccontainerhelper -DisableNameChecking
}' | Set-Content (Join-Path $winPsFolder "Profile.ps1")
AddToStatus "Adding Landing Page to Startup Group"
if ($AddTraefik -eq "Yes") {
$landingPageUrl = "http://${publicDnsName}:8180"
}
else {
$landingPageUrl = "http://${publicDnsName}"
}
New-DesktopShortcut -Name "Landing Page" -TargetPath "C:\Program Files\Internet Explorer\iexplore.exe" -Shortcuts "CommonStartup" -Arguments $landingPageUrl
if ($style -eq "devpreview") {
New-DesktopShortcut -Name "Modern Dev Tools" -TargetPath "C:\Program Files\Internet Explorer\iexplore.exe" -Shortcuts "CommonStartup" -Arguments "http://aka.ms/moderndevtools"
}
if ($artifactUrl -ne "") {
$imageName = Get-BestGenericImageName
AddToStatus "Pulling $imageName (this might take some time)"
if (!(DockerDo -imageName $imageName -command pull)) {
throw "Error pulling image"
}
}
else {
$imageName = ""
$navDockerImage.Split(',') | Where-Object { $_ } | ForEach-Object {
$registry = $_.Split('/')[0]
if (($registry -ne "microsoft") -and ($registryUsername -ne "") -and ($registryPassword -ne "")) {
AddToStatus "Logging in to $registry"
docker login "$registry" -u "$registryUsername" -p "$registryPassword"
}
$imageName = Get-BestNavContainerImageName -imageName $_
AddToStatus "Pulling $imageName (this might take ~30 minutes)"
if (!(DockerDo -imageName $imageName -command pull)) {
throw "Error pulling image"
}
}
}
AddToStatus "Installing Visual C++ Redist"
$vcRedistUrl = "https://download.microsoft.com/download/2/E/6/2E61CFA4-993B-4DD4-91DA-3737CD5CD6E3/vcredist_x86.exe"
$vcRedistFile = "C:\DOWNLOAD\vcredist_x86.exe"
Download-File -sourceUrl $vcRedistUrl -destinationFile $vcRedistFile
Start-Process $vcRedistFile -argumentList "/q" -wait
AddToStatus "Installing SQL Native Client"
$sqlncliUrl = "https://download.microsoft.com/download/3/A/6/3A632674-A016-4E31-A675-94BE390EA739/ENU/x64/sqlncli.msi"
$sqlncliFile = "C:\DOWNLOAD\sqlncli.msi"
Download-File -sourceUrl $sqlncliUrl -destinationFile $sqlncliFile
Start-Process "C:\Windows\System32\msiexec.exe" -argumentList "/i $sqlncliFile ADDLOCAL=ALL IACCEPTSQLNCLILICENSETERMS=YES /qn" -wait
AddToStatus "Installing OpenXML 2.5"
$openXmlUrl = "https://download.microsoft.com/download/5/5/3/553C731E-9333-40FB-ADE3-E02DC9643B31/OpenXMLSDKV25.msi"
$openXmlFile = "C:\DOWNLOAD\OpenXMLSDKV25.msi"
Download-File -sourceUrl $openXmlUrl -destinationFile $openXmlFile
Start-Process $openXmlFile -argumentList "/qn /q /passive" -wait
$beforeContainerSetupScript = (Join-Path $PSScriptRoot "BeforeContainerSetupScript.ps1")
if (Test-Path $beforeContainerSetupScript) {
AddToStatus "Running beforeContainerSetupScript"
. $beforeContainerSetupScript
}
. "c:\demo\SetupNavContainer.ps1"
. "c:\demo\SetupDesktop.ps1"
$finalSetupScript = (Join-Path $PSScriptRoot "FinalSetupScript.ps1")
if (Test-Path $finalSetupScript) {
AddToStatus "Running FinalSetupScript"
. $finalSetupScript
}
if (Get-ScheduledTask -TaskName SetupStart -ErrorAction Ignore) {
schtasks /DELETE /TN SetupStart /F | Out-Null
}
if (Get-ScheduledTask -TaskName SetupVm -ErrorAction Ignore) {
schtasks /DELETE /TN SetupVm /F | Out-Null
}
if ($RunWindowsUpdate -eq "Yes") {
AddToStatus "Installing Windows Updates"
install-module PSWindowsUpdate -force
Get-WUInstall -install -acceptall -autoreboot | ForEach-Object { AddToStatus ($_.Status + " " + $_.KB + " " +$_.Title) }
AddToStatus "Windows updates installed"
}
if (!($imageName)) {
Remove-Item -path "c:\demo\status.txt" -Force -ErrorAction SilentlyContinue
}
shutdown -r -t 30
} catch {
AddToStatus -Color Red -line $_.Exception.Message
$_.ScriptStackTrace.Replace("`r`n","`n").Split("`n") | ForEach-Object { AddToStatus -Color Red -line $_ }
throw
}