How to export az network public-ip list output to a csv?

2.6k Views Asked by At

How do I go about exporting the output to a csv? Thanks in advance

az network public-ip list --query "[].{name: name, address: ipAddress}"

Here's the output:

[
  { address: "23.101.140.39", name: foo},
  { address: "23.101.140.38", name: bar},
]

I tried az network public-ip list --query "[].{name: name, address: ipAddress}" | export-csv -path "c:\ips.csv" but this outputs some metadata instead of actual data. Is there a way to unwind that array of documents using JMESPath query? That might fix the issue I am facing.

1

There are 1 best solutions below

2
Nancy On BEST ANSWER

The Export-Csv is Microsoft.PowerShell.Utility, it converts objects into a series of comma-separated value (CSV) strings. Since you could accept the PowerShell commands, you can easily achieve your requirements with Az PowerShell Get-AzPublicIpAddress. It's not recommended to mix the az CLI commands with PowerShell commands.

Get-AzPublicIpAddress | Select-Object Name,IpAddress | Export-Csv -Path "c:\ips.csv" -NoTypeInformation

enter image description here

Edit

For az cli, you can use it like this with PowerShell,

((az network public-ip list --query "[].{name: name, address: ipAddress}") | ConvertFrom-Json) |  Export-Csv -path "c:\ips.csv" -NoTypeInformation