Get-VMDiskUsage

Description:

This script calculates the free and used disk space for all logical drives inside your vm’s and combines this data with free/used space on your datastores.

Code:

#Get VMware Disk Usage
# Created by Hugo Peeters
# http://www.peetersonline.nl
 
# VARIABLES
$Decimals = 1
$VCServer = “MYVCSERVER”
 
# SCRIPT
# Connect to VC
Write-Progress “Gathering Information” “Connecting to Virtual Center” -Id 0
$VC = Connect-VIServer $VCServer
# Create Output Collection
$myCol = @()
# List Datastores (Datastore Name)
Write-Progress “Gathering Information” “Listing Datastores” -Id 0
$Datastores = Get-Datastore | Sort Name
# List vms
Write-Progress “Gathering Information” “Listing VMs and Disk Files” -Id 0
$VMSummaries = @()
ForEach ($vm in (Get-VM))
{
$VMView = $VM | Get-View
ForEach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | Where {$_.DeviceInfo.Label -match “SCSI Controller”}))
{
ForEach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | Where {$_.ControllerKey -eq $VirtualSCSIController.Key}))
{
$VMSummary = “” | Select VM, HostName, PowerState, DiskFile, DiskName, DiskSize, SCSIController, SCSITarget
$VMSummary.VM = $VM.Name
$VMSummary.HostName = $VMView.Guest.HostName
$VMSummary.PowerState = $VM.PowerState
$VMSummary.DiskFile = $VirtualDiskDevice.Backing.FileName
$VMSummary.DiskName = $VirtualDiskDevice.DeviceInfo.Label
$VMSummary.DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB
$VMSummary.SCSIController = $VirtualSCSIController.BusNumber
$VMSummary.SCSITarget = $VirtualDiskDevice.UnitNumber
$VMSummaries += $VMSummary
}
}
Clear-Variable VMView -ErrorAction SilentlyContinue
}
# Loop through Datastores
ForEach ($Datastore in $Datastores)
{
# List vmdk files in datastore (vmdk Name)
Write-Progress “Gathering Information” (”Processing Datastore {0}-f $Datastore.Name) -Id 0
$DSView = $Datastore | Get-View
$fileQueryFlags = New-Object VMware.Vim.FileQueryFlags
$fileQueryFlags.FileSize = $true
$fileQueryFlags.FileType = $true
$fileQueryFlags.Modification = $true
$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.details = $fileQueryFlags
$searchSpec.sortFoldersFirst = $true
$dsBrowser = Get-View $DSView.browser
$rootPath =["+$DSView.summary.Name+"]$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
ForEach ($result in $searchResult)
{
ForEach ($vmdk in ($result.File | ?{$_.Path -like*.vmdk”} | Sort Path))
{
Write-Progress “Gathering Information” (”Processing VMDK {0}-f $vmdk.Path) -Id 1
Write-Host==============================================================================# Find vm using the vmdk (VM Name)
$VMRef = ($VMSummaries | ?{$_.DiskFile -match $Datastore.Name -and $_.DiskFile -match $vmdk.Path})
“VMDK {0} belongs to VM {1}-f $vmdk.Path, $VMRef.VM
If ($VMRef.Powerstate -eq “PoweredOn”)
{
Write-Host “VM is powered on” -ForegroundColor “yellow”
$Partitions = Get-WmiObject -Class Win32_DiskPartition -ComputerName $VMRef.HostName
If ($?)
{
$Disks = Get-WmiObject -Class Win32_DiskDrive -ComputerName $VMRef.HostName
$LogicalDisks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $VMRef.HostName
$DiskToPartition = Get-WmiObject -Class Win32_DiskDriveToDiskPartition -ComputerName $VMRef.HostName
$LogicalDiskToPartition = Get-WmiObject -Class Win32_LogicalDiskToPartition -ComputerName $VMRef.HostName
Write-Host “Read partition and disk information” -ForegroundColor “yellow”
# Match disk based on SCSI ID’s
$DiskMatch = $Disks | ?{($_.SCSIPort - 1) -eq $VMRef.SCSIController -and $_.SCSITargetID -eq $VMRef.SCSITarget}
If ($DiskMatch -eq $null){Write-Warning “NO MATCHES!}
Else
{
Write-Host “Found match:” -ForegroundColor “yellow”
$DiskMatch
# Find the Partition(s) on this disk
$PartitionsOnDisk = ($DiskToPartition | ?{$_.Antecedent -eq $DiskMatch.__PATH})
If ($PartitionsOnDisk -eq $null){Write-Warning “NO PARTITIONS!}
Else
{
ForEach ($PartitionOnDisk in $PartitionsOnDisk)
{
Write-Host “Disk contains partition” -ForegroundColor “yellow”
$PartitionOnDisk.Dependent
$PartitionMatches = $Partitions | ?{$_.__PATH -eq $PartitionOnDisk.Dependent}
ForEach ($PartitionMatch in $PartitionMatches)
{
$LogicalDiskRefs = $LogicalDiskToPartition | ?{$_.Antecedent -eq $PartitionMatch.__PATH}
If ($LogicalDiskRefs -eq $null)
{
Write-Warning “NO LOGICAL DISKS!}
Else
{
ForEach ($LogicalDiskRef in $LogicalDiskRefs)
{
$LogicalDiskMatches = $LogicalDisks | ?{$_.__PATH -eq $LogicalDiskRef.Dependent}
ForEach ($LogicalDiskMatch in $LogicalDiskMatches)
{
Write-Host “Matching Logical Disk:” -ForegroundColor “yellow”
$LogicalDiskMatch
# Create Output Object
$myObj = “” | Select Datastore, DSSizeGB, DSFreeGB, DSPercentFree, DiskFile, VM, HardDisk, DriveLetter, DiskSizeGB, DiskFreeGB, PercFree
# List datastore name
$myObj.Datastore = $Datastore.Name
# Determine datastore size in GB
$myObj.DSSizeGB = [Math]::Round(($Datastore.CapacityMB * 1MB / 1GB),$Decimals)
$myObj.DSFreeGB = [Math]::Round(($Datastore.FreeSpaceMB * 1MB / 1GB),$Decimals)
# Determine datastore free space (DS%Free)
$myObj.DSPercentFree = [Math]::Round((100*($Datastore.FreeSpaceMB/$Datastore.CapacityMB)),$Decimals)
# List disk file name
$myObj.DiskFile = $vmdk.Path
# List VM Name
$myObj.VM = $VMRef.VM
# Determine virtual hard disk / logical drive
$myObj.HardDisk = $VMRef.DiskName
# Report driveletter
$myObj.DriveLetter = $LogicalDiskMatch.DeviceID
# Report Size
$myObj.DiskSizeGB = [Math]::Round(($LogicalDiskMatch.Size / 1GB),$Decimals)
# Report Free Space
$myObj.DiskFreeGB = [Math]::Round(($LogicalDiskMatch.FreeSpace / 1GB),$Decimals)
# Calculate Percentage free space
$myObj.PercFree = [Math]::Round((100 * ([int]($LogicalDiskMatch.FreeSpace / 1MB) / [int]($LogicalDiskMatch.Size / 1MB))),$Decimals)
Write-Host “RESULT:” -ForegroundColor “yellow”
$myObj
# Add output object to output collection
$myCol += $myObj
}
Clear-Variable LogicalDiskMatches -ErrorAction SilentlyContinue
}
}
Clear-Variable LogicalDiskRefs -ErrorAction SilentlyContinue
}
Clear-Variable PartitionMatches -ErrorAction SilentlyContinue
}
}
Clear-Variable PartitionsOnDisk -ErrorAction SilentlyContinue
}
Clear-Variable DiskMatch -ErrorAction SilentlyContinue
Clear-Variable Disks -ErrorAction SilentlyContinue
Clear-Variable LogicalDisks -ErrorAction SilentlyContinue
Clear-Variable DiskToPartition -ErrorAction SilentlyContinue
Clear-Variable LogicalDiskToPartition -ErrorAction SilentlyContinue
}
Clear-Variable Partitions -ErrorAction SilentlyContinue
}
Else
{
Write-Host “VM is powered off” -ForegroundColor “yellow”
}
Clear-Variable VMRef -ErrorAction SilentlyContinue
Write-Progress “Gathering Information” (”Processing VMDK {0}-f $vmdk.Path) -Id 1 -Completed
}
}
}
# Disconnect from VC
Disconnect-VIServer -Confirm:$False
 
