This script scans all VMs in a vCenter and reports the location of each hard drive. The information collected for each drive is as follows:
VM, Thick/Thin, file path, datastore, capacity, size on disk, Number of snapshots, total size of snapshots.
$Report = @()
$VMs = Get-VM
foreach ($VM in $VMs)
{
# loop through each VM
$VMname = $VM.name
# get diskobs (low level HD file info) and disk (HD info)
$DiskObjs = $vm.ExtensionData.layoutex.disk
$Disks = get-harddisk -vm $VM
foreach ($Disk in $Disks)
{
$tmpObj = '' | select VM,StartageFormat,FilePath,DS,CapacityGB,UsedGB,DiskName,NumSnaps,SnapSizeGB
# populate VM and basic disk info
$tmpObj.VM = $VMName
$tmpObj.StartageFormat = $disk.StorageFormat
$tmpObj.FilePath = $Disk.FileName
$tmpObj.DS = (($disk.Filename).split("]"))[0].replace("[","")
$tmpObj.CapacityGB = $Disk.CapacityGB
$RealFileName = $Disk.FileName.Replace(".vmdk","-flat.vmdk")
$tmpObj.UsedGB = ($VM.ExtensionData.layoutex.file | where{$_.name -eq $RealFileName}).Size /1gb
# get low level file info for this disk (using disk key value)
$ThisDiskObj = ($DiskObjs | where{$_.key -eq $Disk.ExtensionData.key}).chain.filekey | sort
# loop through all files (snapshot) that make up this disk
$tmpObj.SnapSizeGB = 0
$tmpObj.NumSnaps = 0
foreach ($DiskFileKey in $ThisDiskObj)
{
$ThisDiskFile = $vm.ExtensionData.layoutex.file | where{$_.Key -eq $DiskFileKey}
if ($ThisDiskFile.name -match "-flat")
{
# base disk (-flat) VMDK
$tmpObj.UsedGB = $ThisDiskFile.Size / 1gb
}
$ThisDiskFile = $vm.ExtensionData.layoutex.file | where{$_.Key -eq $DiskFileKey}
if ($ThisDiskFile.name -match "-delta")
{
# snapshot (-delta) VMDK
$tmpObj.SnapSizeGB = $tmpObj.SnapSizeGB + $ThisDiskFile.Size / 1gb
$tmpObj.NumSnaps ++
}
}
$tmpObj.DiskName = $Disk.Name
$Report += $tmpObj
}
}
$Report | export-csv "c:\temp\VMs.csv" -NoTypeInformation