Hi,
Can anyone here please assist me in fixing the script that was created by LucD a few years ago to show the actual Guest OS disk space usage?
$report = @()
foreach($vm in Get-VM){
Get-HardDisk -VM $vm | ForEach-Object {
$HardDisk = $_
$row = "" | Select Hostname, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType
$row.Hostname = $vm.VMHost.Name
$row.VM = $VM.Name
$row.GuestName = $vm.Guest.HostName
$row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[")
$row.VMXpath = $HardDisk.FileName
$row.HardDisk = $HardDisk.Name
$row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB))
$row.DiskFreespace = $row.CapacityGB - ($_.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum)
$row.DiskType = $HardDisk.get_DiskType()
$row.TotalVMFSConsumed = $vm.get_UsedSpaceGB()
$row.ProvisionType = $HardDisk.StorageFormat
$report += $row
}
}
$report | Export-Csv -Path C:\VMDisk-CapacityReport.csv -NoTypeInformation -UseCulture
The Line #13 is somehow not showing the actual guest disk space consumption correctly.
Thanks in advance.
IMHO line 13 should be:
$row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum
IMHO line 13 should be:
$row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum
Hi Rob,
Many thanks for the quick response.
How to display the result to just 2 round digits ?
One more last request, how can I get reliable result for the disk used capacity ?
I got: $Row.DiskUsedSpace = $row.CapacityGB - $row.DiskFreeSpace
but the result is minus ?
To display the result to just 2 round digits you can use the following command:
$row.DiskFreespace = [math]::Round(($vm.guest.disks | measure-object freespacegb -sum | select -ExpandProperty sum),2)
The capacity is the capacity of one disk. The free space is the free space of all of the disks of the virtual machine together. There it is going wrong. If you subtract the freespace of all of the disks of the virtual machine together from the capacity of one disk, you can get a value below zero. There is not a rock-solid solution that I am aware of, to match the disks that you get with the Get-HardDisk cmdlet to the disks return by (Get-VM).Guest.Disks. So, it is hard to get the free space from the output of the Get-HardDisk cmdlet.
RvdNieuwendijk , I wonder why is the value of the below column:
$row.DiskFreespace = [math]::Round(($vm.guest.disks | measure-object freespacegb -sum | select -ExpandProperty sum),2)
is always showing the free disk space of VM Disk 1 only not updated to the respective Disk 2, Disk 3, ... ?
I have tried it in my environment and the command shows the correct sum of the FreeSpaceGB of all of the disks of the virtual machine, as you can see in the following output.
PowerCLI C:\> $vm.guest.disks
CapacityGB FreeSpaceGB Path
---------- ----------- ----
49.997 0.293 D:\
49.997 45.720 K:\
199.998 161.014 E:\
499.997 462.090 F:\
99.997 4.342 H:\
99.997 97.315 G:\
99.997 26.391 J:\
59.900 35.618 C:\
PowerCLI C:\> [math]::Round(($vm.guest.disks | measure-object freespacegb -sum | select -ExpandProperty sum).2)
832.78
ok, let me try that once again.
By the way, how is that possible to show the drive letter ?
if you can share that line of code, that‘d be greatly appreciated.
I have tried the above PowerCLI script for specific cluster however the disk free space and percentgae of free space are not connect.
$report = @()
$vms = Get-Cluster <Clustername>| get-vm
foreach($vm in Get-VM){
Get-HardDisk -VM $vm | ForEach-Object {
$HardDisk = $_
$row = "" | Select Hostname, Cluster, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType
$row.Hostname = $vm.VMHost.Name
$row.Cluster = (Get-Cluster -VM $vm ).Name
$row.VM = $VM.Name
$row.GuestName = $vm.Guest.HostName
$row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[")
$row.VMXpath = $HardDisk.FileName
$row.HardDisk = $HardDisk.Name
$row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB))
$row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum
$row.DiskType = $HardDisk.get_DiskType()
$row.TotalVMFSConsumed = $vm.get_UsedSpaceGB()
$row.ProvisionType = $HardDisk.StorageFormat
$report += $row
}
}
$report | Export-Csv -Path
Can someone help me with this.