-
Notifications
You must be signed in to change notification settings - Fork 55
/
SetupStart.ps1
177 lines (149 loc) · 8.02 KB
/
SetupStart.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
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
}
function Download-File([string]$sourceUrl, [string]$destinationFile)
{
AddToStatus "Downloading $destinationFile"
Remove-Item -Path $destinationFile -Force -ErrorAction Ignore
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
(New-Object System.Net.WebClient).DownloadFile($sourceUrl, $destinationFile)
}
function Register-NativeMethod([string]$dll, [string]$methodSignature)
{
$script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; }
}
function Add-NativeMethods()
{
$nativeMethodsCode = $script:nativeMethods | % { "
[DllImport(`"$($_.Dll)`")]
public static extern $($_.Signature);
" }
Add-Type @"
using System;
using System.Text;
using System.Runtime.InteropServices;
public class NativeMethods {
$nativeMethodsCode
}
"@
}
AddToStatus "SetupStart, User: $env:USERNAME"
. (Join-Path $PSScriptRoot "settings.ps1")
$ComputerInfo = Get-ComputerInfo
$WindowsInstallationType = $ComputerInfo.WindowsInstallationType
$WindowsProductName = $ComputerInfo.WindowsProductName
if ($nchBranch -eq "preview") {
AddToStatus "Installing Latest BcContainerHelper preview from PowerShell Gallery"
Install-Module -Name bccontainerhelper -Force -AllowPrerelease
Import-Module -Name bccontainerhelper -DisableNameChecking
AddToStatus ("Using BcContainerHelper version "+(get-module BcContainerHelper).Version.ToString())
}
elseif ($nchBranch -eq "") {
AddToStatus "Installing Latest Business Central Container Helper from PowerShell Gallery"
Install-Module -Name bccontainerhelper -Force
Import-Module -Name bccontainerhelper -DisableNameChecking
AddToStatus ("Using BcContainerHelper version "+(get-module BcContainerHelper).Version.ToString())
} else {
if ($nchBranch -notlike "https://*") {
$nchBranch = "https://github.com/Microsoft/navcontainerhelper/archive/$($nchBranch).zip"
}
AddToStatus "Using BcContainerHelper from $nchBranch"
Download-File -sourceUrl $nchBranch -destinationFile "c:\demo\bccontainerhelper.zip"
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.Filesystem") | Out-Null
[System.IO.Compression.ZipFile]::ExtractToDirectory("c:\demo\bccontainerhelper.zip", "c:\demo")
$module = Get-Item -Path "C:\demo\*\BcContainerHelper.psm1"
AddToStatus "Loading BcContainerHelper from $($module.FullName)"
Import-Module $module.FullName -DisableNameChecking
}
if (-not (Get-InstalledModule Az -ErrorAction SilentlyContinue)) {
AddToStatus "Installing Az module"
Install-Module Az -Force
}
if (-not (Get-InstalledModule AzureAD -ErrorAction SilentlyContinue)) {
AddToStatus "Installing AzureAD module"
Install-Module AzureAD -Force
}
if (-not (Get-InstalledModule "Microsoft.Graph" -ErrorAction SilentlyContinue)) {
AddToStatus "Installing Microsoft.Graph module"
Install-Module "Microsoft.Graph" -Force
}
if (-not (Get-InstalledModule SqlServer -ErrorAction SilentlyContinue)) {
AddToStatus "Installing SqlServer module"
Install-Module SqlServer -Force
}
$securePassword = ConvertTo-SecureString -String $adminPassword -Key $passwordKey
$plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword))
if ($requestToken) {
if (!(Get-ScheduledTask -TaskName request -ErrorAction Ignore)) {
AddToStatus "Registering request task"
$xml = [System.IO.File]::ReadAllText("c:\demo\RequestTaskDef.xml")
Register-ScheduledTask -TaskName request -User $vmadminUsername -Password $plainPassword -Xml $xml
}
}
if ("$createStorageQueue" -eq "yes") {
if (-not (Get-InstalledModule AzTable -ErrorAction SilentlyContinue)) {
AddToStatus "Installing AzTable Module"
Install-Module AzTable -Force
$taskName = "RunQueue"
$startupAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\demo\RunQueue.ps1"
$startupTrigger = New-ScheduledTaskTrigger -AtStartup
$startupTrigger.Delay = "PT5M"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
$task = Register-ScheduledTask -TaskName $taskName `
-Action $startupAction `
-Trigger $startupTrigger `
-Settings $settings `
-RunLevel Highest `
-User $vmAdminUsername `
-Password $plainPassword
$task.Triggers.Repetition.Interval = "PT5M"
$task | Set-ScheduledTask -User $vmAdminUsername -Password $plainPassword | Out-Null
Start-ScheduledTask -TaskName $taskName
}
}
$taskName = "RestartContainers"
if (-not (Get-ScheduledTask -TaskName $taskName -ErrorAction Ignore)) {
AddToStatus "Register RestartContainers Task to start container delayed"
$startupAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -file c:\demo\restartcontainers.ps1"
$startupTrigger = New-ScheduledTaskTrigger -AtStartup
$startupTrigger.Delay = "PT5M"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
$task = Register-ScheduledTask -TaskName $taskName `
-Action $startupAction `
-Trigger $startupTrigger `
-Settings $settings `
-RunLevel Highest `
-User $vmadminUsername `
-Password $plainPassword
}
if ($WindowsInstallationType -eq "Server") {
if (Get-ScheduledTask -TaskName SetupVm -ErrorAction Ignore) {
schtasks /DELETE /TN SetupVm /F | Out-Null
}
AddToStatus "Launch SetupVm"
$onceAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\demo\setupVm.ps1"
Register-ScheduledTask -TaskName SetupVm `
-Action $onceAction `
-RunLevel Highest `
-User $vmAdminUsername `
-Password $plainPassword | Out-Null
Start-ScheduledTask -TaskName SetupVm
}
else {
if (Get-ScheduledTask -TaskName SetupStart -ErrorAction Ignore) {
schtasks /DELETE /TN SetupStart /F | Out-Null
}
$startupAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\demo\SetupVm.ps1"
$startupTrigger = New-ScheduledTaskTrigger -AtStartup
$startupTrigger.Delay = "PT1M"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd -WakeToRun
Register-ScheduledTask -TaskName "SetupVm" `
-Action $startupAction `
-Trigger $startupTrigger `
-Settings $settings `
-RunLevel "Highest" `
-User $vmAdminUsername `
-Password $plainPassword | Out-Null
AddToStatus -color Yellow "Restarting computer. After restart, please Login to computer using RDP in order to resume the installation process. This is not needed for Windows Server."
Shutdown -r -t 60
}