0
votes

I have PowerShell scripts that I run to kick off a build in Azure DevOps as well as doing lots of things in Azure DevOps using the Rest API. I am currently using the token that is converted to Base64 using basic in the header to authenticate. If there a way of using -Credentials (Get-Credentials) with the token instead of a base64 header encoded token when using Invoke-RestMethod? Below is a sample for connecting with the Base64 token and Basic.

Sample script that lists Projects:

$token = "##############################################" 
$UriOrg = "https://dev.azure.com/myADO/"
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)")) }
$uriProcess = $UriOrg + "_apis/process/processes?api-version=5.1"
Invoke-RestMethod -Uri $uriProcess -Method get -Headers $AzureDevOpsAuthenicationHeader
1

1 Answers

0
votes

Although Get-Credential is designed to get a credential using a username and password, you can of course also use it to have someone enter the token..

Something like

$cred = Get-Credential -Message 'Please enter the AzureDevops Token in the Password field' -UserName 'AzureDevops'
if ($cred) {
    $token      = $cred.GetNetworkCredential().Password
    $authHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)")) }
    $UriOrg     = 'https://dev.azure.com/myADO/'
    $uriProcess = $UriOrg + "_apis/process/processes?api-version=5.1"
    Invoke-RestMethod -Uri $uriProcess -Method Get -Headers $authHeader
}

Another option would be to create your own form where it is also possible to enter the Uri for the organization, because Get-Credential does not accept usernames like https://dev.azure.com/myADO/