Skip to content

Instantly share code, notes, and snippets.

@rfennell

rfennell/BuildAgent.ps1

Last active Jan 6, 2020
Embed
What would you like to do?
A Lability definition to create an Azure DevOps Agent
configuration BuildAgent {
param (
[ValidateNotNull()]
[System.Management.Automation.PSCredential]$Credential,
[string]$EnvPrefix,
[Int]$RetryCount = 100,
[Int]$RetryIntervalSec = 30
)
Import-DscResource -ModuleName xPSDesiredStateConfiguration -ModuleVersion 8.0.0.0
Import-DscResource -ModuleName xStorage -ModuleVersion 2.9.0.0
# a Black Marble modules that contain our specific functions (alteratives listed)
Import-DscResource -ModuleName bmDscResources -ModuleVersion 1.7.6
node $AllNodes.Where({$_.Role -eq 'BuildAgent'}).NodeName {
$LocalAdminCredential = New-Object System.Management.Automation.PSCredential ("$($node.NodeName)\$($Credential.UserName)", $Credential.Password)
LocalConfigurationManager {
RebootNodeIfNeeded = $true
AllowModuleOverwrite = $true
ConfigurationMode = 'ApplyOnly'
CertificateID = $Node.Thumbprint
DebugMode = 'All'
}
# make sure Windows manages paging
Script Paging {
PsDscRunAsCredential = $LocalAdminCredential
GetScript = { @{} }
TestScript = {
$computersys = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges;
return $computersys.AutomaticManagedPagefile
}
SetScript = {
$computersys = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges;
$computersys.AutomaticManagedPagefile = $True;
$computersys.Put();
}
}
# make sure the NIC has the name we expect
Script NameNic {
PsDscRunAsCredential = $LocalAdminCredential
GetScript = { @{} }
TestScript = {
$adapters = get-netadapter
$adaptor = $adapters | Where {$_.name -eq 'Ethernet'}
$adaptor -ne $null
}
SetScript = {
$adapter = get-netadapter | where {$_.Name.startswith("Ethernet") }
if (!$adapter.count){
if ($adapter.Name -ne "Ethernet") {
rename-netadapter -name $adapter.name -newname "Ethernet"
}
}
}
}
# A slighly customised network config. Could be replaced by xIPAddress, xDnsServerAddress etc.
bmNetwork NetworkConfiguration {
DependsOn = "[script]NameNic"
IsAzure = $false
IPAddress = $Node.IPAddress
PrefixLength = $Node.PrefixLength
InterfaceAlias = $Node.InterfaceAlias
AddressFamily = $Node.AddressFamily
DefaultGateway = $Node.DefaultGateway
DnsAddress = $Node.DnsAddress
}
# Add an extra disk
xWaitforDisk Disk1 {
DiskNumber = 1
RetryIntervalSec = $RetryIntervalSec
RetryCount = $RetryCount
}
xDisk AgentDisk {
DiskNumber = 1
DriveLetter = 'E'
}
# Set the Window supdate keys, a wrapper for registry settings
bmWindowsUpdate SetWindowsUpdateKeys {
}
# Install agent
Script AddBMPackages {
PsDscRunAsCredential = $LocalAdminCredential
GetScript = { @{} }
TestScript = {
if (Get-PSRepository | Where-Object SourceLocation -eq 'https://packages.blackmarble.co.uk/nuget/PowerShell') {
return $true
}
else {
return $false
}
}
SetScript = {
$RegisterSplat = @{
Name = 'BM'
SourceLocation = 'https://packages.blackmarble.co.uk/nuget/PowerShell'
PublishLocation = 'https://packages.blackmarble.co.uk/nuget/PowerShell'
InstallationPolicy = 'Trusted'
}
Register-PSRepository @RegisterSplat
}
}
$AgentArguments = "--unattended --url $($ConfigurationData.AzureDevOps.Url) --auth pat --token $($ConfigurationData.AzureDevOps.Pat) --pool $($ConfigurationData.AzureDevOps.Pool) --agent $EnvPrefix$($Node.Nodename)-$(Get-Random -SetSeed (Get-Date).Second -Minimum 1 -Maximum 150) --work _work --runAsService --WindowsLogonAccount $($Credential.Username) --WindowsLogonPassword $($Credential.GetNetworkCredential().Password)"
Script InstallAgent {
GetScript = { @{} }
TestScript = {
Test-Path -Path "E:\Agent\.Agent"
}
SetScript = {
Expand-Archive -Path C:\Bootstrap\Agent.zip -DestinationPath E:\Agent -Force
Start-Process -FilePath E:\Agent\config.cmd -ArgumentList $Using:AgentArguments -Wait -RedirectStandardOutput C:\Bootstrap\Output.txt
Get-Content -Path C:\Bootstrap\output.txt | Foreach-Object { Write-Verbose -Message $_ }
}
DependsOn = '[xDisk]AgentDisk'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.