0

I installed Azure cli on a Windows Server Agent and it works fine in cmd/PowerShell. but when I want to use Azure cli on PowerShell task in Azure Pipeline it's not working.

the error message is: 'az' is not recognized as an internal or external command.

cmd example on agent:

enter image description here

task code sample on Azure Pipeline:

- powershell: |
        echo "*********" | az devops login --organization https://dev.azure.com/*****
  displayName: 'Login on Azure CLI

I check Path environment variable and even update variables in pipeline to ensure everything fine but nothing changes.

  • There is an explicit Azure CLI task in Azure Pipelines, which you could use. Also, https://stackoverflow.com/questions/66509534/how-to-run-azure-cli-tasks-from-an-azure-devops-pipeline-on-a-self-hosted-window may be relevant. Note restarting the agent service/machine is required? – Andrew McClement May 31 '23 at 10:25
  • Thanks for your response. I test the restart solution and it's not the answer. about AzureCLL task I try with version 2 and it's not working either. I will try version 1 and come back. – Milad Karimifard May 31 '23 at 10:51

1 Answers1

1

I have followed the below steps to execute az command from the azure pipeline. Check if you have followed all these steps and able to ressolve the issue.

Firstly I used the below command to install Azure CLI on a windows machine.

$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri https://azcliprod.blob.core.windows.net/msi/azure-cli-2.49.0.msi -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; Remove-Item .\AzureCLI.msi

Next I installed the devops agent on VM with following configuration.

enter image description here

When configuring the pipeline, ensure you select the correct agent pool. In my case it is default. To test, add a powershell task task pipeline with command az login --service-principal -u $(appid) -p $(passwd) --tenant $(tenant). Run the pipeline and verify the output. enter image description here

If you still encounter issues, check the environment variable path to verify if the AZ CLI path is included. You can run echo $env:PATH from the pipeline to check. enter image description here If the AZ CLI path is not present, add it to the system variables and verify again.

HowAreYou
  • 605
  • 2
  • 6