Created
August 17, 2020 08:40
-
-
Save stevenvolckaert/aa2a7d20688691771fbe12658006fe64 to your computer and use it in GitHub Desktop.
This file contains 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 | |
<# | |
.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 (steven.volckaert@gmail.com) | |
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 ( | |
[string] $OrganizationName = $(throw "Argument -OrganizationName is required"), | |
[string] $ProjectName = $(throw "Argument -ProjectName is required"), | |
[string] $EnvironmentName = $(throw "Argument -EnvironmentName is required"), | |
[string] $PersonalAccessToken = $(throw "Argument -PersonalAccessToken is required") | |
) | |
# 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.170.1/vsts-agent-win-x64-2.170.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
Hi Steven;
I've made a few updates to your script, could you take a look at https://gist.github.com/celluj34/c2b6961b8d0ae2cecb93a709af737ef4 and let me know if it's something you'd like to incorporate into yours?