0

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

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
SuttyHoo
  • 130
  • 8

1 Answers1

0

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

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • Thanks Joy. Very helpful. It is annoying that these subtle differences exist. I changed my query to use quality rather than 'contains' as below - and there is no issue when running locally: az storage account list --query "[?name=='terraformXX'].name" --output tsv To me, this seems seem like a bug to me in that it does not handle syntax for 'contains'. – SuttyHoo May 19 '21 at 07:57
  • @SuttyHoo Well, but `==` is different from `contains`, simply use Git bash, I think it should be a better way. – Joy Wang May 19 '21 at 08:03