[英]PowerShell to List all Azure VM data disk size confusion
基於 Microsoft Azure 的這篇知識庫文章: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/faq#how-much-storage-can-i-use-with-a-virtual -機器-
我們可以將多個數據磁盤附加到 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 創建的上述 static PowerShell 查詢來自: 優化 PowerShell 代碼以避免在計算屬性中多次調用 cmdlet? 我只能在下面手動復制粘貼/重復這一行:
data1diskingb = ($_.StorageProfile.DataDisks[0].DiskSizeGB)
data2diskingb = ($_.StorageProfile.DataDisks[1].DiskSizeGB)
data3diskingb = ($_.StorageProfile.DataDisks[2].DiskSizeGB)
如果 Azure VM 有超過 5-10 個數據盤,我必須相應地復制和粘貼多次。
所以我需要一些幫助來更新腳本,以便它可以動態顯示所有數據磁盤。
先感謝您。
使用下面的 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 $disk.Name, $disk.DiskSizeGB
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.