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

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

  1. 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"
}
  1. 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."
}
  1. 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'
  1. 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
    }
}
  1. 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)
  1. Extract Agent ZIP:

    • It uses the System.IO.Compression.FileSystem assembly to extract the downloaded ZIP file into the current directory.
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($agentZip, "$PWD")
  1. 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
  1. 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.