简体   繁体   English

如何使用 Azure CLI 获取安装了操作系统的 Azure VMS 列表?

[英]how can i get a list of Azure VMS with operating system installed using Azure CLI?

I run the following command and get a list for one vm which details what operating system is installed, however, is there a way I can do this for all VMS in a subscription rather than one by one and output it to a csv file?我运行以下命令并获取一个 vm 的列表,其中详细说明了安装的操作系统,但是,有没有一种方法可以为订阅中的所有 VMS 执行此操作,而不是一个一个地将其输出到 csv 文件? I need to know which version of windows is installed on all vms我需要知道所有虚拟机上安装了哪个版本的 Windows

az vm show -g $vmresourcegroup -n $vmname --query '[name, storageProfile.imageReference]'

By using the below script you can pull the list of virtual machines under a particular subscription with VirtualMachineName, Operating system & their SKU names通过使用以下脚本,您可以使用 VirtualMachineName、操作系统及其 SKU 名称提取特定订阅下的虚拟机列表

az login

$result = az vm list --subscription <subscriptionName> `
 --query "[].[name,storageProfile.imageReference.offer,storageProfile.imageReference.sku]" 
$json = $result | ConvertFrom-Json

$json | % {
$item = New-Object PSCustomObject
$item | Add-Member -NotePropertyName "vmName" -NotePropertyValue $_.SyncRoot[0]
$item | Add-Member -NotePropertyName "Os" -NotePropertyValue $_.SyncRoot[1]
$item | Add-Member -NotePropertyName "sku" -NotePropertyValue $_.SyncRoot[2]

$item | Export-Csv C:\Users\Desktop\9.csv  -NoTypeInformation  -Append
}

在此处输入图片说明

Regarding the first part of your question:关于你问题的第一部分:

az vm list will return all vms in the current subscription. az vm list将返回当前订阅中的所有az vm list As it returns an array of vms, you also need to adjust your JMESPATH query accordingly:当它返回一组 vm 时,您还需要相应地调整 JMESPATH 查询:

az vm list --query '[].[name, storageProfile.imageReference]'

Regarding the second part of your question:关于你问题的第二部分:

In order to get csv output you will need to flatten the output first, as "imageReference" has several subproperties.为了获得 csv 输出,您需要先展平输出,因为“imageReference”有几个子属性。

I get from the tags in your question that you are running az cli in PowerShell:我从您问题中的标签中得知您正在 PowerShell 中运行 az cli:

 az vm list -o json | 
  ConvertFrom-Json | 
  Select-Object -ExpandProperty StorageProfile -Property Name | 
  Select-Object -ExpandProperty ImageReference -Property Name |
  ConvertTo-Csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM