I need to select only one value from the out-table.
The code is:
az vm list-ip-addresses --name VMname --resource-group GroupName --out table
I get the result as below
VirtualMachine PublicIPAddresses PrivateIPAddresses
---------------- ------------------- --------------------
VMName XX.XX.XX.XX XX.XX.XX.XX
I want to select only the PublicIPAddresses
value and extract it to csv.
TIA
2
Answers
Output format option
--output table
(-o table
) is meant for human-friendly (tabular) display rather than for programmatic processing.It’s better to use an output format based on structured text, such as JSON.
Indeed, JSON is the default output format, so you can simply omit your
--out table
argument and useConvertFrom-Json
to parse the output and extract the values of interest:The structure of the JSON data returned is assumed to be the same as in this question.
Note that you can streamline the operation by using the
--query
parameter to perform a JMESPath query at the source, so that only the values of interest are directly returned:Note:
--output tsv
, in which case you don’t need theConvertFrom-Json
call – see below.According to this answer, the following shorter alternative that uses the
az vm show
sub-command – rather thanaz vm list-ip-addresses
– works too:Note:
The above uses a different structured text format, TSV (tab-separated values), with
--output tsv
, which in this simple case obviates the need for post-processing on the PowerShell side:Extracting public IPs for multiple VMs (all VMs in a resource group):
You can add the option -d which will pick up the default defined public IP address: