Guest User

Untitled

a guest
Nov 22nd, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # --- Begin Configuration --- #
  2.  
  3. # File path to store disk space usage CSV
  4. $outFile = "C:\disks.txt"
  5.  
  6. # Set delta threshold in percentage
  7. # E.g.: 5 means a change in free space of more than 5% will trigger warning
  8. $deltaThresholdPercent = 5
  9.  
  10. # Set delta threshold in GB
  11. # E.g.: 5 means a change in free space of more than 5 GB will trigger warning
  12. $deltaThresholdSpace = 5
  13.  
  14. # Absolute threshold in percentage
  15. # E.g.: 20 means total free space below 20% will trigger warning
  16. $absoluteThresholdPercent = 20
  17.  
  18. # Absolute threshold in GB
  19. # E.g.: 10 means total free space below 10GB will trigger warning
  20. $absoluteThresholdSpace = 10
  21.  
  22. # Email recipients
  23. [String[]] $recipients = "[email protected]"
  24.  
  25. # Email address that reports originate from
  26. $fromEmail = "[email protected]"
  27.  
  28. # SMTP server name or IP address
  29. $mailServer = "127.0.0.1"
  30.  
  31. # --- End Configuration --- #
  32.  
  33.  
  34.  
  35.  
  36. # Initialize warning; assume false
  37. $warning = $false
  38.  
  39. # Get hostname
  40. $computerName = Get-Content Env:\COMPUTERNAME
  41.  
  42. # Email subject line
  43. $subj = $computerName + ": Disk Usage Warning"
  44.  
  45. # Load all local volumes
  46. $disks = Get-WmiObject Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
  47.  
  48. # Iterate over volumes
  49. foreach ($disk in $disks)
  50. {
  51.     $volumeLetter = $disk.DeviceID
  52.     $totalSize = ($disk.Size/1GB)
  53.  
  54.     [double] $currentFreeSpace = [System.Math]::Round(($disk.FreeSpace/1GB), 2)
  55.     [double] $currentFreePercent = [System.Math]::Round(($currentFreeSpace/$totalSize) * 100, 2)
  56.  
  57.     # Run absolute free space check
  58.     if ($currentFreePercent -le $absoluteThresholdPercent -or $currentFreeSpace -le $absoluteThresholdSpace)
  59.     {
  60.         # Trigger warning
  61.         $warning = $true
  62.         $msg += $volumeLetter + " has " + $currentFreeSpace + "GB (" + $currentFreePercent + "%) free disk space remaining.`r`n`r`n"
  63.         $msg += "Warning threshold: " + $absoluteThresholdSpace + "GB or " + $absoluteThresholdPercent + "%.`r`n`r`n"
  64.     }
  65.  
  66.     # Don't run relative check if comparison data doesn't exist
  67.     if ((Test-Path $outFile) -eq $true)
  68.     {
  69.         # Read CSV
  70.         $csvIn = Import-Csv $outFile -Header("volume","freeSpace")
  71.         foreach ($row in $csvIn)
  72.         {
  73.             if ($row.volume -eq $volumeLetter)
  74.             {
  75.                 [double] $oldFreePercent = [System.Math]::Round(($row.freeSpace/$totalSize) * 100, 2)
  76.                 [double] $oldFreeSpace = $row.freeSpace
  77.             }
  78.         }
  79.        
  80.         # Calculate deltas
  81.         [double] $deltaFreePercent = [System.Math]::Round((([math]::abs($currentFreeSpace - $oldFreeSpace))/$totalSize) * 100, 2)
  82.         [double] $deltaFreeSpace = [System.Math]::Round(([math]::abs($currentFreeSpace - $oldFreeSpace)), 2)
  83.        
  84.         # Run relative free space check
  85.         if (($deltaFreePercent -ge $deltaThresholdPercent) -or ($deltaFreeSpace -ge $deltaThresholdSpace))
  86.         {
  87.             $warning = $true
  88.             $lastWriteTime = (Get-Item $outFile).LastWriteTime
  89.             $msg += $volumeLetter + " has lost " + $deltaFreeSpace + "GB (" + $deltaFreePercent + "%) free space since " + $lastWriteTime + "`r`n"
  90.             $msg += $volumeLetter + " currently has " + $currentFreeSpace + "GB (" + $currentFreePercent + "%) free space.`r`n`r`n"
  91.             $msg += "Warning threshold: " + $deltaThresholdSpace + "GB (or " + $deltaThresholdPercent + "%) loss since last run.`r`n`r`n"
  92.         }
  93.     }
  94.    
  95.     # Update CSV with new disk usage
  96.     $csv += $volumeLetter + "," + $currentFreeSpace + "`r`n"
  97. }
  98. echo $csv | Out-File $outFile
  99.  
  100. function SendEmail
  101. {
  102.     Param(
  103.         [String] $from, [String[]] $to,
  104.         [String] $subject, [String] $body,
  105.         [String] $smtpServer = "127.0.0.1"
  106.     )
  107.  
  108.     $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
  109.     $email = New-Object Net.Mail.MailMessage
  110.  
  111.     foreach($address in $to) {
  112.         $email.To.Add($address)
  113.     }
  114.     $email.From = $from
  115.     $email.Subject = $subject
  116.     $email.Body = $body
  117.     $smtp.Send($email)
  118. }
  119.  
  120. # Send warning email
  121. if ($warning -eq $true)
  122. {
  123.     SendEmail $fromEmail $recipients $subj $msg $mailServer
  124. }
Advertisement
Add Comment
Please, Sign In to add comment