Daily Disk Space Checker to Excel 2


posted by liquidstone
07-06-2010

Downloads: 765
File size: 2.4kB
Views: 4,008

Embed
Daily Disk Space Checker to Excel 2
  1. $ErrorActionPreference = "silentlycontinue" 
  2. $a = New-Object -comobject Excel.Application 
  3. $a.visible = $True 
  4.  
  5. $b = $a.Workbooks.Add() 
  6. $c = $b.Worksheets.Item(1) 
  7.  
  8. $c.Cells.Item(1,1) = "Machine Name  " 
  9.  
  10. $d = $c.UsedRange 
  11. $d.Interior.ColorIndex = 19 
  12. $d.Font.ColorIndex = 11 
  13. $d.Font.Bold = $True 
  14. $date = get-date -uformat "%Y%m%d" 
  15. $save = "c:\powershell\diskspace_$date.xls" 
  16.  
  17. $intRow =
  18. $exrow =
  19. $VMrow =
  20.  
  21. $pass = cat c:\powershell\pw\pw1.txt | ConvertTo-SecureString 
  22. $mycred1 = New-Object -TypeName system.Management.Automation.PSCredential -ArgumentList "domain\administrator",$pass 
  23.  
  24.  
  25.     get-content c:\powershell\files\servers.txt | ForEach-Object
  26.     gwmi win32_logicaldisk -Filter "drivetype = 3" -ComputerName $_ -Credential $mycred1 }  | foreach-object {  
  27.     if($_.__server -eq $server
  28.         {$exrow = $exrow +
  29.         $introw = $introw-2} 
  30.     else
  31.         $exrow = 2} 
  32.      
  33.     $server = $_.__server 
  34.     $c.Cells.Item($intRow,$exrow).Interior.ColorIndex =
  35.         $c.Cells.Item($intRow, 1) = $server 
  36.         $c.cells.item($introw, $exrow ) = $_.deviceid, '{0:0.0}GB' -f ($_.size / 1GB) 
  37.         $c.Cells.Item($intRow+1, $exrow) = '{0:0.0}GB' -f ($_.freespace / 1GB) 
  38.          
  39.                 if (($_.freespace / 1GB) -le 10) 
  40.         {$c.Cells.Item($intRow+1, $exrow).Interior.ColorIndex = 45} 
  41.          
  42.             if (($_.freespace / 1GB) -le 2) 
  43.             {$c.Cells.Item($intRow+1, $exrow).Interior.ColorIndex = 29} 
  44.          
  45.                 if (($_.freespace / 1GB) -le 1) 
  46.                 {$c.Cells.Item($intRow+1, $exrow).Interior.ColorIndex = 3} 
  47.                  
  48.                 $introw = $introw +
  49.  
  50. $c.cells.item($introw+1, 1) = "10GB or less" 
  51. $c.Cells.Item($intRow+1, 1).Interior.ColorIndex = 45 
  52. $c.cells.item($introw+2, 1) = "2GB or less" 
  53. $c.Cells.Item($intRow+2,1).Interior.ColorIndex = 29 
  54. $c.cells.item($introw+3, 1) = "1GB or less"  
  55. $c.Cells.Item($intRow+3,1).Interior.ColorIndex =
  56.  
  57. $d.EntireColumn.AutoFit() 
  58.  
  59. $b.SaveAs($save
  60. $a.quit() 
  61.  
  62. start-sleep -s 15 
  63.  
  64. $mail = New-Object System.Net.Mail.MailMessage 
  65. $att = new-object Net.Mail.Attachment($save
  66. $mail.From = "sender <sender@e-mail.com>" 
  67. $mail.To.Add("receiver@e-mail.com"
  68. $mail.Subject = "Diskspace $date" 
  69. $mail.Body = "The log file is attached" 
  70. $mail.Attachments.Add($att
  71. $smtp = New-Object System.Net.Mail.SmtpClient("mail-server"
  72. $smtp.Credentials = New-Object System.Net.NetworkCredential("domain\user","P@Ssw0rD"
  73.  
  74. $smtp.Send($mail
  75. $att.Dispose() 
  76.  
  77. remove-item "c:\powershell\diskspace.xls" 
  78.  
  79.  
  80. exit 

I use this script to daily check the diskspace on or hosts.

It writes out to excel and wil sent de results in a e-mail.

You need a .txt file with the ip adresses, and you need to create a securestring with your credentials.

(read-host -assecurestring | convertfrom-securestring | out-file C:\powershell\pw\pw1.txt)

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