Skip to content

Instantly share code, notes, and snippets.

@MaxMelcher
Last active February 20, 2024 07:02
Show Gist options
  • Save MaxMelcher/fa10b8bd7f1e39910c0402576ccf1b1f to your computer and use it in GitHub Desktop.
Save MaxMelcher/fa10b8bd7f1e39910c0402576ccf1b1f to your computer and use it in GitHub Desktop.
Powershell to install the latest Azure DevOps Agent
param (
[string]$URL,
[string]$PAT,
[string]$POOL,
[string]$AGENT
)
Write-Host "start"
if (test-path "c:\agent")
{
Remove-Item -Path "c:\agent" -Force -Confirm:$false -Recurse
}
new-item -ItemType Directory -Force -Path "c:\agent"
set-location "c:\agent"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$wr = Invoke-WebRequest https://api.github.com/repos/Microsoft/azure-pipelines-agent/releases/latest
$tag = ($wr | ConvertFrom-Json)[0].tag_name
$tag = $tag.Substring(1)
write-host "$tag is the latest version"
$url = "https://vstsagentpackage.azureedge.net/agent/$tag/vsts-agent-win-x64-$tag.zip"
Invoke-WebRequest $url -Out agent.zip
Expand-Archive -Path agent.zip -DestinationPath $PWD
.\config.cmd --unattended --url $URL --auth pat --token $PAT --pool $POOL --agent $AGENT --acceptTeeEula --runAsService
exit 0
@UstDoesTech
Copy link

This is so incredibly helpful! Thank you for sharing - this has probably saved me hours of work.

@S0meth1ng85
Copy link

S0meth1ng85 commented Aug 12, 2023

Thanks for the script.
If you use Azure Custom Script Extension it is necessary to use -UseBasicParsing parameter
$wr = Invoke-WebRequest -Uri "https://api.github.com/repos/Microsoft/azure-pipelines-agent/releases/latest" -UseBasicParsing

More info: MicrosoftDocs/azure-docs#14377
https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows#using-invoke-webrequest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment