Reporting Virtual Machine Dynamic Disk Space Using PowerShell And System Center Virtual Machine Manager 2008 R2

Dynamically expanding disks enable you to thin provision virtual machines (VMs)  storage.  This means you can pack more VMs onto a given amount of disk, but you need to ensure that if you provision more storage than is physically available, you don’t run out of space on the host.  The PowerShell script below produces a csv file showing the space available for growth on each VM with a dynamically expanding disk.  You can use this script to see how much space could still be used by your VMs and therefore see if you’re likely to run out of space on the host.  It’ll also show you if a dynamic disk is about to reach it’s maximum size.

Load PowerShell from the icon in the SCVMM console

Next copy and paste the code below into the PowerShell console, replacing VMMHOST with the name of your SCVMM host and the path of the csv file as appropriate.

Get-VirtualHardDisk -VMMServer VMMHOST -All | Select-Object @{Label=”VM Host”;Expression={$_.VMHost}},@{Label=”Name”;Expression={$_.Name}},@{Label=”Space To Grow”;Expression={[Math]::Truncate(($_.MaximumSize-$_.Size) / 1GB)}},@{Label=”VHDType”;Expression={$_.VHDType}} | Where-Object {($_.VHDType -Match “DynamicallyExpanding” -And $_.Name -NotMatch “{“)} | Sort-Object “Space To Grow” -Descending |  Export-Csv C:\DiskInformation.csv

Opening the csv in Excel produces

 

The PowerShell script is a one liner and breaks down as follows:

Get-VirtualHardDisk -VMMServer VMMHOST -All | Gets the virtual disk for the VMMServer specified

Select-Object @{Label=”VM Host”;Expression={$_.VMHost}},@{Label=”Name”;Expression={$_.Name}},@{Label=”Space To Grow”;Expression={[Math]::Truncate(($_.MaximumSize-$_.Size) / 1GB)}},@{Label=”VHDType”;Expression={$_.VHDType}} | Selects the VM host name, disk name, calculates the maximum disk size – current disk size and converts to GB, VHD type

Where-Object {($_.VHDType -Match “DynamicallyExpanding” -And $_.Name -NotMatch “{“)} | Filters the results to only include dynamically expanding disks whose names don’t start {

Sort-Object “Space To Grow” -Descending | Sorts the output by space to grow, descending

Export-Csv C:\DiskInformation.csv Exports the results to a csv file

 

Advertisement