# OUTPUT
Write-Host===================================================Write-Host===================================================$TotalDSFree = ($myCol | Select Datastore, DSFreeGB -Unique | Measure-Object DSFreeGB -Sum).Sum
$TotalDSSize = ($myCol | Select Datastore, DSSizeGB -Unique | Measure-Object DSSizeGB -Sum).Sum
$AverageDSFree = [Math]::Round(100 * ($TotalDSFree / $TotalDSSize),$Decimals)
$AverageDiskFree = [Math]::Round(100 * (($myCol | Measure-Object DiskFreeGB -Sum).Sum / ($myCol | Measure-Object DiskSizeGB -Sum).Sum),$Decimals)
Write-Host “Total DS Free: $TotalDSFreeWrite-Host “Total DS Size: $TotalDSSizeWrite-Host “Average DS Free Percentage: $AverageDSFreeWrite-Host “Average Disk Free Percentage: $AverageDiskFree$myCol | Export-Csv -NoTypeInformation ‘D:\scripts\VMwareDiskUsage.csv’

Download:
get-vmdiskusage (rename to .ps1)

Disclaimer:

All the scripts on PeetersOnline.nl are published under a Creative Commons license, which means you should refer to me if you want to republish (pieces of) them. Thank you. More information about Creative Commons can be found here: Creative Commons Attribution 3.0 Netherlands License.

