Files/Program Files/WindowsPowerShell/Modules/HyperVLabClient/1.0.0.0/Functions/Install-LabVMTfsAgent.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
function Install-LabVMTfsAgent {
    param (
        [string]$ServerUrl = $script:configuration.TfsServerUrl,
        [PSCredential]$ServerCredential = $script:domainCredential,
        [string]$AgentName = $script:configuration.AgentName,
        [string]$AgentFolder = $script:configuration.AgentFolder,
        [string]$WorkFolder = $script:configuration.WorkFolder,
        [PSCredential]$AgentCredential,
        [string]$PoolName = $script:configuration.PoolName,
        [bool]$Force = $true
    )

    if (-not $AgentCredential -and $script:configuration.AgentUserName -and $script:configuration.AgentPassword) {
        $AgentCredential = New-Object -TypeName PSCredential -ArgumentList $script:configuration.AgentUserName,(ConvertTo-SecureString $script:configuration.AgentPassword -AsPlainText -Force)
    }

    Uninstall-LabVMTfsAgent -TfsServerUrl $TfsServerUrl -AgentName $AgentName -AgentFolder $AgentFolder -WorkFolder $WorkFolder

    try {
        # download agent-zip from TFS-instance
        $downloadPath = [System.IO.Path]::GetTempFileName()
        $url = "$($ServerUrl.TrimEnd('/'))/_apis/distributedtask/packages/agent"
        if ($ServerCredential) {
            Invoke-WebRequest -Uri $url -OutFile $downloadPath -Credential $ServerCredential
        }
        else {
            Invoke-WebRequest -Uri $url -OutFile $downloadPath
        }

        # unzip the agent-zip
        Add-Type -AssemblyName System.IO.Compression.FileSystem
        if (Test-Path $AgentFolder -PathType Container) {
            Remove-Item $AgentFolder -Recurse -Force
        }
        [System.IO.Compression.ZipFile]::ExtractToDirectory($downloadPath, $AgentFolder)

        # configure the agent (https://www.visualstudio.com/docs/build/agents/windows)
        $configCommand = "$(Join-Path -Path $AgentFolder -ChildPath 'Agent\VsoAgent.exe') /Configure /NoPrompt"
        $configCommand += ' /RunningAsService'
        $configCommand += ' /Force'
        $configCommand += " /ServerUrl:$ServerUrl"
        $configCommand += " /Name:$AgentName"
        $configCommand += " /PoolName:$PoolName"
        if ($WorkFolder) {
            $configCommand += " /WorkFolder:$WorkFolder"
        }
        if ($AgentCredential) {
            $agentPassword = $AgentCredential.GetNetworkCredential().Password
            $configCommand += " /WindowsServiceLogonAccount:$($AgentCredential.UserName)"
            $configCommand += " /WindowsServiceLogonPassword:$AgentPassword"
        }

        Write-Host "Configuring TFS Agent with the following command: '$(if ($agentPassword) { $configCommand.Replace($agentPassword, '*******') } else { $configCommand })'"
        Invoke-Expression -Command $configCommand
    }
    finally {
        # clean up (delete downloaded agent-zip)
        if (Test-Path -Path $downloadPath) {
            Remove-Item -Path $downloadPath -Force
        }
    }
}