【发布时间】: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