Posted on 1 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 pat --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.

1 thought on “Install Azure DevOps agent using powershell

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.