0

I'm working with azure CLI to script out a storage upgrade as well as add a policy, all in a python script. However, when I run the script I'm getting some expected and some very NOT expected output.

What I'm using so far:

from azure.cli.core import get_default_cli

def az_cli (args_str):
    args = args_str.split()
    cli = get_default_cli()
    cli.invoke(args)
    if cli.result.result:
        return cli.result.result
    elif cli.result.error:
        raise cli.result.error
    return True

sas = az_cli("storage account list --query [].{Name:name,ResourceGroup:resourceGroup,Kind:kind}")

print(sas)

By using this SO article as reference I'm pretty easily making Azure CLI calls, however my output is the following:

[
{
    "Kind": "StorageV2",
    "Name": "TestStorageName",
    "ResourceGroup": "my_test_RG"
  },
  {
    "Kind": "Storage",
    "Name": "TestStorageName2",
    "ResourceGroup": "my_test_RG_2"
  }
]
[OrderedDict([('Name', 'TestStorageName'), ('ResourceGroup', 'my_test_RG'), ('Kind', 'StorageV2')]), OrderedDict([('Name', 'TestStorageName2'), ('ResourceGroup', 'my_test_RG_2'), ('Kind', 'Storage')])]

I appear to be getting 2 arrays back, and I'm unsure of what the cause is. I'm assuming it has to do with my using the --query to narrow down the output I get back, but I'm at a loss as to why it then repeats itself. Expected result would just be the first part that's in json format. I have also tried with tsv output as well with the same results. I appreciate any insight!

MattsM
  • 3
  • 2
  • As I keep working on this, I changed the call to: sas = az_cli("storage account list --query [].[name,resoureGroup,kind] -o tsv") however this still adds an extra array at the end, though it's just a single line and repeats the first storage account in the list, just in an array format. Still no idea why that is. – MattsM Jul 15 '21 at 21:22
  • You are printing the `sas` object. The object's ToString method determines what is displayed. Instead, print the property of the object. – John Hanley Jul 15 '21 at 22:31

0 Answers0