Vmware-Esxi

獲取 SAN 上 VMDK 的實際使用情況

  • August 8, 2017

我有一個腳本可以檢索我的 vCenter 集群中所有虛擬機的詳細資訊。本質上Get-VM | Get-Harddisk

我也可以獲取Provisioned SpaceUsed Space值,但這些僅適用於整個 VM,我想在實際 SAN 上獲取每個 VMDK 文件。

我有一些運氣,($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB但這並沒有檢索到我所有虛擬機的詳細資訊,我無法鍛煉,為什麼?

更新 1: 所以我發現這些資訊可以通過$vm.ExtensionData.Storage.PerDatastoreUsage. 這將返回每個數據儲存的詳細資訊數組,並顯示使用了多少磁碟。現在的問題是我不知道如何計算哪個條目與哪個磁碟相關(除了手動檢查)。如果每個磁碟都在不同的數據儲存上,這很好,但是當它們都在相同且大小相同時(即,我們有一個 Windows VM,在同一數據儲存上具有 2 個 100GB 精簡磁碟),它被證明更具挑戰性。

我最終在 VMware 社區網站上找到了這篇文章https://communities.vmware.com/message/1816389#1816389,它提供了以下程式碼作為我可以調整的解決方案:

Get-View -ViewType VirtualMachine -Property Name, Config.Hardware.Device, LayoutEx | %{
$viewVM = $_; $viewVM.Config.Hardware.Device | ?{$_ -is [VMware.Vim.VirtualDisk]} | %{
   ## for each VirtualDisk device, get some info
   $oThisVirtualDisk = $_
   ## get the LayoutEx Disk item that corresponds to this VirtualDisk
   $oLayoutExDisk = $viewVM.LayoutEx.Disk | ?{$_.Key -eq $oThisVirtualDisk.Key}
   ## get the FileKeys that correspond to the LayoutEx -> File items for this VirtualDisk
   $arrLayoutExDiskFileKeys = $oLayoutExDisk.Chain | ?{$_ -is [VMware.Vim.VirtualMachineFileLayoutExDiskUnit]}
   New-Object -TypeName PSObject -Property @{
       ## add the VM name
       VMName = $viewVM.Name
       ## the disk label, like "Hard disk 1"
       DiskLabel = $_.DeviceInfo.Label
       ## the datastore path for the VirtualDisk file
       DatastorePath = $_.Backing.FileName
       ## the provisioned size of the VirtualDisk
       ProvisionedSizeGB = [Math]::Round($_.CapacityInKB / 1MB, 1)
       ## get the LayoutEx File items that correspond to the FileKeys for this LayoutEx Disk, and get the size for the items that are "diskExtents" (retrieved as bytes, so converting to GB)
       SizeOnDatastoreGB = [Math]::Round(($arrLayoutExDiskFileKeys | %{$_.FileKey} | %{$intFileKey = $_; $viewVM.LayoutEx.File | ?{($_.Key -eq $intFileKey) -and ($_.Type -eq "diskExtent")}} | Measure-Object -Sum Size).Sum / 1GB, 1)
   } ## end new-object
} ## end foreach-object
} ## end outer foreach-object

我通過社區網站被引導到優秀的部落格http://www.lucd.info/2010/03/23/yadr-a-vdisk-reporter/,它的解決方案也包括快照的大小

引用自:https://serverfault.com/questions/866747