PowerCLI: Get VM disk usage per cluster




Today I was asked to gather a list of all VMs within a given cluster and report on their disk usage (all the VMs are thick provisioned) and currently do not have a tool to perform a seemly simple task. After some searching I have found many versions of this script around the internet but none that quite suited me needs. I have modified about 50% of the original script to suit my needs and added these features.

  • Prompts for vCenter hostname or IP
  • Requires user to select a cluster
  • Queries vCenter for list of cluster names (for use above)
  • Limits results to specified cluster
  • Prompts for output filename
  • Default saves to current user’s Documents folder
  • Prints location of filename
  • Auto launches file once complete

I should also mention that I am not a script writer. My abilities exist in finding, understanding (most of the time), and modifying existing Powershell scripts. I will be unable to assist with drastic modifications to any script.

Requirements: VMware PowerCLI, vCenter Credentials with appropriate permissions

How to run: Save below script to .ps1 format and run from PowerCLI. No need to modify anything to make compatible with your environment.

$vCenterName = Read-Host -Prompt ‘Enter vCenter hostname or IP’
Write-Host “Connecting…”
connect-viserver $vCenterName

$MyCollection = @()

get-cluster | select-object name
$ClusterName = Read-Host -Prompt ‘Enter Cluster Name (must be exact)’
$ClusterFilter = get-view -ViewType ClusterComputeResource -Property Name -Filter @{“Name” = $ClusterName } | select -ExpandProperty MoRef

$AllVMs = Get-View -ViewType VirtualMachine -SearchRoot $ClusterFilter | Where {-not $_.Config.Template}
$SortedVMs = $AllVMs | Select *, @{N=”NumDisks”;E={@($_.Guest.Disk.Length)}} | Sort-Object -Descending NumDisks

ForEach ($VM in $SortedVMs){
$Details = New-object PSObject
$Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
$DiskNum = 0
Foreach ($disk in $VM.Guest.Disk){
$Details | Add-Member -Name “Disk$($DiskNum)path” -MemberType NoteProperty -Value $Disk.DiskPath
$Details | Add-Member -Name “Disk$($DiskNum)Capacity(MB)” -MemberType NoteProperty -Value ([fusion_builder_container hundred_percent=”yes” overflow=”visible”][fusion_builder_row][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][math]::Round($disk.Capacity/ 1MB))
$Details | Add-Member -Name “Disk$($DiskNum)FreeSpace(MB)” -MemberType NoteProperty -Value ([math]::Round($disk.FreeSpace / 1MB))
$MyCollection += $Details

$OutputFileName = Read-Host ‘Enter filename including extension (.csv)’
$MyCollection | Export-Csv $env:USERPROFILE\Documents\$OutputFileName

Write-Host “File has been saved to” $env:USERPROFILE\Documents\$OutputFileName

explorer.exe $env:USERPROFILE\Documents\$OutputFileName

# Out-GridView, Export-Csv, ConvertTo-Html or ConvertTo-Xml can be used above instead