0
votes

az command to list storage accounts using --query runs in Azure Cloud Shell but fails when run locally.

Any idea why?

I am using:

  • Windows 10PC

  • Python version 3.9.1

  • AZ CLI: 2.23.0

Steps:

  • Run PowerShell as Admin

  • Login successfully with az login

  • Run following commands:

Works locally (Storage / no --query):

az storage account list  --output tsv

Works locally (Container / with --query)

az container list --query "[?contains(name, 'mycontainer2')]" --output tsv

Fails locally / Works in Cloud Shell (Storage/ with --query):

az storage account list --query "[?contains(name,'terraformXX')].name" --output tsv

Exception:

].name was unexpected at this time.

C:\WINDOWS\system32> "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\..\python.exe" -IBm azure.cli storage account list --query [?contains(name,'terraformXX')].name --output tsv

The error suggests that some escape character is required. No luck finding a solution with escape characters + it works in Cloud Shell, so I think might be a misleading message.

Thanks in advance

1

1 Answers

0
votes

When running Azure CLI in PowerShell, there always some escape issues, when running it in Azure Cloud Shell, even if you choose PowerShell instead of Bash, it will work, as Azure Cloud Shell runs on a Linux machine Ubuntu 16.04 LTS, mentioned here.

To solve the issue, finding a solution with escape characters every time is not a long-term plan, I recommend you to use Git Bash in Windows, just run the command in it, then will work fine.

enter image description here

Besides, if your environment is Windows, you can use azure powershell directly, make sure you have installed the Az module, then use the command below.

Connect-AzAccount
(Get-AzStorageAccount | Where-Object {$_.StorageAccountName -like '*test*'}).StorageAccountName

enter image description here