Displaying vSphere disk properties (including provisioning & persistence)
I was doing some tidying of old scripts and came across something I thought it might be useful, so I tidied it up and added some documentation.
This PowerShell script uses the vSphere PowerCLI to display a list of virtual machine disks, file-names, modes (persistent or non-persistent), sizes and whether or not the disk is thinly provisioned. You’ll need to connect to one or more vSphere servers first.
# Create an empty array for results
$arrResults = @()
# Get the .net view of the virtual machines
$objVMViews = Get-View -ViewType "VirtualMachine" | Where-Object {!$_.Config.Template}
# Loop through the .net view objects representing the machines
foreach ($objVMView in $objVMViews){
# Loop through the .net view's devices
foreach ($objDevice in $objVMView.Config.Hardware.Device) {
# Where the device is a virtual disk
if ($objDevice.GetType().Name -eq "VirtualDisk"){
# Create a new object to represent the virtual disk
$objVirtualDisk = New-Object PSObject
# Append properties to the disk object based on the view object
$objVirtualDisk | Add-Member -Name "Name" -MemberType NoteProperty -Value $objVMView.Name
$objVirtualDisk | Add-Member -Name "DeviceLabel" -MemberType NoteProperty -Value $objDevice.DeviceInfo.Label
$objVirtualDisk | Add-Member -Name "FileName" -MemberType NoteProperty -Value $objDevice.Backing.FileName
$objVirtualDisk | Add-Member -Name "DiskMode" -MemberType NoteProperty -Value $objDevice.Backing.DiskMode
$objVirtualDisk | Add-Member -Name "SizeGB" -MemberType NoteProperty -Value ($objDevice.CapacityInKB / 1024 / 1024)
# If there is a ThinProvisioned property, then the disk is sparse
if ($objDevice.Backing.ThinProvisioned){
$objVirtualDisk | Add-Member -Name "ThinProvisioned" -MemberType NoteProperty -Value $True}
else {
$objVirtualDisk | Add-Member -Name "ThinProvisioned" -MemberType NoteProperty -Value $False
}
# Append the virtual disk object to the array of results
$arrResults += $objVirtualDisk
}
}
}
# Display the results on screen
$arrResults | Format-Table
It’s likely that I based the original version of this script on someone else’s work as it contained a couple of techniques which I don’t tend to use (like using Select-Object to create object properties), but I’m afraid I can’t remember where, and searching for a couple of keywords brings back no results.