pf-available-space.ps1
function Get-AvailableDiskSpace($path) { $path = Get-Path $path $volumes = Get-WmiObject win32_volume $vol = $volumes | Select-Object -Property Caption, FreeSpace, @{Name='Length'; Expression={ $_.Caption.Length }} | Sort-Object Length -Descending | Where-Object { $path -like "$($_.Caption)*" } | Select-Object -First 1 if (-not $vol) { Write-Error "No disk volume found for '$path'" } return $vol.FreeSpace } function Get-AvailableDiskSpace::Example { $result = Get-AvailableDiskSpace 'C:\ClusterStorage\Volume1\VM' Get-SizeText -size $result } function Assert-AvailableDiskSpace($path, $requiredSpace) { $path = Get-Path $path $freeSpace = Get-AvailableDiskSpace -path $path if ( $freeSpace -lt $requiredSpace ) { $requiredSpaceGB = Get-SizeText $requiredSpace $freeSpaceGB = Get-SizeText $freeSpace throw "Not enough space in $path, required at least $requiredSpaceGB is required and $freeSpaceGB are available" } } function Assert-AvailableDiskSpace:::Example ($path, $requiredSpace) { Assert-AvailableDiskSpace 'C:\ClusterStorage\Volume1\VM' 30GB } |