我正在尝试从订阅中获取虚拟机,这样我就可以在 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
关于azure - 如何在powershell/azure中将格式输出到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59125613/