List vSphere Datastores with less than 20% Free Space

The below script lists all datastores with less than 20% free space:


Connect-VIServer -Server vCenterName

function CalcPercent {
param(
[parameter(Mandatory = $true)]
[int]$InputNum1,
[parameter(Mandatory = $true)]
[int]$InputNum2)
$InputNum1 / $InputNum2*100
}

$datastores = Get-Datastore | Sort Name
ForEach ($ds in $datastores)
{
if (($ds.Name -match "Shared") -or ($ds.Name -match ""))
{
$PercentFree = CalcPercent $ds.FreeSpaceMB $ds.CapacityMB
$PercentFree = "{0:N2}" -f $PercentFree
$ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree
}
}
$datastores | where-object {$_.PercentFree -le 20} | Select Name,FreeSpaceGB,CapacityGB,PercentFree
Facebooktwittergoogle_pluslinkedinby feather

Leave a Reply

Your email address will not be published. Required fields are marked *