Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # --- Begin Configuration --- #
- # File path to store disk space usage CSV
- $outFile = "C:\disks.txt"
- # Set delta threshold in percentage
- # E.g.: 5 means a change in free space of more than 5% will trigger warning
- $deltaThresholdPercent = 5
- # Set delta threshold in GB
- # E.g.: 5 means a change in free space of more than 5 GB will trigger warning
- $deltaThresholdSpace = 5
- # Absolute threshold in percentage
- # E.g.: 20 means total free space below 20% will trigger warning
- $absoluteThresholdPercent = 20
- # Absolute threshold in GB
- # E.g.: 10 means total free space below 10GB will trigger warning
- $absoluteThresholdSpace = 10
- # Email recipients
- # Email address that reports originate from
- # SMTP server name or IP address
- $mailServer = "127.0.0.1"
- # --- End Configuration --- #
- # Initialize warning; assume false
- $warning = $false
- # Get hostname
- $computerName = Get-Content Env:\COMPUTERNAME
- # Email subject line
- $subj = $computerName + ": Disk Usage Warning"
- # Load all local volumes
- $disks = Get-WmiObject Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
- # Iterate over volumes
- foreach ($disk in $disks)
- {
- $volumeLetter = $disk.DeviceID
- $totalSize = ($disk.Size/1GB)
- [double] $currentFreeSpace = [System.Math]::Round(($disk.FreeSpace/1GB), 2)
- [double] $currentFreePercent = [System.Math]::Round(($currentFreeSpace/$totalSize) * 100, 2)
- # Run absolute free space check
- if ($currentFreePercent -le $absoluteThresholdPercent -or $currentFreeSpace -le $absoluteThresholdSpace)
- {
- # Trigger warning
- $warning = $true
- $msg += $volumeLetter + " has " + $currentFreeSpace + "GB (" + $currentFreePercent + "%) free disk space remaining.`r`n`r`n"
- $msg += "Warning threshold: " + $absoluteThresholdSpace + "GB or " + $absoluteThresholdPercent + "%.`r`n`r`n"
- }
- # Don't run relative check if comparison data doesn't exist
- if ((Test-Path $outFile) -eq $true)
- {
- # Read CSV
- $csvIn = Import-Csv $outFile -Header("volume","freeSpace")
- foreach ($row in $csvIn)
- {
- if ($row.volume -eq $volumeLetter)
- {
- [double] $oldFreePercent = [System.Math]::Round(($row.freeSpace/$totalSize) * 100, 2)
- [double] $oldFreeSpace = $row.freeSpace
- }
- }
- # Calculate deltas
- [double] $deltaFreePercent = [System.Math]::Round((([math]::abs($currentFreeSpace - $oldFreeSpace))/$totalSize) * 100, 2)
- [double] $deltaFreeSpace = [System.Math]::Round(([math]::abs($currentFreeSpace - $oldFreeSpace)), 2)
- # Run relative free space check
- if (($deltaFreePercent -ge $deltaThresholdPercent) -or ($deltaFreeSpace -ge $deltaThresholdSpace))
- {
- $warning = $true
- $lastWriteTime = (Get-Item $outFile).LastWriteTime
- $msg += $volumeLetter + " has lost " + $deltaFreeSpace + "GB (" + $deltaFreePercent + "%) free space since " + $lastWriteTime + "`r`n"
- $msg += $volumeLetter + " currently has " + $currentFreeSpace + "GB (" + $currentFreePercent + "%) free space.`r`n`r`n"
- $msg += "Warning threshold: " + $deltaThresholdSpace + "GB (or " + $deltaThresholdPercent + "%) loss since last run.`r`n`r`n"
- }
- }
- # Update CSV with new disk usage
- $csv += $volumeLetter + "," + $currentFreeSpace + "`r`n"
- }
- echo $csv | Out-File $outFile
- function SendEmail
- {
- Param(
- [String] $from, [String[]] $to,
- [String] $subject, [String] $body,
- [String] $smtpServer = "127.0.0.1"
- )
- $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
- $email = New-Object Net.Mail.MailMessage
- foreach($address in $to) {
- $email.To.Add($address)
- }
- $email.From = $from
- $email.Subject = $subject
- $email.Body = $body
- $smtp.Send($email)
- }
- # Send warning email
- if ($warning -eq $true)
- {
- SendEmail $fromEmail $recipients $subj $msg $mailServer
- }
Advertisement
Add Comment
Please, Sign In to add comment