1

I am trying to get a table output with numbers in the Azure CLI which gives this as a output

Number      Location     Name
----------  -----------  -------------
1           somewhere    ResourceGroup1
2           somewhere    ResourceGroup2

The code I have right now is

az group list --query '[].{location:location, name:name}'

The output I'm getting right now is

Location     Name
----------  ---------------
somewhere    ResourceGroup1
somewhere    ResourceGroup2

My end goal is that if you choose the number 1 you select the name so I can use that later in the script

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Brynn
  • 97
  • 2
  • 13

3 Answers3

2

For your issue, there is no Azure CLI command can achieve it. But you can use a script to let it come true. For example, you can use a shell script:

#!/bin/bash

az group list --query '[].{location: location, name: name}' -o table >> output.txt

# This command just add the line number inside the file, it's optional.
cat -n output.txt >> result.txt

# you can just get the group name with a specific line, the same result with output.txt
awk '{if (NR == line) print $3}' result.txt 

Hope this will be helpful.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
1

From what i understand you are trying to create variable to use later from output. You do not need to put it in a table first. Using same example you have you could do something like below;

gpname="$(az group list --query [0].name --output tsv)"

az group show -n $gpname

Imgur

Good Luck.....

Information in Comments::

What you are looking for is more Linux than Azure. I am not a Linux CLI expert but her is a basic script that you can build on.

#!/bin/bash

gpnames="$(az group list --query [].name --output tsv)"

PS3='Select A number: '

select gpname in $gpnames

do

az group show -n $gpname

Done

Imgur

Hope this helps......

Hannel
  • 1,656
  • 3
  • 10
  • 17
  • I am indeed trying to make a variable from the resource group name but to do that you need to be able to select on first because in my case i will be working with loads of resource groups – Brynn Nov 01 '18 at 09:50
  • 1
    What you are looking for is more Linux than Azure. I am not a Linux CLI expert but her is a basic script that you can build on. `#!/bin/bash` `gpnames="$(az group list --query [].name --output tsv)"` `PS3='Select A number: '` `select gpname in $gpnames` `do` `az group show -n $gpname` `Done` [Imgur](https://i.imgur.com/mlUPZ4E.png) Hope this helps...... – Hannel Nov 01 '18 at 18:24
  • This is indeed the output i need! Unfortunatly i dont avhe it working on my machine...yet. If this is indeed the solution i need ill send the powershell code and accept your anwser! – Brynn Nov 02 '18 at 09:08
1

you can use contains expression (jmespath) in the filter to filter the results:

filter=resource_group_name
filterExpression="[?contains(name, '$filter')].name"
az group list --query "$filterExpression" -o tsv

which is a much better way compared to already present answers.

more reading:
http://jmespath.org/specification.html#filterexpressions
http://jmespath.org/specification.html#built-in-functions

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • I think i get what you are trying to do here but somehow i cant get this one working on my machine – Brynn Nov 01 '18 at 09:53
  • just try azure cloud shell. this will only work on linux, not on windows. on windows you can use powershell – 4c74356b41 Nov 01 '18 at 09:59
  • Unfortunatly i dont get any results back in the azure cloud shell – Brynn Nov 01 '18 at 10:27
  • well, change the filter? change your working subscription? – 4c74356b41 Nov 01 '18 at 10:29
  • i cant change the subscription and it seems like the filter terms isnt recognized at all – Brynn Nov 01 '18 at 10:33
  • 1
    do you get any output from `az group list` in cloud shell? if you do - change the filter to match the name of one of the resource groups and it should work – 4c74356b41 Nov 01 '18 at 10:43
  • This worked unfortunatly this isnt the anwser i was looking for – Brynn Nov 01 '18 at 10:57
  • well, this is exactly what you are trying to do, filter the results – 4c74356b41 Nov 01 '18 at 11:05
  • Hi, After rereading your answer and letting a colleague look at it i changed a few things and now it works and it is what i was searching for! the only thing i am trying to fix right now is that it is case sensitive – Brynn Nov 02 '18 at 10:42
  • zthis is the code i am using right now `az group list --query '[].{name:name}' --output table` `$filter = Read-Host -Prompt "Please filter to find the correct resource group"` `az group list --query "[?contains(name, '$filter')].name" --output table` – Brynn Nov 02 '18 at 10:43
  • 1
    well, this was an example you are supposed to build upon :) best of luck – 4c74356b41 Nov 02 '18 at 10:59