0

I am at a loss as to why I cannot run a basic az devops command in an Azure Pipeline using the Classic template.

So basically, I have two Powershell tasks defined for my Classic pipeline and these are:

enter image description here

Task 1: Login to my Azure DevOps organisation using the following commands:

$mytoken = "My_PersonalAccessToken"
echo $mytoken "|" az devops login --organization https://dev.azure.com/OrganisationName

Task 2: List My Azure DevOps Projects

az devops project list

Task 1 works perfectly and login is confirmed as successful.

Task 2 however fails and displays the below error in the pipeline output:

 Before you can run Azure DevOps commands, you need to run the login command(az login if using AAD/MSA identity else az devops login if using PAT token) to setup credentials.  Please see https://aka.ms/azure-devops-cli-auth for more information.

enter image description here

What could I be doing wrong? Would really appreciate some help, although I must emphasise again that my pipeline uses the Classic template and any proposed solution must be tailored for that.

Mind you, when I run these very same commands in my Powershell ISE editor, both run perfectly and I get all my Azure DevOps projects listed.

hitman126
  • 699
  • 1
  • 12
  • 43

2 Answers2

1

I see that piping PAT on StdIn to az devops login is failing. However, an even better approach IMHO would be to set the AZURE_DEVOPS_EXT_PAT environment variable, and not use az devops login at all. :)

Pipeline variables:

enter image description here

Tasks:

enter image description here

Check Sign in with a personal access token (PAT) for more details.

Bhargavi Annadevara
  • 4,923
  • 2
  • 13
  • 30
0

Maybe there is a product issue recently, as the Azure CLI should work, it worked for me last month, see here.

For now, if you want to list the projects, you could call the REST API Projects - List directly in PowerShell task.

$MyPat = '<PAT>'
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"))
$URL = 'https://dev.azure.com/orgname/_apis/projects?api-version=6.0'
$header = @{
    'Authorization' = 'Basic ' + $B64Pat
    'Content-Type' = 'application/json'
}

Invoke-RestMethod -Method Get -Uri $URL -Headers $header| ConvertTo-Json

enter image description here

Joy Wang
  • 39,905
  • 3
  • 30
  • 54