Creative Commons License
 

 

  • ckotte

    March 17th, 2009

    Hi,
    is it possible to include also the disks of VMs which are offline, which have LINUX installed and which are in other domains?
    Thanks.
    Regards,
    Christian

  • admin

    March 17th, 2009

    Hi Christian,

    Off line VMs is not possible, as you need to access the client OS the retrieve the disk information. Linux might be possible via the VMware Tools. Try searching the VMware VI Toolkit Communities for an answer. For conencting to VMs in other domains, take a look at the *-VICredentialStore cmdlets. They allow for storing and retrieving credentials for use within scripts.

    Hope this helps,
    Hugo

  • ckotte

    March 17th, 2009

    @admin
    Would it be possible to include only the vmdk disk size information for such VMS? Otherwise, I have to completely rewrite the script since the loop continues if the VM is powered of or if WMI is not available.

  • ckotte

    March 17th, 2009

    I meant to say that you can rewrite the script if it’s possible?

  • admin

    March 17th, 2009

    @ckotte
    You could add the vmdk file size (DiskFileSize) to the script:
    # Create Output Object
    $myObj = “” | Select Datastore, DSSizeGB, DSFreeGB, DSPercentFree, DiskFile, DiskFileSizeGB, VM, HardDisk, DriveLetter, DiskSizeGB, DiskFreeGB, PercFree

    # List disk file name
    $myObj.DiskFile = $vmdk.Path
    # List disk size
    $myObj.DiskFileSizeGB = [math]::Round(($vmdk.FileSize / 1GB),$decimals)

  • Matt Hentov

    April 19th, 2009

    Just a small quibble.

    Odd that you would publish your scripts under Creative Commons yet not post them for download as pure ASCII.

    Thanks for solving this problem for us. I thought I’d need to purchase some expensive third party tool to get this data.

    Many thanks again.

    - Matt

  • Nicholas Carde

    February 21st, 2010

    Hello,

    I’m getting the following error when the script attempts to create the .csv file (the output directory location exists) — thank you for your help!

    Total DS Free: 1934.4
    Total DS Size: 14284.6
    Average DS Free Percentage: 13.5
    Average Disk Free Percentage: 23.2
    Export-Csv : Cannot bind argument to parameter ‘Path’ because it is null.
    At D:\Temp\powershell\scripts\get-vmdiskusage.ps1:181 char:20
    + $myCol | Export-Csv <<<< -NoTypeInformation $ouputfile

  • admin

    February 21st, 2010

    Try adding a line in the beginning:
    $ouputfile = ‘D:\scripts\output.csv’

    That might be missing.

    Hugo

  • Nicholas Carde

    February 22nd, 2010

    Thank you, Hugo!

    In the download script link above there is a typo in the export-csv section (calls $ouputfile instead of $outputfile as declared on Line 8).

  • GoldenArm

    April 15th, 2010

    I am getting this error when I run this code :

    Exception calling “SearchDatastoreSubFolders” with 2 arguments: Permission to perform this operation was denied”

    Could you tell me why …?

    Thanks in Advance
    _M

  • admin

    April 15th, 2010

    Looks like you do not have proper permissions to browse datastores.

  • GoldenArm

    April 15th, 2010

    THanks. Found out that already. I referred the “visdk25programmingguide.pdf” document….see the last line on page 184 of this book. Which has the details.

    Thanks
    -M

  • GoldenArm

    April 16th, 2010

    Hi Admin,

    After fixing the privelege issue when I ran the code….I get a different error….Get-WmiObject : Access is denied. (Exception from HRESULT: 0×80070005 (E_ACCESSDENIED))…could you tell me what needs to be done to get rid of this?

    Thanks
    -M

  • Ancksu

    May 21st, 2010

    Hi Admi,
    Your script is exactly what I need, but I have error message. Can you please help, me ?

    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    At C:\temp\disk.ps1:70 char:28
    + $Partitions = Get-WmiObject <<<< -Class Win32_DiskPartition -ComputerName $V
    MRef.HostName
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMExcept
    ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
    .GetWmiObjectCommand

    Thanks in advance

  • admin

    May 22nd, 2010

    Ancksu,
    The error is: RPC server is unavailable. This means you could not connect to one of the servers. It might be powered off or running linux. You can try adding $erroractionpreference = “SilentlyContinue” to the start of the script.
    Hugo

  • Ancksu

    June 1st, 2010

    Thanks Hugo,
    I did it but the problem is I only get 3 on 30 VM.
    In the 30 VM one is a linux so normal, but all the other VM are Windows and are power on.
    Do you have any idea ?

  • Ancksu

    June 1st, 2010

    I solved the problem. In fact it was a naming problem.
    By replacing $VMRef.HostName by $VMRef.VM and that work perfectly.
    thanks

  • Roland

    July 28th, 2010

    Hi Hugo

    When I run your script in PowerCLI it returns all VMs to be offline and therefore it will not output any information. But in fact all my VMs (almost all Windows machines) are running and have up to date VM tools. I’m running vSphere/vCenter 4.0 U1, what am I doing wrong?

    Cheers
    Roland

  • No trackbacks yet

Leave a Comment

* are Required fields