Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Azure CLI within Azure Powershell Task

I want to create a Powershell script which executes some AzureRm... commands and follows those up with some Az commands. Reason being that some commands are only available via Az.

When trying to execute these scripts in a release pipeline, the script always fails with the following error:

ERROR: Please run 'az login' to setup account.

Executing the Az commands in a Azure CLI task work as expected, because Az Login is executed by the task.

I don't want to pass the secret required to login to the script if at all possible. I would rather fall back to separating the scripts into two steps in the pipeline.

Is it possible to use the Azcommands within a Azure Powershell task without passing the secrets manually?

Minimal example:

  • Create a new release pipeline
  • Add a task Azure PowerShell
  • Use inline script
  • As script, execute az account show
like image 775
Alex AIT Avatar asked Oct 22 '18 07:10

Alex AIT


People also ask

Can I run Azure CLI commands in PowerShell?

You can now run the Azure CLI with the az command from either Windows Command Prompt or PowerShell.

Which task should be used to run a shell or batch script containing Azure CLI commands against an azure subscription?

Azure CLI task. Use this task to run a shell or batch script containing Azure CLI commands against an Azure subscription. This task is used to run Azure CLI commands on cross-platform agents running on Linux, macOS, or Windows operating systems.


1 Answers

The short term solution I already had in place was passing the ServicePrincipal information into the powershell script and executing az login manually (same as Bevan's answer below).

My long term solution was to replace all Azure CLI calls with "Az Powershell" commands. Luckily, most commands are available by now.

A couple of commands don't have an equivalent commandlet. But if they are available via ARM, you can figure out an alternative command with Powershell.

Many of them involve using New-AzResource/New-AzureRmResource or Invoke-AzResourceAction/Invoke-AzureRmResourceAction

  • How to create a Linux AppService Plan with New-AzAppServicePlan?
  • Manage Azure Cosmos DB SQL API resources using PowerShell
# AzureCLI
az cosmosdb list-keys
# Powershell:
$keys = Invoke-AzResourceAction -Action listKeys `
    -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2015-04-08" `
    -ResourceGroupName $resourceGroupName -Name $accountName
like image 160
Alex AIT Avatar answered Oct 12 '22 00:10

Alex AIT