Skip to content

Instantly share code, notes, and snippets.

@s-bauer
Last active August 4, 2019 11:00
Show Gist options
  • Save s-bauer/640179c97ef3283e5cf1d627a8f1660c to your computer and use it in GitHub Desktop.
Save s-bauer/640179c97ef3283e5cf1d627a8f1660c to your computer and use it in GitHub Desktop.
Configure Azure Devops Agents in bulk
param(
[Parameter(Mandatory=$true)] [string] $AzDoTenant,
[Parameter(Mandatory=$true)] [string] $AzDoPAT,
[Parameter(Mandatory=$true)] [string] $AgentPool,
[Parameter(Mandatory=$true)] [string] $WorkDirectory,
[Parameter(Mandatory=$true)] [int] $AgentCount,
[Parameter(Mandatory=$false)] [string] $AgentPrefix = "Agent_"
)
$InformationPreference = "Continue"
$WarningPreference = "Continue"
$ErrorPreference = "Stop"
$agentDownloadURL = "https://vstsagentpackage.azureedge.net/agent/2.154.3/vsts-agent-win-x64-2.154.3.zip"
$rootFolder = Get-Location
$agentZip = Join-Path $rootFolder "agent.zip"
$agentTemplate = Join-Path $rootFolder "agent_template"
# Download Agent
if(Test-Path $agentZip) {
Remove-Item $agentZip -Force
}
Write-Information "Downloading Agent from `"$agentDownloadURL`""
$ProgressPreference = "SilentlyContinue"
Invoke-WebRequest $agentDownloadURL -OutFile $agentZip
# Ensure AgentTemplate directory
if(Test-Path $agentTemplate) {
Remove-Item $agentTemplate -Recurse -Force
}
New-Item $agentTemplate -ItemType Directory | Out-Null
# Extract Agent
Write-Information "Extracting Agent to `"$agentTemplate`""
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($agentZip, $agentTemplate)
# Create individual Agents
for($i=0; $i -lt $AgentCount; $i++) {
$cAgentName = "$($AgentPrefix)$($i)"
$cAgentPath = Join-Path $rootFolder $cAgentName
Write-Information "Setting up `"$cAgentName`""
if(Test-Path $cAgentPath) {
Remove-Item $cAgentPath -Recurse -Force
}
Copy-Item -Path $agentTemplate -Destination $cAgentPath -Recurse
$cWorkDir = Join-Path $WorkDirectory $cAgentName
$command = "$($cAgentPath)\config.cmd"
$command += " --unattended"
$command += " --url https://dev.azure.com/$AzDoTenant"
$command += " --auth pat"
$command += " --token $AzDoPAT"
$command += " --pool `"$AgentPool`""
$command += " --agent `"$cAgentName`""
$command += " --replace"
$command += " --runAsService"
$command += " --windowsLogonAccount `"NT AUTHORITY\NETWORK SERVICE`""
$command += " --work `"$cWorkDir`""
Invoke-Expression $command
}
Remove-Item $agentZip -Force -Recurse
Remove-Item $agentTemplate -Force -Recurse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment