# PowerCLI Module Installed 

<# .SYNOPSIS ScriptsbyScott.com

This process queries hard drive size and calcualtes free space on a percentage.  

.DESCRIPTION     This script queries VM's and pulls the hard drive size and gets free space. 

 .EXAMPLE      C:>VM_Free_Space.ps1     

#> 

Gets the VM hard drive free space from a list of Vm's running on a virtual server like ESXi / vCenter.

The percent can be esily changed in the script, it is currently set to 20% and puts this out to screen.

Could easily append to an email via a scheduled task for some monitoring. 

PowerShell
Edit|Remove
Add-PSSnapin VMware.VimAutomation.Core 
     
Connect-VIServer  -Server YourServerName  -User Domain\Username -Password "xxxxxxxxxx" 
 
                    
ForEach ($VM in Get-VM | where-object {($_.powerstate -ne "PoweredOff") -and ($_.Extensiondata.Guest.ToolsStatus -Match ".*Ok.*")}){ 
                  
    ForEach ($Drive in $VM.Extensiondata.Guest.Disk) { 
                  
    $Path = $Drive.DiskPath 
                  
    #Calculations  
    $Freespace = [math]::Round($Drive.FreeSpace / 1GB) 
    $Capacity = [math]::Round($Drive.Capacity/ 1GB) 
                  
    $SpaceOverview = "$Freespace" + "/" + "$capacity"  
    $PercentFree = [math]::Round(($FreeSpace)/ ($Capacity) * 100)  
                  
        #VMs with less space 
        if ($PercentFree -lt 20{      
            $Output = $Output + "VM: " + $VM.Name + "`n" 
            $Output = $Output + "Disk: " + $Path + "`n" 
            $OutPut = $Output + "Free: " + $Freespace + " GB `n" 
            $Output = $Output + "Free: " + $PercentFree + "% `n"  + "`n" 
        } 
                  
    } # End ForEach ($Drive in in $VM.Extensiondata.Guest.Disk) 
                  
} # End ForEach ($VM in Get-VM) 
                 
$Output 
                 
Clear-Variable Output  
Clear-Variable VM 
Clear-Variable Path 
Clear-Variable FreeSpace 
Clear-Variable PercentFree 
Clear-Variable Capacity 
Clear-Variable Spaceoverview 
Clear-Variable Drive