Skip to content

Instantly share code, notes, and snippets.

@c0shea
Created October 16, 2016 22:29
Show Gist options
  • Save c0shea/6d46f700a93a7d7b678342cb3670aafd to your computer and use it in GitHub Desktop.
Save c0shea/6d46f700a93a7d7b678342cb3670aafd to your computer and use it in GitHub Desktop.
Gets disk utilization from local and remote machines
[CmdletBinding()]
Param ([Parameter(Mandatory=$False,Position=0)]
[String[]]$ComputerName = "SERVER")
Write-Host ""
# Change these values
$IgnoreSystemReserved = $true
# Percents must be expressed as a decimal value (e.g. 50% is 0.5)
$WarningPercent = 0.15
$DangerPercent = 0.1
$CriticalPercent = 0.05
ForEach ( $Name in $ComputerName ) {
Write-Host "Disk Utilization for $Name is: "
Get-WmiObject -ComputerName $Name -Class Win32_Volume `
| Format-Table -auto `
@{Label="Drive";`
Expression={$_.DriveLetter};`
Align="Right"},`
@{Label="Volume Label";`
Expression={$_.Label};`
Width=25},`
@{Label="Size";`
Expression={`
If($_.Capacity / 1GB -lt 1) {"{0:N0} MB" -f ($_.Capacity / 1MB)}`
ElseIf($_.Capacity / 1TB -lt 1) {"{0:N0} GB" -f ($_.Capacity / 1GB)}`
Else {"{0:N0} TB" -f ($_.Capacity / 1TB)}};`
Align="Right"},`
@{Label="Free";`
Expression={`
If($_.FreeSpace / 1GB -lt 1) {"{0:N0} MB" -f ($_.FreeSpace / 1MB)}`
ElseIf($_.FreeSpace / 1TB -lt 1) {"{0:N0} GB" -f ($_.FreeSpace / 1GB)}`
Else {"{0:N0} TB" -f ($_.FreeSpace / 1TB)}};`
Align="Right"},`
@{Label="% Free";`
Expression={"{0:P0}" -f ($_.FreeSpace / $_.Capacity)};`
Align="Right"},`
@{Label="% Used";`
Expression={"{0:P0}" -f (($_.Capacity - $_.FreeSpace) / $_.Capacity)};`
Align="Right"},`
@{Label="Status";`
Expression={`
$PercentFree = $_.FreeSpace / $_.Capacity;`
If($IgnoreSystemReserved -and $_.Label -eq "System Reserved") {"IGNORED"}`
ElseIf($PercentFree -le $WarningPercent -and $PercentFree -gt $DangerPercent) {"WARN"}`
ElseIf($PercentFree -le $DangerPercent -and $PercentFree -gt $CriticalPercent) {"DANGER"}`
ElseIf($PercentFree -le $CriticalPercent) {"CRITICAL"}`
Else {"OK"}};`
Width=8};
}
@c0shea
Copy link
Author

c0shea commented Oct 16, 2016

Call via the command line: powershell "DiskUtilization.ps1" "SERVER","SERVER2"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment