Find capacity risks without rounding them away
Calculate free-space percentages, preserve unknown data, and apply a precise threshold.
State the threshold
This lab's policy marks a volume Low when free space is strictly below 20 percent. Exactly 20 percent is OK. These are exercise settings, not a universal production threshold; real capacity policies depend on workload, growth, and recovery time.
Convert the CSV values before dividing. Calculate the unrounded percentage as 100 * FreeBytes / TotalBytes. Use that value for the decision, then round the displayed FreePercent to two decimal places.
$ratio = 100 * 19999 / 100000
[pscustomobject]@{ Raw=$ratio; Display=[math]::Round($ratio,2) }
A value of 19.999 percent displays as 20.00 but still falls below the threshold. Rounding first could hide the finding. The fixtures include values immediately below and above the boundary.
Unknown does not mean healthy
A total capacity of zero cannot support a percentage calculation. Negative free space or free space greater than total capacity is also invalid for this export. Return Unknown with a null percentage for invalid data, rather than divide by zero, clamp the values, or silently remove the row.
Zero free bytes on a valid positive-capacity disk is different: that is a valid measurement of a full disk and should be Low.
Keep the volume ID, computer, and mount together. A mount name such as D: is not unique across a fleet.
Import-Csv '/home/user/disks.csv' | Select-Object VolumeId, Computer, Mount, TotalBytes, FreeBytes
Put it into practice.
For every disks.csv row emit VolumeId, Computer, Mount, FreePercent, and State. Valid measurements have TotalBytes > 0 and 0 <= FreeBytes <= TotalBytes. State is Low below 20%, otherwise OK; decide before rounding FreePercent to two decimals. Invalid measurements produce Unknown and a null FreePercent. Preserve all rows.
See an explained solution
foreach ($disk in Import-Csv '/home/user/disks.csv') {
$total = [double]$disk.TotalBytes
$free = [double]$disk.FreeBytes
$percent = $null
$state = 'Unknown'
if ($total -gt 0 -and $free -ge 0 -and $free -le $total) {
$ratio = 100 * $free / $total
$state = if ($ratio -lt 20) { 'Low' } else { 'OK' }
$percent = [math]::Round($ratio, 2)
}
[pscustomobject]@{ VolumeId=$disk.VolumeId; Computer=$disk.Computer; Mount=$disk.Mount; FreePercent=$percent; State=$state }
}
The validity guard separates missing capacity from genuinely full disks. The decision uses the raw ratio, so V06 stays Low even though its displayed percentage rounds to 20.