microsoft / VSTSAgent.PowerShell

Tools for managing and automating your Azure DevOps Agents.
MIT License
29 stars 23 forks source link

Install VSTS Agent using PAT #20

Closed Ankita-Chaudhari closed 4 years ago

Ankita-Chaudhari commented 4 years ago

Hi,

I need to install a VSTS agent inside an Azure VM using a DSC configuration file. As my client's requirement, they want to make use of PAT rather than passing the Account credentials. Is there any way by which I can use this module to install VSTS agent using PAT? If yes, could you please share with me an example of how to use it.

Thanks in advance.

jwittner commented 4 years ago

Hi! Thanks for the question! Currently the AccountCredentials are expected to include a PAT as the password and the username is ignored so you should be able to do what you want right now. =)

FYI, we pass them as a Credential through DSC as DSC doesn't support SecureString by itself. You can see here where the Password is used as a PAT for the call to Install-VSTSAgent.

Good luck and let us know if you run into any issues!

Ankita-Chaudhari commented 4 years ago

I cannot see the example file in the link provided by you https://github.com/microsoft/VSTSAgent.PowerShell/issues/Install-VSTSAgent.

jwittner commented 4 years ago

Whoops, wrong link! I've adjusted above, but here for reference too.

Ankita-Chaudhari commented 4 years ago

Thank you for the link!

This is my configuration file Configuration xVSTSAgent_Install { param (
[parameter(Mandatory = $true)] [PSCredential]$AccountCredential ) Import-DscResource -ModuleName VSTSAgent

Node 'localhost' {

    xVSTSAgent VSTSAgent {
        Name              = 'Agent01'
        ServerUrl         = 'https://XXXX.visualstudio.com'
        AccountCredential = $AccountCredential
        AgentDirectory    = 'C:\Agent'
        Work              = 'C:\VSTSAgentsWork\Agent01'
        Ensure            = 'Present'
    }
}

}

When I compile this configuration it asks for account creds, to that input I am passing the PAT token. It fails with the following error : "Credential with name 'PAT Token' not found for account '0d059b1b-0d4d-4e56-8b5a-dbc80da1a5cd'."

image

Could you please help me with an example file on how to pass the PAT token?

Thanks in advance!

jwittner commented 4 years ago

In Azure Automation you have to create a Credential in the Automation account. Then you can pass the name of that credential to the compilation job - it'll replace it with the actual PSCredential behind the scenes for you.