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

标签 azure powershell azure-cli

我正在尝试从订阅中获取虚拟机,这样我就可以在 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 文件。 enter image description here

因此,如果您想根据需要创建csv文件,请引用以下脚本

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

enter image description here

关于azure - 如何在powershell/azure中将格式输出到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59125613/

相关文章:

azure - 将文件从 URL 复制到 Azure BLOB

azure - 如何解释Azure VM的instanceView的状态?

powershell - 将 stdout 和 stderr 重定向到单独的文件时抑制 NativeCommandError 输出

azure - 如何在azure devops管道中执行azure cli命令

azure - 在从模板部署的突触管道中分配sparkpoolname时出现问题

asp.net - 如何在环境变量中覆盖 appsettings.json 中基于数组的设置?

Azure Active Directory 应用程序所需的资源访问限制

javascript - 等效于 Powershell 中的 Javascript encodeURI?

c# - Powershell Mysql 错误

Azure 函数未使用 Azure CLI 进行部署