Setting Up Your Azure DevOps Environment: A Step-by-Step Guide to Configuring Agents for Seamless Integration

Administrator Check:
- The script checks whether the current PowerShell session is running with administrator privileges. If not, it throws an error, indicating that the script should be run in an administrator PowerShell prompt.
$ErrorActionPreference="Stop"
If (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
throw "Run command in an administrator PowerShell prompt"
}
PowerShell Version Check:
- It checks if the version of PowerShell is at least 3.0. If not, it throws an error.
If ($PSVersionTable.PSVersion -lt (New-Object System.Version("3.0"))) {
throw "The minimum version of Windows PowerShell that is required by the script (3.0) does not match the currently running version of Windows PowerShell."
}
Agent Directory Creation:
- It checks if the directory 'azagent' exists on the system drive. If not, it creates the directory.
If (-not (Test-Path $env:SystemDrive\'azagent')) {
mkdir $env:SystemDrive\'azagent'
}
cd $env:SystemDrive\'azagent'
Agent Directory Structure Creation:
- It iterates through possible subdirectories 'A1' to 'A99' within 'azagent' and creates the first non-existing subdirectory.
for ($i = 1; $i -lt 100; $i++) {
$destFolder = "A" + $i.ToString()
if (-not (Test-Path ($destFolder))) {
mkdir $destFolder
cd $destFolder
break
}
}
Download Agent ZIP:
- It defines the URI for the agent ZIP file and downloads it. The script sets up TLS 1.2 as the security protocol for the download.
$agentZip = "$PWD\agent.zip"
$DefaultProxy = [System.Net.WebRequest]::DefaultWebProxy
$securityProtocol = @()
$securityProtocol += [Net.ServicePointManager]::SecurityProtocol
$securityProtocol += [Net.SecurityProtocolType]::Tls12
[Net.ServicePointManager]::SecurityProtocol = $securityProtocol
$WebClient = New-Object Net.WebClient
$Uri = 'https://vstsagentpackage.azureedge.net/agent/3.230.0/vsts-agent-win-x64-3.230.0.zip'
if ($DefaultProxy -and (-not $DefaultProxy.IsBypassed($Uri))) {
$WebClient.Proxy = New-Object Net.WebProxy($DefaultProxy.GetProxy($Uri).OriginalString, $True)
}
$WebClient.DownloadFile($Uri, $agentZip)
Extract Agent ZIP:
- It uses the
System.IO.Compression.FileSystemassembly to extract the downloaded ZIP file into the current directory.
- It uses the
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($agentZip, "$PWD")
Agent Configuration:
- It runs the 'config.cmd' script with specified parameters to configure the agent.
.\config.cmd --environment --environmentname "Dev-server-new" --agent $env:COMPUTERNAME --runasservice --work '_work' --url 'https://dev.azure.com/muhammadhassanb333/' --projectname 'KIPS-LMS' --auth PAT --token tbgrwtefloyq7t42etklwrumrrfdsyfkvyxhu6zjfcx5helvdlwq
Cleanup:
- It removes the downloaded ZIP file.
Remove-Item $agentZip
In summary, the script sets up an Azure DevOps agent on a Windows machine. It checks for administrator privileges, PowerShell version, creates necessary directories, downloads and extracts the agent, and finally configures the agent with specified parameters. The script is designed to be run in a PowerShell prompt with administrative privileges.




