-
Notifications
You must be signed in to change notification settings - Fork 55
/
SetupDockerAgentVM.ps1
95 lines (73 loc) · 4.83 KB
/
SetupDockerAgentVM.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
$ErrorActionPreference = "Stop"
$WarningActionPreference = "Continue"
$ComputerInfo = Get-ComputerInfo
$WindowsInstallationType = $ComputerInfo.WindowsInstallationType
$WindowsProductName = $ComputerInfo.WindowsProductName
try {
if (Get-ScheduledTask -TaskName SetupVm -ErrorAction Ignore) {
schtasks /DELETE /TN SetupVm /F | Out-Null
}
function AddToStatus([string]$line) {
([DateTime]::Now.ToString([System.Globalization.DateTimeFormatInfo]::CurrentInfo.ShortTimePattern.replace(":mm",":mm:ss")) + " $line") | Add-Content -Path "c:\agent\status.txt" -Force -ErrorAction SilentlyContinue
}
function Login-Docker([string]$registry, [string]$registryUsername, [string]$registryPassword)
{
if ("$registryUsername" -ne "" -and "$registryPassword" -ne "") {
$securePassword = ConvertTo-SecureString -String $registryPassword -Key $passwordKey
$plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword))
AddToStatus "Login to '$registry' with $registryUsername"
docker login "$registry" -u "$registryUsername" -p "$plainPassword"
}
}
AddToStatus "SetupDockerAgentVm, User: $env:USERNAME"
. (Join-Path $PSScriptRoot "settings.ps1")
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
AddToStatus "Disabling Server Manager Open At Logon"
New-ItemProperty -Path "HKCU:\Software\Microsoft\ServerManager" -Name "DoNotOpenServerManagerAtLogon" -PropertyType "DWORD" -Value "0x1" –Force | Out-Null
Login-Docker -registry "$registry1" -registryUsername "$registry1username" -registryPassword "$registry1password"
Login-Docker -registry "$registry2" -registryUsername "$registry2username" -registryPassword "$registry2password"
Login-Docker -registry "$registry3" -registryUsername "$registry3username" -registryPassword "$registry3password"
Login-Docker -registry "$registry4" -registryUsername "$registry4username" -registryPassword "$registry4password"
if (Get-ScheduledTask -TaskName SetupStart -ErrorAction Ignore) {
schtasks /DELETE /TN SetupStart /F | Out-Null
}
$securePassword = ConvertTo-SecureString -String $adminPassword -Key $passwordKey
$plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword))
$vmno = [int]$vmName.Substring($vmName.LastIndexOf('-')+1)
AddToStatus "Register Build Agents"
1..$Processes | % {
$agentNo = $vmno*$processes+$_
$taskName = "$queue-$agentNo"
$startupAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\agent\StartDockerAgent.ps1 $taskName"
$startupTrigger = New-ScheduledTaskTrigger -AtStartup
$delay = (5+$_)
$startupTrigger.Delay = "PT${delay}M"
$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
}
AddToStatus "Complete, and start tasks"
shutdown -r -t 30
} catch {
AddToStatus $_.Exception.Message
$_.ScriptStackTrace.Replace("`r`n","`n").Split("`n") | % { AddToStatus $_ }
throw
}