【问题标题】:PowerShell to List all Azure VM data disk size confusionPowerShell 列出所有 Azure VM 数据磁盘大小混乱
【发布时间】:2022-08-20 16:13:10
【问题描述】:

基于 Microsoft Azure 的这篇知识库文章:https://docs.microsoft.com/en-us/azure/virtual-machines/windows/faq#how-much-storage-can-i-use-with-a-virtual-machine-

我们可以将多个数据磁盘附加到 Azure VM。

但是,我们如何在 PowerShell 查询中动态显示它?

Get-AzVM | ForEach-Object {
    $size = $_.HardwareProfile.VmSize
    $vmsize = Get-AzVMSize -vmname $_.Name -ResourceGroupName $_.ResourceGroupName | Where-Object { $_.name -eq $size }
    $nic = $_.NetworkProfile.NetworkInterfaces.id.split(\'/\') | Select-Object -Last 1

    # Implicitly outputs an object with the given properties
    [pscustomobject]@{
        Location   = $_.Location
        Name       = $_.Name
        osdiskingb = $_.StorageProfile.OsDisk.DiskSizeGB
        data1diskingb = ($_.StorageProfile.DataDisks[0].DiskSizeGB)
        data2diskingb = ($_.StorageProfile.DataDisks[1].DiskSizeGB)
        data3diskingb = ($_.StorageProfile.DataDisks[2].DiskSizeGB)
        memory     = [Math]::Round(($vmsize.MemoryInMB)/1024, 1)
        cpu        = $vmsize.NumberOfCores
        nic        = $nic
        ip         = (Get-AzNetworkInterface -Name $nic).ipconfigurations.privateipaddress
        VMTags     = $_.Tags
        VMStatus   = $_.StatusCode
        State      = $_.ProvisioningState
    } 
} | ogv

使用 Zett42 创建的上述静态 PowerShell 查询来自:Optimize PowerShell code to avoid calling the cmdlet multiple times inside calculated properties? 我只能手动复制粘贴/重复以下这一行:

data1diskingb = ($_.StorageProfile.DataDisks[0].DiskSizeGB)
data2diskingb = ($_.StorageProfile.DataDisks[1].DiskSizeGB)
data3diskingb = ($_.StorageProfile.DataDisks[2].DiskSizeGB)

如果 Azure VM 有超过 5-10 个数据磁盘,我必须相应地复制和粘贴多次。

所以我需要一些帮助来更新脚本,以便它可以动态显示所有数据磁盘。

先感谢您。

    标签: azure powershell azure-powershell


    【解决方案1】:

    使用下面的 PowerShell 命令打印与 VM 关联的所有数据磁盘。

    (Get-AzVM -Name <VM-Name> -ResourceGroupName <RG-Name>).StorageProfile.DataDisks
    

    如果您只想打印磁盘名称和大小,请使用 foreach 语句。

    $disks = (Get-AzVM -Name <VM-Name> -ResourceGroupName <RG-Name>).StorageProfile.DataDisks
    
    foreach ($disk in $disks){
        Write-Output $i.Name, $i.DiskSizeGB
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-30
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2021-04-11
      • 2019-11-01
      相关资源
      最近更新 更多