itye1970
itye1970

Reputation: 1995

how can i get the value of an azure cli list command into a variable?

When i run the following command in azure, I want to place the value of the "name" in a variable like $ipconfigname. The output lists out many other values but I only need this one value "name" in the variable, how can i accomplish this?

az network nic ip-config list -g rg01 --nic-name vmnic01

Upvotes: 2

Views: 3833

Answers (2)

Charles Xu
Charles Xu

Reputation: 31434

Well, when you use the list command, it means the output must be a list no matter how many members it has. So as you want, the variable $ipconfigname actually is a list of the config name. You can use the Azure CLI command to set the variable like below only with the name list:

$ipconfigNames = az network nic ip-config list -g rg01 --nic-name vmnic01 --query "[].name" -o tsv

Upvotes: 2

Nick Graham
Nick Graham

Reputation: 1469

The az cli outputs JSON so if you run your command from a PowerShell prompt you can use ConvertFrom-Json to create an object from the output and then get the property you need

$IpConfig = az network nic ip-config list -g rg01 --nic-name vmnic01 | ConvertFrom-Json
$IpConfigName = $IpConfig.name

Upvotes: 4

Related Questions