Agents/Get-AzureDevOpsAgentInstallParameters.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<#
.SYNOPSIS
Generate a set of parameter for CI Agent or Deployment Agent
.DESCRIPTION
Generate a set of parameter see also: https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/v2-windows?view=azure-devops
.PARAMETER poolName
Name of the Pool
.PARAMETER deploymentGroup
Name of the Deployment Group
.PARAMETER projectName
Name of the Azure DevOps Project
.PARAMETER deploymentGroupTags
Optional Tags for the Agent, if it is used as a deployment group agent
.PARAMETER agentName
Name of the agent
.PARAMETER organizationUri
Uri of your Azure DevOps organization.
.PARAMETER vstsToken
VSTS-Token to use PAT authentication.
.PARAMETER runasservice
Add the agent as service.
.PARAMETER credential
Optional Credentials for Service Registration
.EXAMPLE
Install Agent at a Deployment Pool:
Get-AzureDevOpsAgentInstallParameters -deploymentGroup $poolName -projectName $projectName -organizationUri $devOpsURL -vstsToken $vstsToken -agentName $containerName -credential $credential -runasservice
 
Install Agent at an Agent Pool:
Get-AzureDevOpsAgentInstallParameters -poolName $pool.name -organizationUri $devOpsURL -vstsToken $vstsToken -agentName $containerName -credential $credential -runasservice
.NOTES
General notes
#>


function Get-AzureDevOpsAgentInstallParameters {
    param (
        # -- Agent Pool -------
        [string]$poolName         = "",
        # -- Deployment Pool --
        [string]$deploymentGroup  = "",
        [string]$projectName      = "",
        [string[]]$deploymentGroupTags = $null,
        # ---------------------
        [string]$agentName        = "",
        [string]$organizationUri,
        [string]$vstsToken,
        [switch]$runasservice,
        [PSCredential]$credential = $null
    )
    Process {
        $parameters = @(
            "--unattended",
            "--agent ""$agentName""",
            "--url ""$organizationUri""",
            "--auth PAT",
            "--token $vstsToken",
            "--acceptTeeEula",
            "--noRestart",
            "--replace"
            )
        if ($runasservice) {
            $parameters += @("--runasservice")
        }
        if ("$poolName" -ne "") {
            $parameters += @("--pool ""$poolName""")
        } elseif ("$deploymentGroup" -ne "" -and "$projectName" -ne "") {
            $parameters += @(
                "--deploymentgroup", 
                "--deploymentgroupname ""$deploymentGroup""", 
                "--work ""_work""",
                "--projectname ""$projectName"""
            )
            if ($null -ne $deploymentGroupTags) {
                $parameters += @(
                    "--addDeploymentGroupTags",
                    "--deploymentGroupTags ""$([string]::Join(",", $deploymentGroupTags))"""
                )
            }
        } else {
            Write-Warning "Incomplete parameters for Pool (poolName) or Deployment Group (deploymentGroup and projectName)"
        }

        if ($null -ne $credential) {
            $parameters += @(
                "--windowsLogonAccount .\$($credential.UserName)",
                "--windowsLogonPassword $($Credential.GetNetworkCredential().Password)"
            )        
        }
        
        return $([string]::Join(" ", $parameters))
    }
}
Set-Alias -Name Get-AgentInstallParams -Value Get-AzureDevOpsAgentInstallParameters
Export-ModuleMember -Function Get-AzureDevOpsAgentInstallParameters -Alias Get-AgentInstallParams