Disk Space Monitor


posted by Colin Smith
02-25-2009

Downloads: 2,400
File size: 2.7kB
Views: 15,928

Embed
Disk Space Monitor
  1. #*============================================================================= 
  2. #* Script Name: Diskmonitor.ps1 
  3. #* Created:     02-25-2009 
  4. #* Author:      Colin Smiht 
  5. #* Company:      
  6. #* Email:       
  7. #* Web:         http://sysadminsmith.com    
  8. #*============================================================================= 
  9. #* Purpose:  This script will gather disk information about all the servers listed in your computerlistall file. This is a csv file. 
  10. #*           It will also send out notifications via email to anyone you want based on low disk parameters that you provide 
  11. #*              
  12. #*============================================================================= 
  13.  
  14. ########################################## 
  15. ###        Gather Disk Information       ### 
  16. ########################################## 
  17. Clear-Content "D:\Scripts\Powershell\PAC\lowdisk.txt" 
  18. $i =
  19. $users = "some.email@address.com" 
  20. $computers = import-csv "D:\Scripts\computerlistall.txt" 
  21. echo "ServerName        Drive Letter    Drive Size    Free Space    Percent Free" >> "D:\Scripts\Powershell\PAC\lowdisk.txt" 
  22. echo "----------        ------------    ----------    ----------    ------------" >> "D:\Scripts\Powershell\PAC\lowdisk.txt" 
  23. foreach ($line in $computers
  24.     $computer = $line.hostname 
  25.     $ip = $line.ip 
  26.     $computer 
  27.     $ip 
  28.     
  29.     $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} 
  30.     foreach($drive in $drives
  31.         $size1 = $drive.size / 1GB  
  32.         $size1 
  33.         $size = "{0:N2}" -f $size1 
  34.         $size 
  35.         $free1 = $drive.freespace / 1GB 
  36.         $free = "{0:N2}" -f $free1 
  37.         $ID = $drive.DeviceID 
  38.         $a = $free1 / $size1 * 100 
  39.         $b = "{0:N2}" -f $a 
  40.  
  41. ############################################## 
  42. ##    Determine if any disks low    ## 
  43. ############################################## 
  44.  
  45.         if (($ID -eq "D:") -or ($ID -eq "S:") -or ($ID -eq "T:") -or ($ID -eq "C:") -and ($free1 -lt 1)) 
  46.         
  47.             echo "$computer        $ID            $size        $free        $b" >> "D:\Scripts\Powershell\PAC\lowdisk.txt" 
  48.             $i++ 
  49.             #[char]10 | Out-File -append ./low.txt 
  50.         
  51.     
  52.  
  53.  
  54. #################################################### 
  55.  
  56. ##    Send Notification if alert $i is greater then 0         ## 
  57.  
  58. #################################################### 
  59.  
  60. if ($i -gt 0) 
  61.  
  62.  
  63.     foreach ($user in $users
  64.  
  65.  
  66.         echo �Sending Email notification ro $user� 
  67.  
  68.         $smtpServer = �smtp server� 
  69.  
  70.         $smtp = New-Object Net.Mail.SmtpClient($smtpServer
  71.  
  72.         $emailFrom = �fromuser@domain.com� 
  73.  
  74.        $subject = �Email Subject� 
  75.  
  76.         foreach ($line in Get-Content �D:\Scripts\lowdisk.txt�) 
  77.  
  78.  
  79.             $body +=$line `n� 
  80.  
  81.         
  82.  
  83.         $smtp.Send($EmailFrom,$user,$subject,$body
  84.  
  85.         $body = �� 
  86.  
  87.     
  88.  

This is a script that will monitor the physical drives of any machines that you have in an input file. It will send out notifcations if the free space is below a threshhold that is defined by you.

Comments

Bob Morcus - ITvention wrote re: Disk Space Monitor
on 07-23-2010 3:32

Hi,

Script was monitoring only last entry in computerinstall.txt.

Changed line 29 cut char:  } and place  }  on line 42.

Now it's working perfect.

mschomm wrote re: Disk Space Monitor
on 08-13-2010 8:00

Needs some work. Paths and names for input and output files are hard coded as literals throughout the script.

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