Disk Space


posted by clayman2
08-24-2010

Downloads: 1,080
File size: 7kB
Views: 4,158

Embed
Disk Space
  1. # Change the following variables based on your environment 
  2. $html_file_dir = "C:\PS_Scripts" 
  3. $server_file = "C:\PS_Scripts\servers.txt" 
  4. $from_address = "myemail@something.com" 
  5. $to_address = "whereiwanttosendit@something.com" 
  6. $email_gateway = "smtp.something.com" # Can either be DNS name or IP address to SMTP server 
  7. # The seventh line from the bottom (line 167) is used if your smtp gateway requires authentication. If you require smtp authentication 
  8. # you must uncomment it and set the following variables. 
  9. $smtp_user = "" 
  10. $smtp_pass = "" 
  11.  
  12. # Change the following variables for the style of the report. 
  13. $background_color = "rgb(140,166,193)" # can be in rgb format (rgb(0,0,0)) or hex format (#FFFFFF) 
  14. $server_name_font = "Arial" 
  15. $server_name_font_size = "20px" 
  16. $server_name_bg_color = "rgb(77,108,145)" # can be in rgb format (rgb(0,0,0)) or hex format (#FFFFFF) 
  17. $heading_font = "Arial" 
  18. $heading_font_size = "14px" 
  19. $heading_name_bg_color = "rgb(95,130,169)" # can be in rgb format (rgb(0,0,0)) or hex format (#FFFFFF) 
  20. $data_font = "Arial" 
  21. $data_font_size = "11px" 
  22.  
  23. # Colors for space 
  24. $very_low_space = "rgb(255,0,0)" # very low space equals anything in the MB 
  25. $low_space = "rgb(251,251,0)" # low space is less then or equal to 10 GB 
  26. $medium_space = "rgb(249,124,0)" # medium space is less then or equal to 100 GB 
  27.  
  28. ########################################################################### 
  29. #### NO CHANGES SHOULD BE MADE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING 
  30. ########################################################################### 
  31. # Define some variables 
  32. $ErrorActionPreference = "SilentlyContinue" 
  33. $date = Get-Date -UFormat "%Y%m%d" 
  34. $html_file = New-Item -ItemType File -Path "$html_file_dir\DiskSpace_$date.html" -Force 
  35. # Create the file 
  36. $html_file 
  37.  
  38. # Function to be used to convert bytes to MB or GB or TB 
  39. Function ConvertBytes { 
  40.     param($size
  41.     If ($size -lt 1MB) { 
  42.         $drive_size = $size / 1KB 
  43.         $drive_size = [Math]::Round($drive_size, 2) 
  44.         [string]$drive_size + ' KB' 
  45.     }elseif ($size -lt 1GB){ 
  46.         $drive_size = $size / 1MB 
  47.         $drive_size = [Math]::Round($drive_size, 2) 
  48.         [string]$drive_size + ' MB' 
  49.     }ElseIf ($size -lt 1TB){  
  50.         $drive_size = $size / 1GB 
  51.         $drive_size = [Math]::Round($drive_size, 2) 
  52.         [string]$drive_size + ' GB' 
  53.     }Else
  54.         $drive_size = $size / 1TB 
  55.         $drive_size = [Math]::Round($drive_size, 2) 
  56.         [string]$drive_size + ' TB' 
  57.     
  58.  
  59. # Create the header and footer contents of the html page for output 
  60. $html_header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  61. <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml"> 
  62. <head> 
  63. <title>Server Drive Space</title> 
  64. <style type="text/css"> 
  65.     .serverName { text-align:center; font-family:"' + $server_name_font + '"; font-size:' + $server_name_font_size +
  66.         '; font-weight:bold; background-color: ' + $server_name_bg_color + '; border: 1px solid black; width: 150px; } 
  67.     .headings { text-align:center; font-family:"' + $heading_font + '"; font-size:' + $heading_font_size +
  68.         '; font-weight:bold; background-color: ' + $heading_name_bg_color + '; border: 1px solid black; width: 150px; } 
  69.     .data { font-family:"' + $data_font + '"; font-size:' + $data_font_size + '; border: 1px solid black; width: 150px; } 
  70.     #dataTable { border: 1px solid black; border-collapse:collapse; } 
  71.     body { background-color: ' + $background_color + '; } 
  72.     #legend { border: 1px solid black; position:absolute; right:500px; top:10px; } 
  73. </style> 
  74. <script language="JavaScript" type="text/javascript"> 
  75. <!-- 
  76.  
  77. function zxcWWHS(){ 
  78. if (document.all){ 
  79.   zxcCur=''hand''; 
  80.   zxcWH=document.documentElement.clientHeight; 
  81.   zxcWW=document.documentElement.clientWidth; 
  82.   zxcWS=document.documentElement.scrollTop; 
  83.   if (zxcWH==0){ 
  84.    zxcWS=document.body.scrollTop; 
  85.    zxcWH=document.body.clientHeight; 
  86.    zxcWW=document.body.clientWidth; 
  87.   } 
  88. } 
  89. else if (document.getElementById){ 
  90.   zxcCur=''pointer''; 
  91.   zxcWH=window.innerHeight-15; 
  92.   zxcWW=window.innerWidth-15; 
  93.   zxcWS=window.pageYOffset; 
  94. } 
  95. zxcWC=Math.round(zxcWW/2); 
  96. return [zxcWW,zxcWH,zxcWS]; 
  97. } 
  98.  
  99.  
  100. window.onscroll=function(){ 
  101. var img=document.getElementById(''legend''); 
  102. if (!document.all){ img.style.position=''fixed''; window.onscroll=null; return; } 
  103. if (!img.pos){ img.pos=img.offsetTop; } 
  104. img.style.top=(zxcWWHS()[2]+img.pos)+''px''; 
  105. } 
  106. //--> 
  107. </script> 
  108. </head> 
  109. <body>' 
  110.  
  111. $html_footer = '</body> 
  112. </html>' 
  113.  
  114. # Start to create the reports file 
  115. Add-Content $html_file $html_header 
  116.  
  117. # Retrieve the contents of the server.txt file, this file should contain either the 
  118. # ip address or the host name of the machine on a single line. Loop through the file 
  119. # and get the drive information. 
  120. Get-Content $server_file |` 
  121.     ForEach-Object {  
  122.         # Get the hostname of the machine 
  123.         $hostname = Get-WmiObject -Impersonation Impersonate -ComputerName $_ -Query "SELECT Name From Win32_ComputerSystem" 
  124.         $name = $hostname.Name.ToUpper() 
  125.         Add-Content $html_file ('<Table id="dataTable"><tr><td colspan="3" class="serverName">' + $name + '</td></tr> 
  126.         <tr><td class="headings">Drive Letter</td><td class="headings">Total Size</td><td class="headings">Free Space</td></tr>'
  127.          
  128.         # Get the drives of the server 
  129.         $drives = Get-WmiObject Win32_LogicalDisk -Filter "drivetype=3" -ComputerName $_ -Impersonation Impersonate 
  130.          
  131.         # Now that I have all the drives, loop through and add to report 
  132.         ForEach ($drive in $drives) { 
  133.             $space_color = "" 
  134.             $free_space = $drive.FreeSpace 
  135.             If ($free_space -le 1073741824) { 
  136.                 $space_color = $very_low_space 
  137.             }elseif ($free_space -le 10737418240) { 
  138.                 $space_color = $low_space 
  139.             }elseif ($free_space -le 107374182400) { 
  140.                 $space_color = $medium_space 
  141.             
  142.                  
  143.             Add-Content $html_file ('<tr><td class="data">' + $drive.deviceid + '</td><td class="data">' + (ConvertBytes $drive.size) +
  144.                 '</td><td class="data" bgcolor="' + $space_color + '">' + (ConvertBytes $drive.FreeSpace) + '</td></tr>'
  145.         
  146.         # Close the table 
  147.         Add-Content $html_file ('</table></br><div id="legend"> 
  148.             <Table><tr><td style="font-size:12px">Less then or equal to 1 GB</td><td bgcolor="' + $very_low_space + '" width="10px"></td></tr> 
  149.                 <tr><td style="font-size:12px">Less then or equal to 10 GB</td><td bgcolor="' + $low_space + '" width="10px"></td></tr> 
  150.                 <tr><td style="font-size:12px">Less then or equal to 100 GB</td><td bgcolor="' + $medium_space + '" width="10px"></td></tr> 
  151.             </table></div>'
  152.     
  153.      
  154. # End the reports file 
  155. Add-Content $html_file $html_footer 
  156.  
  157. # Email the file 
  158. $mail = New-Object System.Net.Mail.MailMessage 
  159. $att = new-object Net.Mail.Attachment($html_file
  160. $mail.From = $from_address 
  161. $mail.To.Add($to_address
  162. $mail.Subject = "Server Diskspace $date" 
  163. $mail.Body = "The diskspace report file is attached." 
  164. $mail.Attachments.Add($att
  165. $smtp = New-Object System.Net.Mail.SmtpClient($email_gateway
  166. #$smtp.Credentials = New-Object System.Net.NetworkCredential($smtp_user,$smtp_pass) 
  167.  
  168. $smtp.Send($mail
  169. $att.Dispose() 
  170.  
  171. # Delete the file 
  172. Remove-Item $html_file 

The following script will connect to a list a servers provided from a text file, it will return the disk size and free space of the drives. It will create a HTML report and email the file. You must create a file with either the name of the servers or IP addresses line by line. Within the script there are a few variables that will need to be modified, and some varibales to change the look of the HTML report.

Comments

hortiz wrote re: Disk Space
on 12-12-2011 10:09 AM

Excellent script, Its working Fine for me :D

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.