Posted on Leave a comment

Install Azure DevOps agent using powershell

With Azure devops you can create incredible automations. But in many cases you will need first to install the azure devops agent in order to run your tasks. Using the below powershell you can automate the installation of an azure devops agent.

New-Item "C:\agent" -itemType Directory
cd "C:\agent"
$url = ""
$token = "PAT_TOKEN"
$auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$token"))

$package = Invoke-RestMethod "$url/_apis/distributedtask/packages/agent?platform=win-x64&$`top=1" -Headers @{Authorization = "Basic $auth"}

$fileName = $package.value[0].fileName;
$downloadUrl = $package.value[0].downloadUrl;

Invoke-WebRequest -UseBasicParsing $downloadUrl -OutFile
Expand-Archive -Force -DestinationPath .
Remove-Item -Force

.\config.cmd --unattended --replace --acceptTeeEula --work work --url --pool YOUR_POOL_NAME --auth path --token $token --runAsService --runAsAutoLogon --windowsLogonAccount USER --windowsLogonPassword USER_PASSWORD

During the installation you may notice a problem for the powershell task to write on the log file.

Nevertheless the agent will be installed successfully.