Guest User

Disk Usage Monitor

a guest
Dec 28th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #############################################################################
  2. #                                                                           #
  3. #  Check disk space and send an HTML report as the body of an email.        #
  4. #  Reports only disks on computers that have low disk space.                #
  5. #  Author: Mike Carmody                                                     #
  6. #  Some ideas extracted from Thiyagu's Exchange DiskspaceHTMLReport module. #
  7. #  Date: 8/10/2011                                                          #
  8. #  I have not added any error checking into this script yet.                #
  9. #                                                                           #
  10. #                                                                           #
  11. #############################################################################
  12. # Continue even if there are errors
  13. $ErrorActionPreference = "Continue";
  14.  
  15. #########################################################################################
  16. # Items to change to make it work for you.
  17. #
  18. # EMAIL PROPERTIES
  19. #  - the $users that this report will be sent to.
  20. #  - near the end of the script the smtpserver, From and Subject.
  21.  
  22. # REPORT PROPERTIES
  23. #  - you can edit the report path and report name of the html file that is the report.
  24. #########################################################################################
  25.  
  26. # Set your warning and critical thresholds
  27. $percentWarning = 15;
  28. $percentCritcal = 10;
  29.  
  30. # EMAIL PROPERTIES
  31.     # Set the recipients of the report.
  32.         $users = "YourDistrolist@company.com"
  33.         #$users = "You@company.com" # I use this for testing by uing my email address.
  34.         #$users = "you@company.com", "manager@company.com", "etc@company.com";  # can be sent to individuals.
  35.  
  36.  
  37. # REPORT PROPERTIES
  38.     # Path to the report
  39.         $reportPath = "D:\Jobs\DiskSpaceQuery\Reports\";
  40.  
  41.     # Report name
  42.         $reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html";
  43.  
  44. # Path and Report name together
  45. $diskReport = $reportPath + $reportName
  46.  
  47. #Set colors for table cell backgrounds
  48. $redColor = "#FF0000"
  49. $orangeColor = "#FBB917"
  50. $whiteColor = "#FFFFFF"
  51.  
  52. # Count if any computers have low disk space.  Do not send report if less than 1.
  53. $i = 0;
  54.  
  55. # Get computer list to check disk space
  56. $computers = Get-Content "servers_c.txt";
  57. $datetime = Get-Date -Format "MM-dd-yyyy_HHmmss";
  58.  
  59. # Remove the report if it has already been run today so it does not append to the existing report
  60. If (Test-Path $diskReport)
  61.     {
  62.         Remove-Item $diskReport
  63.     }
  64.  
  65. # Cleanup old files..
  66. $Daysback = "-7"
  67. $CurrentDate = Get-Date;
  68. $DateToDelete = $CurrentDate.AddDays($Daysback);
  69. Get-ChildItem $reportPath | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item;
  70.  
  71. # Create and write HTML Header of report
  72. $titleDate = get-date -uformat "%m-%d-%Y - %A"
  73. $header = "
  74.         <html>
  75.         <head>
  76.         <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
  77.         <title>DiskSpace Report</title>
  78.         <STYLE TYPE='text/css'>
  79.         <!--
  80.         td {
  81.             font-family: Tahoma;
  82.             font-size: 11px;
  83.             border-top: 1px solid #999999;
  84.             border-right: 1px solid #999999;
  85.             border-bottom: 1px solid #999999;
  86.             border-left: 1px solid #999999;
  87.             padding-top: 0px;
  88.             padding-right: 0px;
  89.             padding-bottom: 0px;
  90.             padding-left: 0px;
  91.         }
  92.         body {
  93.             margin-left: 5px;
  94.             margin-top: 5px;
  95.             margin-right: 0px;
  96.             margin-bottom: 10px;
  97.             table {
  98.             border: thin solid #000000;
  99.         }
  100.         -->
  101.         </style>
  102.         </head>
  103.         <body>
  104.         <table width='100%'>
  105.         <tr bgcolor='#CCCCCC'>
  106.         <td colspan='7' height='25' align='center'>
  107.         <font face='tahoma' color='#003399' size='4'><strong>AEM Environment DiskSpace Report for $titledate</strong></font>
  108.         </td>
  109.         </tr>
  110.         </table>
  111. "
  112.  Add-Content $diskReport $header
  113.  
  114. # Create and write Table header for report
  115.  $tableHeader = "
  116. <table width='100%'><tbody>
  117.     <tr bgcolor=#CCCCCC>
  118.    <td width='10%' align='center'>Server</td>
  119.     <td width='5%' align='center'>Drive</td>
  120.     <td width='15%' align='center'>Drive Label</td>
  121.     <td width='10%' align='center'>Total Capacity(GB)</td>
  122.     <td width='10%' align='center'>Used Capacity(GB)</td>
  123.     <td width='10%' align='center'>Free Space(GB)</td>
  124.     <td width='5%' align='center'>Freespace %</td>
  125.     </tr>
  126. "
  127. Add-Content $diskReport $tableHeader
  128.  
  129. # Start processing disk space reports against a list of servers
  130.   foreach($computer in $computers)
  131.     {  
  132.     $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3"
  133.     $computer = $computer.toupper()
  134.         foreach($disk in $disks)
  135.     {        
  136.         $deviceID = $disk.DeviceID;
  137.         $volName = $disk.VolumeName;
  138.         [float]$size = $disk.Size;
  139.         [float]$freespace = $disk.FreeSpace;
  140.         $percentFree = [Math]::Round(($freespace / $size) * 100, 2);
  141.         $sizeGB = [Math]::Round($size / 1073741824, 2);
  142.         $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
  143.         $usedSpaceGB = $sizeGB - $freeSpaceGB;
  144.         $color = $whiteColor;
  145.  
  146. # Set background color to Orange if just a warning
  147.     if($percentFree -lt $percentWarning)      
  148.         {
  149.        $color = $orangeColor   
  150.  
  151. # Set background color to Orange if space is Critical
  152.       if($percentFree -lt $percentCritcal)
  153.         {
  154.         $color = $redColor
  155.        }        
  156.  
  157.  # Create table data rows
  158.     $dataRow = "
  159.         <tr>
  160.        <td width='10%'>$computer</td>
  161.         <td width='5%' align='center'>$deviceID</td>
  162.         <td width='15%' >$volName</td>
  163.         <td width='10%' align='center'>$sizeGB</td>
  164.         <td width='10%' align='center'>$usedSpaceGB</td>
  165.         <td width='10%' align='center'>$freeSpaceGB</td>
  166.         <td width='5%' bgcolor=`'$color`' align='center'>$percentFree</td>
  167.         </tr>
  168. "
  169. Add-Content $diskReport $dataRow;
  170. Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree";
  171.     $i++       
  172.         }
  173.     }
  174. }
  175.  
  176. # Create table at end of report showing legend of colors for the critical and warning
  177.  $tableDescription = "
  178. </table><br><table width='20%'>
  179.     <tr bgcolor='White'>
  180.    <td width='10%' align='center' bgcolor='#FBB917'>Warning less than 15% free space</td>
  181.     <td width='10%' align='center' bgcolor='#FF0000'>Critical less than 10% free space</td>
  182.     </tr>
  183. "
  184.     Add-Content $diskReport $tableDescription
  185.     Add-Content $diskReport "</body></html>"
  186.  
  187. # Send Notification if alert $i is greater then 0
  188. if ($i -gt 0)
  189. {
  190.     foreach ($user in $users)
  191. {
  192.         Write-Host "Sending Email notification to $user"
  193.        
  194.         $smtpServer = "MySMTPServer"
  195.         $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
  196.         $msg = New-Object Net.Mail.MailMessage
  197.         $msg.To.Add($user)
  198.         $msg.From = "myself@company.com"
  199.         $msg.Subject = "Environment DiskSpace Report for $titledate"
  200.         $msg.IsBodyHTML = $true
  201.         $msg.Body = get-content $diskReport
  202.         $smtp.Send($msg)
  203.         $body = ""
  204.     }
  205.   }
Add Comment
Please, Sign In to add comment