SHOW:
|
|
- or go back to the newest paste.
| 1 | # File path to store disk space usage CSV | |
| 2 | $outFile = "C:\disks.txt" | |
| 3 | ||
| 4 | # Set delta threshold in percentage | |
| 5 | # E.g.: 5 means a change in free space of more than 5% will trigger warning | |
| 6 | $deltaThresholdPercent = 1 | |
| 7 | ||
| 8 | # Set delta threshold in GB | |
| 9 | # E.g.: 5 means a change in free space of more than 5 GB will trigger warning | |
| 10 | $deltaThresholdSpace = 1 | |
| 11 | ||
| 12 | # Absolute threshold in percentage | |
| 13 | # E.g.: 20 means free space below 20% will trigger warning | |
| 14 | $absoluteThresholdPercent = 20 | |
| 15 | ||
| 16 | # Absolute threshold in GB | |
| 17 | # E.g.: 10 means free space below 10GB will trigger warning | |
| 18 | $absoluteThresholdSpace = 10 | |
| 19 | ||
| 20 | ||
| 21 | ||
| 22 | # Initialize warning; assume false | |
| 23 | $warning = $false | |
| 24 | ||
| 25 | # Get hostname | |
| 26 | $computerName = Get-Content Env:\COMPUTERNAME | |
| 27 | ||
| 28 | # Email subject line | |
| 29 | $subj = "Disk Usage Report for " + $computerName + "." | |
| 30 | ||
| 31 | # Start email message body | |
| 32 | $msg = "`r`n=== Disk Usage Report for " + $computerName + " ===`n`n" | |
| 33 | ||
| 34 | # Email recipients | |
| 35 | - | [String[]] $recipients = "[email protected]" |
| 35 | + | [String[]] $recipients = "[email protected]" |
| 36 | ||
| 37 | # Load all local volumes | |
| 38 | $disks = Get-WmiObject Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
| |
| 39 | ||
| 40 | # Iterate over volumes | |
| 41 | foreach ($disk in $disks) | |
| 42 | {
| |
| 43 | ||
| 44 | $volumeLetter = $disk.DeviceID | |
| 45 | $totalSize = ($disk.Size/1GB) | |
| 46 | ||
| 47 | [double]$currentFreeSpace = [System.Math]::Round(($disk.FreeSpace/1GB), 2) | |
| 48 | [double]$currentFreePercent = [System.Math]::Round(($currentFreeSpace/$totalSize) * 100, 2) | |
| 49 | ||
| 50 | # Run absolute free space check | |
| 51 | if (100 - $currentFreePercent -le $absoluteThresholdPercent -or $currentFreeSpace -le $absoluteThresholdSpace) | |
| 52 | {
| |
| 53 | # Trigger warning | |
| 54 | $warning = $true | |
| 55 | $msg += $volumeLetter + " has " + $currentFreeSpace + "GB (" + $currentFreePercent + "%) disk space remaining.`r`n"
| |
| 56 | $msg += "Total free space less than " + $absoluteThresholdSpace + "GB or " + $absoluteThresholdPercent + "% triggers this warning.`r`n" | |
| 57 | } | |
| 58 | ||
| 59 | # Don't run relative check if comparison data doesn't exist | |
| 60 | if ((Test-Path $outFile) -eq $true) | |
| 61 | {
| |
| 62 | # Read CSV | |
| 63 | $csvIn = Import-Csv $outFile -Header("volume","freeSpace")
| |
| 64 | foreach ($row in $csvIn) | |
| 65 | {
| |
| 66 | if ($row.volume -eq $volumeLetter) | |
| 67 | {
| |
| 68 | [double]$oldFreePercent = [System.Math]::Round(($row.freeSpace/$totalSize) * 100, 2) | |
| 69 | [double]$oldFreeSpace = $row.freeSpace | |
| 70 | } | |
| 71 | } | |
| 72 | ||
| 73 | # Calculate deltas | |
| 74 | [double]$deltaFreePercent = [System.Math]::Round((([math]::abs($currentFreeSpace - $oldFreeSpace))/$totalSize) * 100, 2) | |
| 75 | [double]$deltaFreeSpace = [System.Math]::Round(([math]::abs($currentFreeSpace - $oldFreeSpace)), 2) | |
| 76 | ||
| 77 | # Run relative free space check | |
| 78 | if (($deltaFreePercent -ge $deltaThresholdPercent) -or ($deltaFreeSpace -ge $deltaThresholdSpace)) | |
| 79 | {
| |
| 80 | $warning = $true | |
| 81 | $lastWriteTime = (Get-Item $outFile).LastWriteTime | |
| 82 | $msg += $volumeLetter + " has lost " + $deltaFreeSpace + "GB (" + $deltaFreePercent + "%) free space since " + $lastWriteTime + "`r`n"
| |
| 83 | $msg += $volumeLetter + " currently has " + $currentFreeSpace + "GB (" + $currentFreePercent + "%) free space.`r`n"
| |
| 84 | $msg += "Free space losses in excess of " + $deltaThresholdSpace + "GB or " + $deltaThresholdPercent + "% trigger this warning.`r`n" | |
| 85 | } | |
| 86 | } | |
| 87 | ||
| 88 | # Update CSV with new disk usage | |
| 89 | $csv += $volumeLetter + "," + $currentFreeSpace + "`r`n" | |
| 90 | } | |
| 91 | echo $csv | Out-File $outFile | |
| 92 | echo $msg | |
| 93 | ||
| 94 | # :TODO: Send warning email |