如何将格式输出到文件中powershell/azure

How to output the format into a file in powershell/azure

我正在尝试从订阅中获取我的虚拟机,这样我就可以在 python 中创建一些命令来从结构中提取不同的东西,但要做到这一点,我需要文件是最简单的方法以 CSV 格式保存,每次我尝试使用 powershell 导出内容时,它都不起作用。

screenshot of powershell results

你能告诉我这个命令应该怎么写吗?

az vm list --query "[].[resourceGroup,name,storageProfile.osDisk.osType]" -o table | Export-Csv D:\vmlist.csv -Delimiter ";" -Force -NoTypeInformation

根据我的研究,当我们使用命令 "Export-Csv" 将输出保存为 CSV 文件时,它会将输出视为一个对象,并将该对象的属性作为列存储到 CSV 文件中。现在命令 az vm list --query "[].[resourceGroup,name,storageProfile.osDisk.osType]" -o table 的输出是 String。所以我们无法根据您的需要创建 CSV 文件。

所以如果你想根据需要创建csv文件,请参考下面的脚本

$result = az vm list --query "[].{resource:resourceGroup, name:name}" -o json | ConvertFrom-Json 
$result | export-csv -Path e:\test.csv  -NoTypeInformation