forked from cegeka-dsa/nav-arm-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitbuildagent.ps1
82 lines (71 loc) · 3.82 KB
/
initbuildagent.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
#usage initbuildagent.ps1
param
(
[string] $templateLink = "https://raw.githubusercontent.com/Microsoft/nav-arm-templates/master/buildagent.json",
[Parameter(Mandatory=$true)]
[string] $vmAdminUsername,
[Parameter(Mandatory=$true)]
[string] $adminPassword,
[Parameter(Mandatory=$true)]
[string] $organization,
[Parameter(Mandatory=$true)]
[string] $token,
[Parameter(Mandatory=$true)]
[string] $pool,
[Parameter(Mandatory=$true)]
[string] $agentUrl,
[Parameter(Mandatory=$false)]
[string] $finalSetupScriptUrl,
[Parameter(Mandatory=$true)]
[int] $count = 1,
[Parameter(Mandatory=$true)]
[string] $vmname
)
function Download-File([string]$sourceUrl, [string]$destinationFile)
{
Remove-Item -Path $destinationFile -Force -ErrorAction Ignore
(New-Object System.Net.WebClient).DownloadFile($sourceUrl, $destinationFile)
}
[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
Set-ExecutionPolicy -ExecutionPolicy unrestricted -Force
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
if (!(Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction Ignore)) {
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.208 -Force -WarningAction Ignore | Out-Null
}
$DownloadFolder = "C:\Download"
MkDir $DownloadFolder -ErrorAction Ignore | Out-Null
$installDocker = (!(Test-Path -Path "C:\Program Files\Docker\docker.exe" -PathType Leaf))
if ($installDocker) {
$installDockerScriptUrl = $templateLink.Substring(0,$templateLink.LastIndexOf('/')+1)+'InstallOrUpdateDockerEngine.ps1'
$installDockerScript = Join-Path $DownloadFolder "InstallOrUpdateDockerEngine.ps1"
Download-File -sourceUrl $installDockerScriptUrl -destinationFile $installDockerScript
. $installDockerScript -Force -envScope "Machine"
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
$agentFilename = $agentUrl.Substring($agentUrl.LastIndexOf('/')+1)
$agentFullname = Join-Path $DownloadFolder $agentFilename
Download-File -sourceUrl $agentUrl -destinationFile $agentFullname
1..$count | ForEach-Object {
$agentName = "$vmName-$_"
$agentFolder = "C:\$agentName"
mkdir $agentFolder -ErrorAction Ignore | Out-Null
Set-Location $agentFolder
[System.IO.Compression.ZipFile]::ExtractToDirectory($agentFullname, $agentFolder)
if ($agentUrl -like 'https://github.com/actions/runner/releases/download/*') {
.\config.cmd --unattended --url "$organization" --token "$token" --name $agentName --labels "$pool" --runAsService --windowslogonaccount "NT AUTHORITY\SYSTEM"
}
else {
.\config.cmd --unattended --url "$organization" --auth PAT --token "$token" --pool "$pool" --agent $agentName --runAsService --windowslogonaccount "NT AUTHORITY\SYSTEM"
}
}
if ($finalSetupScriptUrl) {
if ($finalSetupScriptUrl -notlike "https://*" -and $finalSetupScriptUrl -notlike "http://*") {
$finalSetupScriptUrl = $templateLink.Substring(0,$templateLink.LastIndexOf('/')+1)+$finalSetupScriptUrl
}
Set-Content -Path (Join-Path $DownloadFolder "url.txt") -Value "$finalSetupScriptUrl"
$finalSetupScript = Join-Path $DownloadFolder "FinalSetupScript.ps1"
Download-File -sourceUrl $finalSetupScriptUrl -destinationFile $finalSetupScript
. $finalSetupScript
}
Shutdown -r -t 60