forked from microsoft/nav-arm-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize.ps1
379 lines (333 loc) · 19.1 KB
/
initialize.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
# usage initialize.ps1
param
(
[string] $templateLink = "https://raw.githubusercontent.com/Microsoft/nav-arm-templates/master/navdeveloperpreview.json",
[string] $containerName = "navserver",
[string] $hostName = "",
[string] $storageConnectionString = "",
[string] $vmAdminUsername = "vmadmin",
[string] $navAdminUsername = "admin",
[string] $azureSqlAdminUsername = "sqladmin",
[string] $adminPassword = "P@ssword1",
[string] $navDockerImage = "",
[string] $registryUsername = "",
[string] $registryPassword = "",
[string] $sqlServerType = "SQLExpress",
[string] $azureSqlServer = "",
[string] $appBacpacUri = "",
[string] $tenantBacpacUri = "",
[string] $databaseBakUri = "",
[string] $includeAppUris = "",
[string] $enableSymbolLoading = "No",
[string] $includeCSIDE = "No",
[string] $includeAL = "No",
[string] $clickonce = "No",
[string] $enableTaskScheduler = "Default",
[string] $licenseFileUri = "",
[string] $certificatePfxUrl = "",
[string] $certificatePfxPassword = "",
[string] $publicDnsName = "",
[string] $fobFileUrl = "",
[string] $workshopFilesUrl = "",
[string] $beforeContainerSetupScriptUrl = "",
[string] $finalSetupScriptUrl = "",
[string] $style = "devpreview",
[string] $AssignPremiumPlan = "No",
[string] $CreateTestUsers = "No",
[string] $CreateAadUsers = "No",
[string] $RunWindowsUpdate = "No",
[string] $Multitenant = "No",
[string] $ContactEMailForLetsEncrypt= "",
[string] $RemoteDesktopAccess = "*",
[string] $WinRmAccess = "-",
[string] $BingMapsKey = "",
[string] $Office365UserName = "",
[string] $Office365Password = "",
[string] $Office365CreatePortal = "No",
[string] $requestToken = "",
[string] $createStorageQueue = "",
[string] $AddTraefik = "No",
[string] $nchBranch = ""
)
function Get-VariableDeclaration([string]$name) {
$var = Get-Variable -Name $name
if ($var) {
('$'+$var.Name+' = "'+$var.Value+'"')
} else {
""
}
}
function Log([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"
}
function Download-File([string]$sourceUrl, [string]$destinationFile)
{
Log "Downloading $destinationFile"
Remove-Item -Path $destinationFile -Force -ErrorAction Ignore
(New-Object System.Net.WebClient).DownloadFile($sourceUrl, $destinationFile)
}
if ($publicDnsName -eq "") {
$publicDnsName = $hostname
}
$ComputerInfo = Get-ComputerInfo
$WindowsInstallationType = $ComputerInfo.WindowsInstallationType
$WindowsProductName = $ComputerInfo.WindowsProductName
[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
$settingsScript = "c:\demo\settings.ps1"
if (Test-Path $settingsScript) {
. "$settingsScript"
} else {
New-Item -Path "c:\myfolder" -ItemType Directory -ErrorAction Ignore | Out-Null
New-Item -Path "C:\DEMO" -ItemType Directory -ErrorAction Ignore | Out-Null
Get-VariableDeclaration -name "templateLink" | Set-Content $settingsScript
Get-VariableDeclaration -name "hostName" | Add-Content $settingsScript
Get-VariableDeclaration -name "StorageConnectionString"| Add-Content $settingsScript
Get-VariableDeclaration -name "containerName" | Add-Content $settingsScript
Get-VariableDeclaration -name "vmAdminUsername" | Add-Content $settingsScript
Get-VariableDeclaration -name "navAdminUsername" | Add-Content $settingsScript
Get-VariableDeclaration -name "azureSqlAdminUsername" | Add-Content $settingsScript
Get-VariableDeclaration -name "Office365Username" | Add-Content $settingsScript
Get-VariableDeclaration -name "Office365CreatePortal" | Add-Content $settingsScript
Get-VariableDeclaration -name "navDockerImage" | Add-Content $settingsScript
Get-VariableDeclaration -name "registryUsername" | Add-Content $settingsScript
Get-VariableDeclaration -name "registryPassword" | Add-Content $settingsScript
Get-VariableDeclaration -name "sqlServerType" | Add-Content $settingsScript
Get-VariableDeclaration -name "azureSqlServer" | Add-Content $settingsScript
Get-VariableDeclaration -name "appBacpacUri" | Add-Content $settingsScript
Get-VariableDeclaration -name "tenantBacpacUri" | Add-Content $settingsScript
Get-VariableDeclaration -name "databaseBakUri" | Add-Content $settingsScript
Get-VariableDeclaration -name "includeAppUris" | Add-Content $settingsScript
Get-VariableDeclaration -name "enableSymbolLoading" | Add-Content $settingsScript
Get-VariableDeclaration -name "includeCSIDE" | Add-Content $settingsScript
Get-VariableDeclaration -name "includeAL" | Add-Content $settingsScript
Get-VariableDeclaration -name "clickonce" | Add-Content $settingsScript
Get-VariableDeclaration -name "enableTaskScheduler" | Add-Content $settingsScript
Get-VariableDeclaration -name "licenseFileUri" | Add-Content $settingsScript
Get-VariableDeclaration -name "publicDnsName" | Add-Content $settingsScript
Get-VariableDeclaration -name "workshopFilesUrl" | Add-Content $settingsScript
Get-VariableDeclaration -name "style" | Add-Content $settingsScript
Get-VariableDeclaration -name "RunWindowsUpdate" | Add-Content $settingsScript
Get-VariableDeclaration -name "AssignPremiumPlan" | Add-Content $settingsScript
Get-VariableDeclaration -name "CreateTestUsers" | Add-Content $settingsScript
Get-VariableDeclaration -name "CreateAadUsers" | Add-Content $settingsScript
Get-VariableDeclaration -name "Multitenant" | Add-Content $settingsScript
Get-VariableDeclaration -name "WindowsInstallationType"| Add-Content $settingsScript
Get-VariableDeclaration -name "WindowsProductName" | Add-Content $settingsScript
Get-VariableDeclaration -name "ContactEMailForLetsEncrypt" | Add-Content $settingsScript
Get-VariableDeclaration -name "RemoteDesktopAccess" | Add-Content $settingsScript
Get-VariableDeclaration -name "WinRmAccess" | Add-Content $settingsScript
Get-VariableDeclaration -name "BingMapsKey" | Add-Content $settingsScript
Get-VariableDeclaration -name "RequestToken" | Add-Content $settingsScript
Get-VariableDeclaration -name "CreateStorageQueue" | Add-Content $settingsScript
Get-VariableDeclaration -name "AddTraefik" | Add-Content $settingsScript
$passwordKey = New-Object Byte[] 16
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($passwordKey)
('$passwordKey = [byte[]]@('+"$passwordKey".Replace(" ",",")+')') | Add-Content $settingsScript
$securePassword = ConvertTo-SecureString -String $adminPassword -AsPlainText -Force
$encPassword = ConvertFrom-SecureString -SecureString $securePassword -Key $passwordKey
('$adminPassword = "'+$encPassword+'"') | Add-Content $settingsScript
$encOffice365Password = ""
if ("$Office365Password" -ne "") {
$secureOffice365Password = ConvertTo-SecureString -String $Office365Password -AsPlainText -Force
$encOffice365Password = ConvertFrom-SecureString -SecureString $secureOffice365Password -Key $passwordKey
}
('$Office365Password = "'+$encOffice365Password+'"') | Add-Content $settingsScript
}
#
# styles:
# devpreview
# developer
# workshop
# sandbox
# demo
#
$includeWindowsClient = $true
if (Test-Path -Path "c:\DEMO\Status.txt" -PathType Leaf) {
Log "VM already initialized."
exit
}
Set-Content "c:\DEMO\RemoteDesktopAccess.txt" -Value $RemoteDesktopAccess
Set-Content "c:\DEMO\WinRmAccess.txt" -Value $WinRmAccess
Set-ExecutionPolicy -ExecutionPolicy unrestricted -Force
Log -color Green "Starting initialization"
Log "Running $WindowsProductName"
Log "Initialize, user: $env:USERNAME"
Log "TemplateLink: $templateLink"
$scriptPath = $templateLink.SubString(0,$templateLink.LastIndexOf('/')+1)
New-Item -Path "C:\DOWNLOAD" -ItemType Directory -ErrorAction Ignore | Out-Null
if (!(Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction Ignore)) {
Log "Installing NuGet Package Provider"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.208 -Force -WarningAction Ignore | Out-Null
}
if (!(Get-Module powershellget | Where-Object { $_.Version -ge [version]"2.2.1" })) {
Log "Installing PowerShellGet 2.2.1"
Install-Module powershellget -RequiredVersion 2.2.1 -force
}
Log "Installing Internet Information Server (this might take a few minutes)"
if ($WindowsInstallationType -eq "Server") {
Add-WindowsFeature Web-Server,web-Asp-Net45
} else {
Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServer,IIS-ASPNET45 -All -NoRestart | Out-Null
}
Remove-Item -Path "C:\inetpub\wwwroot\iisstart.*" -Force
Download-File -sourceUrl "${scriptPath}Default.aspx" -destinationFile "C:\inetpub\wwwroot\default.aspx"
Download-File -sourceUrl "${scriptPath}status.aspx" -destinationFile "C:\inetpub\wwwroot\status.aspx"
Download-File -sourceUrl "${scriptPath}line.png" -destinationFile "C:\inetpub\wwwroot\line.png"
Download-File -sourceUrl "${scriptPath}Microsoft.png" -destinationFile "C:\inetpub\wwwroot\Microsoft.png"
Download-File -sourceUrl "${scriptPath}web.config" -destinationFile "C:\inetpub\wwwroot\web.config"
if ($requestToken) {
Download-File -sourceUrl "${scriptPath}request.aspx" -destinationFile "C:\inetpub\wwwroot\request.aspx"
}
$title = 'Dynamics Container Host'
[System.IO.File]::WriteAllText("C:\inetpub\wwwroot\title.txt", $title)
[System.IO.File]::WriteAllText("C:\inetpub\wwwroot\hostname.txt", $publicDnsName)
if ("$RemoteDesktopAccess" -ne "") {
Log "Creating Connect.rdp"
"full address:s:${publicDnsName}:3389
prompt for credentials:i:1
username:s:$vmAdminUsername" | Set-Content "c:\inetpub\wwwroot\Connect.rdp"
}
if ($WindowsInstallationType -eq "Server") {
Log "Turning off IE Enhanced Security Configuration"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" -Name "IsInstalled" -Value 0 | Out-Null
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}" -Name "IsInstalled" -Value 0 | Out-Null
}
$setupDesktopScript = "c:\demo\SetupDesktop.ps1"
$setupStartScript = "c:\demo\SetupStart.ps1"
$setupVmScript = "c:\demo\SetupVm.ps1"
$setupNavContainerScript = "c:\demo\SetupNavContainer.ps1"
$setupAadScript = "c:\demo\SetupAAD.ps1"
if ($vmAdminUsername -ne $navAdminUsername) {
'. "c:\run\SetupWindowsUsers.ps1"
Write-Host "Creating Host Windows user"
$hostUsername = "'+$vmAdminUsername+'"
if (!($securePassword)) {
# old version of the generic nav container
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
}
New-LocalUser -AccountNeverExpires -FullName $hostUsername -Name $hostUsername -Password $securePassword -ErrorAction Ignore | Out-Null
Add-LocalGroupMember -Group administrators -Member $hostUsername -ErrorAction Ignore
' | Set-Content "c:\myfolder\SetupWindowsUsers.ps1"
}
Download-File -sourceUrl "${scriptPath}SetupWebClient.ps1" -destinationFile "c:\myfolder\SetupWebClient.ps1"
Download-File -sourceUrl "${scriptPath}SetupDesktop.ps1" -destinationFile $setupDesktopScript
Download-File -sourceUrl "${scriptPath}SetupNavContainer.ps1" -destinationFile $setupNavContainerScript
Download-File -sourceUrl "${scriptPath}SetupAAD.ps1" -destinationFile $setupAadScript
Download-File -sourceUrl "${scriptPath}SetupVm.ps1" -destinationFile $setupVmScript
Download-File -sourceUrl "${scriptPath}SetupStart.ps1" -destinationFile $setupStartScript
Download-File -sourceUrl "${scriptPath}RestartContainers.ps1" -destinationFile "c:\demo\restartContainers.ps1"
if ($requestToken) {
Download-File -sourceUrl "${scriptPath}Request.ps1" -destinationFile "C:\DEMO\Request.ps1"
Download-File -sourceUrl "${scriptPath}RequestTaskDef.xml" -destinationFile "C:\DEMO\RequestTaskDef.xml"
}
if ("$createStorageQueue" -eq "yes") {
Download-File -sourceUrl "${scriptPath}RunQueue.ps1" -destinationFile "C:\DEMO\RunQueue.ps1"
}
if ("$requestToken" -ne "" -or "$createStorageQueue" -eq "yes") {
# Request commands
New-Item -Path "C:\DEMO\request" -ItemType Directory | Out-Null
Download-File -sourceUrl "${scriptPath}request\Demo.ps1" -destinationFile "C:\DEMO\request\Demo.ps1"
Download-File -sourceUrl "${scriptPath}request\ReplaceNavServerContainer.ps1" -destinationFile "C:\DEMO\request\ReplaceNavServerContainer.ps1"
Download-File -sourceUrl "${scriptPath}request\RestartComputer.ps1" -destinationFile "C:\DEMO\request\RestartComputer.ps1"
}
Download-File -sourceUrl "${scriptPath}Install-VS2017Community.ps1" -destinationFile "C:\DEMO\Install-VS2017Community.ps1"
if ($beforeContainerSetupScriptUrl) {
$beforeContainerSetupScript = "c:\demo\BeforeContainerSetupScript.ps1"
Download-File -sourceUrl $beforeContainerSetupScriptUrl -destinationFile $beforeContainerSetupScript
}
if ($finalSetupScriptUrl) {
$finalSetupScript = "c:\demo\FinalSetupScript.ps1"
Download-File -sourceUrl $finalSetupScriptUrl -destinationFile $finalSetupScript
}
if ($fobFileUrl -ne "") {
Download-File -sourceUrl $fobFileUrl -destinationFile "c:\demo\objects.fob"
}
if ($workshopFilesUrl -ne "") {
$workshopFilesFolder = "c:\WorkshopFiles"
$workshopFilesFile = "C:\DOWNLOAD\WorkshopFiles.zip"
New-Item -Path $workshopFilesFolder -ItemType Directory -ErrorAction Ignore | Out-Null
Download-File -sourceUrl $workshopFilesUrl -destinationFile $workshopFilesFile
Log "Unpacking Workshop Files to $WorkshopFilesFolder"
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.Filesystem") | Out-Null
[System.IO.Compression.ZipFile]::ExtractToDirectory($workshopFilesFile, $workshopFilesFolder)
}
if ($nchBranch) {
if ($nchBranch -notlike "https://*") {
$nchBranch = "https://github.com/Microsoft/navcontainerhelper/archive/$($nchBranch).zip"
}
Log "Using Nav Container Helper from $nchBranch"
Download-File -sourceUrl $nchBranch -destinationFile "c:\demo\navcontainerhelper.zip"
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.Filesystem") | Out-Null
[System.IO.Compression.ZipFile]::ExtractToDirectory("c:\demo\navcontainerhelper.zip", "c:\demo")
$module = Get-Item -Path "C:\demo\*\NavContainerHelper.psm1"
Log "Loading NavContainerHelper from $($module.FullName)"
Import-Module $module.FullName -DisableNameChecking
} else {
Log "Installing Latest Nav Container Helper from PowerShell Gallery"
Install-Module -Name navcontainerhelper -Force
Import-Module -Name navcontainerhelper -DisableNameChecking
Log ("Using Nav Container Helper version "+(get-module NavContainerHelper).Version.ToString())
}
if ($AddTraefik -eq "Yes") {
if ($certificatePfxUrl -ne "" -and $certificatePfxPassword -ne "") {
Log -color Red "Certificate specified, cannot add Traefik"
$AddTraefik = "No"
}
if (-not $ContactEMailForLetsEncrypt) {
Log -color Red "Contact EMail for LetsEncrypt not specified, cannot add Traefik"
$AddTraefik = "No"
}
if ($clickonce -eq "Yes") {
Log -color Red "ClickOnce specified, cannot add Traefik"
$AddTraefik = "No"
}
if ($AddTraefik -eq "Yes") {
Setup-TraefikContainerForNavContainers -overrideDefaultBinding -PublicDnsName $publicDnsName -ContactEMailForLetsEncrypt $ContactEMailForLetsEncrypt
}
else {
Get-VariableDeclaration -name "AddTraefik" | Add-Content $settingsScript
}
}
if ($certificatePfxUrl -ne "" -and $certificatePfxPassword -ne "") {
Download-File -sourceUrl $certificatePfxUrl -destinationFile "c:\programdata\navcontainerhelper\certificate.pfx"
('$certificatePfxPassword = "'+$certificatePfxPassword+'"
$certificatePfxFile = "c:\programdata\navcontainerhelper\certificate.pfx"
$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 "Importing Certificate to LocalMachine\my"
Import-PfxCertificate -FilePath $certificatePfxFile -CertStoreLocation cert:\localMachine\my -Password (ConvertTo-SecureString -String $certificatePfxPassword -AsPlainText -Force) | Out-Null
}
$dnsidentity = $cert.GetNameInfo("SimpleName",$false)
if ($dnsidentity.StartsWith("*")) {
$dnsidentity = $dnsidentity.Substring($dnsidentity.IndexOf(".")+1)
}
Write-Host "DNS identity $dnsidentity"
') | Set-Content "c:\myfolder\SetupCertificate.ps1"
('Write-Host "DNS identity $dnsidentity"
') | Set-Content "c:\myfolder\AdditionalSetup.ps1"
$ContactEMailForLetsEncrypt = ""
Get-VariableDeclaration -name "ContactEMailForLetsEncrypt" | Add-Content $settingsScript
}
if ($WindowsInstallationType -eq "Server") {
if (!(Test-Path -Path "C:\Program Files\Docker\docker.exe" -PathType Leaf)) {
Log "Installing Docker"
Install-module DockerMsftProvider -Force
Install-Package -Name docker -ProviderName DockerMsftProvider -Force
}
} else {
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V, Containers -All -NoRestart | Out-Null
}
$startupAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File $setupStartScript"
$startupTrigger = New-ScheduledTaskTrigger -AtStartup
$startupTrigger.Delay = "PT1M"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
Register-ScheduledTask -TaskName "SetupStart" `
-Action $startupAction `
-Trigger $startupTrigger `
-Settings $settings `
-RunLevel "Highest" `
-User "NT AUTHORITY\SYSTEM" | Out-Null
Log "Restarting computer and start Installation tasks"
Shutdown -r -t 60