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

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

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

Post 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
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

Hi Mike - Start by reviewing the following.

Help Send-MailMessage -FULL
HELP about_if
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

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

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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?
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

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

Post 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}
User avatar
mtartaglia
Posts: 101
Last visit: Mon Dec 19, 2022 11:45 am

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

Post 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
}

}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
This topic is 8 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked