Forked from stevenvolckaert/Register-AzurePipelinesAgent.ps1
Created
February 22, 2021 17:31
-
-
Save celluj34/c2b6961b8d0ae2cecb93a709af737ef4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 3 | |
#Requires -RunAsAdministrator | |
<# | |
.SYNOPSIS | |
Downloads and installs an Azure Pipelines Agent on the current machine, | |
and registers it with the specified Azure Devops organization, project, and environment. | |
.DESCRIPTION | |
This cmdlet downloads an Azure Pipelines Agent on the current machine, installs it to C:\azagent, | |
and finally runs the .\config.cmd command, which registers the agent with the specified | |
Azure DevOps organization, project, and environment. | |
.PARAMETER OrganizationName | |
The name of the Azure DevOps organization to register the agent with. | |
.PARAMETER ProjectName | |
The name of the Azure DevOps project to register the agent with. | |
.PARAMETER EnvironmentName | |
The name of the Azure DevOps environment to register the agent with. The environment must exist in the project. | |
.PARAMETER PersonalAccessToken | |
The Personal Access Token (PAT) to use when registering the agent with the Azure DevOps organization, | |
project, and environment. | |
.NOTES | |
File Name : Register-AzurePipelinesAgent.ps1 | |
Author : Steven Volckaert ([email protected]) | |
Prerequisites : PowerShell version 3 | |
.LINK | |
https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/v2-windows | |
https://docs.microsoft.com/en-us/azure/devops/pipelines/process/environments-virtual-machines | |
.EXAMPLE | |
.\Register-AzurePipelinesAgent.ps1 -OrganizationName contoso -ProjectName Contoso.Web -EnvironmentName Production -PersonalAccessToken 1234 | |
#> | |
Param ( | |
[Parameter(Mandatory = $True)] [string] $OrganizationName, | |
[Parameter(Mandatory = $True)] [string] $ProjectName, | |
[Parameter(Mandatory = $True)] [string] $EnvironmentName, | |
[Parameter(Mandatory = $True)] [string] $PersonalAccessToken | |
) | |
# FUNCTIONS ########################################################################################################### | |
# MAIN ################################################################################################################ | |
$ErrorActionPreference = "Stop"; | |
$Stopwatch = New-Object -TypeName System.Diagnostics.Stopwatch; | |
$CurrentPrincipal = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()); | |
If (-NOT $CurrentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { | |
throw "Run command in an Administrator PowerShell prompt."; | |
}; | |
If ($PSVersionTable.PSVersion -lt (New-Object System.Version("3.0"))) { | |
throw "The minimum version of Windows PowerShell that is required by this script (v3.0) does not match the currently running version of Windows PowerShell."; | |
}; | |
$AgentRootPath = "$env:SystemDrive\azagent"; | |
If (-NOT (Test-Path $AgentRootPath)) { | |
mkdir $AgentRootPath; | |
}; | |
Set-Location $AgentRootPath; | |
$AgentDirectory = ""; | |
for ($i = 1; $i -lt 100; $i++) { | |
$AgentDirectory = "A" + $i.ToString(); | |
if (-NOT (Test-Path ($AgentDirectory))) { | |
mkdir $AgentDirectory; | |
Set-Location $AgentDirectory; | |
break; | |
} | |
} | |
$AgentZipFilePath = "$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/2.182.1/vsts-agent-win-x64-2.182.1.zip'; | |
if ($DefaultProxy -and (-not $DefaultProxy.IsBypassed($Uri))) { | |
$WebClient.Proxy = New-Object Net.WebProxy($DefaultProxy.GetProxy($Uri).OriginalString, $True); | |
}; | |
Write-Output "Download $Uri to file '$AgentZipFilePath' - Please wait." | |
$Stopwatch.Start(); | |
$WebClient.DownloadFile($Uri, $AgentZipFilePath); | |
$Stopwatch.Stop(); | |
Write-Output "Download $Uri to file '$AgentZipFilePath' - OK ($($Stopwatch.ElapsedMilliseconds) ms)." | |
Write-Output "Extract file '$AgentZipFilePath' to directory '$PWD' - Please wait." | |
$Stopwatch.Restart(); | |
Add-Type -AssemblyName System.IO.Compression.FileSystem; | |
[System.IO.Compression.ZipFile]::ExtractToDirectory($AgentZipFilePath, "$PWD"); | |
$Stopwatch.Stop(); | |
Write-Output "Extract file '$AgentZipFilePath' to directory '$PWD' - OK ($($Stopwatch.ElapsedMilliseconds) ms)." | |
Write-Output "Execute '$PWD\config.cmd'." | |
$Stopwatch.Restart(); | |
.\config.cmd ` | |
--environment ` | |
--environmentname "$EnvironmentName" ` | |
--agent "$env:COMPUTERNAME-$AgentDirectory" ` | |
--runasservice ` | |
--work "_work" ` | |
--url "https://dev.azure.com/$OrganizationName/" ` | |
--projectname "$ProjectName" ` | |
--auth PAT ` | |
--token $PersonalAccessToken; | |
$Stopwatch.Stop(); | |
Write-Output "Execute '$PWD\config.cmd' - OK ($($Stopwatch.ElapsedMilliseconds) ms)." | |
Write-Output "Delete file '$AgentZipFilePath' - Please wait." | |
$Stopwatch.Restart(); | |
Remove-Item $AgentZipFilePath; | |
$Stopwatch.Stop(); | |
Write-Output "Delete file '$AgentZipFilePath' - OK ($($Stopwatch.ElapsedMilliseconds) ms)." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment