Page 1 of 1

Need to send email only when free space is under 500GB

Posted: Wed Dec 09, 2015 6:25 am
by mtartaglia
Here is the code so far. Any help is much appreciated.

$TotalGB = @{ Name = "Capacity(GB)"; expression = { [math]::round(($_.Capacity/ 1073741824), 2) } }
$FreeGB = @{ Name = "FreeSpace(GB)"; expression = { [math]::round(($_.FreeSpace / 1073741824), 2) } }
$FreePerc = @{ Name = "Free(%)"; expression = { [math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100), 0) } }

function get-mountpoints
{
$volumes = Get-WmiObject -computer $server win32_volume | Where-object { $_.DriveLetter -eq $null }

$volumes | Select SystemName, Label, Flag, $TotalGB, $FreeGB, $FreePerc | where { $_.Label -like '*Logs' } | Format-Table -AutoSize
}

$servers = (Get-Content .\servers.txt)

foreach ($server in $servers)

{
get-mountpoints
}

Re: Need to send email only when free space is under 500GB

Posted: Wed Dec 09, 2015 8:00 am
by jvierra
Hi Mike - Start by reviewing the following.

Help Send-MailMessage -FULL
HELP about_if

Re: Need to send email only when free space is under 500GB

Posted: Wed Dec 09, 2015 9:27 am
by mtartaglia
Actually I'm more concerned with how I do it with the logic. I want it to create an email when free space is under 500GB. I just haven't put the email part of the code in yet.

Re: Need to send email only when free space is under 500GB

Posted: Wed Dec 09, 2015 9:39 am
by jvierra
miketartaglia wrote:Actually I'm more concerned with how I do it with the logic. I want it to create an email when free space is under 500GB. I just haven't put the email part of the code in yet.
What have you tried? Have you read the help I posted?

Re: Need to send email only when free space is under 500GB

Posted: Wed Dec 09, 2015 9:43 am
by dan.potter
  1. $TotalGB = @{ Name = "Capacity(GB)"; expression = { [math]::round(($_.Capacity/ 1073741824), 2) } }
  2. $FreeGB = @{ Name = "FreeSpace(GB)"; expression = { [math]::round(($_.FreeSpace / 1073741824), 2) } }
  3. $FreePerc = @{ Name = "Free(%)"; expression = { [math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100), 0) } }
  4.  
  5. function get-mountpoints
  6. {
  7. $volumes = Get-WmiObject win32_volume | Where-object { $_.DriveLetter -eq 'c:' }
  8.  
  9. $volumes | Select SystemName, Label, Flag, $TotalGB, $FreeGB, $FreePerc
  10. }
  11.  
  12.  
  13. PS H:\> ((get-mountpoints).'FreeSpace(GB)' -lt 500)
  14. True
  15. PS H:\> ((get-mountpoints).'FreeSpace(GB)' -lt 300)
  16. False
  17. PS H:\>
  18.  
  19.  
  20. if((get-mountpoints).'FreeSpace(GB)' -lt 500){ send email}

Re: Need to send email only when free space is under 500GB

Posted: Wed Dec 09, 2015 10:21 am
by mtartaglia
Revised code. Sends the email no matter what.


$TotalGB = @{ Name = "Capacity(GB)"; expression = { [math]::round(($_.Capacity/ 1073741824), 2) } }
$FreeGB = @{ Name = "FreeSpace(GB)"; expression = { [math]::round(($_.FreeSpace / 1073741824), 2) } }
$FreePerc = @{ Name = "Free(%)"; expression = { [math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100), 0) } }

function get-mountpoints
{
$volumes = Get-WmiObject -computer $server win32_volume | Where-object { $_.DriveLetter -eq $null }

$volumes | Select SystemName, Label, Flag, $TotalGB, $FreeGB, $FreePerc | where { $_.Label -like '*Logs' } | Format-Table -AutoSize
}


$servers = (Get-Content .\servers.txt)

foreach ($server in $servers)
{
get-mountpoints
if ((get-mountpoints).'FreeSpace(GB)' -lt 500)
{
$email = @{
From = "DiskSpaceReport@domain.com"
To = "userid@domian.COM"
Subject = "LOG ALERT for $server"
SMTPServer = "smtp.domian.com"
Body = "Server log volume is filling up"

}
send-mailmessage @email
}

}

Re: Need to send email only when free space is under 500GB

Posted: Wed Dec 09, 2015 11:00 am
by jvierra
When you copy scripts and attempt to use them without understanding what they do you will likely always get into this corner.

The following is effectively what you are trying to do. YOu will need to learn enough PowerShell to debug this as I expect the filter may not be correct for your installation.
  1. ]$servers = Get-Content .\servers.txt
  2. foreach ($server in $servers) {
  3.     if ($volume = Get-WmiObject -computer $server win32_volume -Filter 'DriveLetter IS Null AND Label like "%logs%"') {
  4.         if ($volume.Freespace -lt 500Gb) {
  5.             $body = 'Server log volume is filling up'
  6.             Send-MailMessage -Subject "LOG ALERT for $server" -Body $body -From DiskSpaceReport@bjs.com -To mtartaglia@BJS.COM -SMTPServer smtp.bjs.com
  7.         }
  8.     }
  9. }
Notice that most of your coed is either not used or not required to send the mail you are trying to send.