Support Forums

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

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
User avatar
miketartaglia
Posts: 38
Joined: Mon Feb 02, 2015 7:44 am

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

Postby miketartaglia » Wed Dec 09, 2015 7:25 am

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
}
User avatar
jvierra
Posts: 9725
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Wed Dec 09, 2015 9:00 am

Hi Mike - Start by reviewing the following.

Help Send-MailMessage -FULL
HELP about_if
User avatar
miketartaglia
Posts: 38
Joined: Mon Feb 02, 2015 7:44 am

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

Postby miketartaglia » Wed Dec 09, 2015 10:27 am

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.
User avatar
jvierra
Posts: 9725
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Wed Dec 09, 2015 10:39 am

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: 632
Joined: Wed Oct 08, 2014 6:02 am

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

Postby dan.potter » Wed Dec 09, 2015 10:43 am

$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 win32_volume | Where-object { $_.DriveLetter -eq 'c:' }
 
$volumes | Select SystemName, Label, Flag, $TotalGB, $FreeGB, $FreePerc
}
 
 
PS H:\> ((get-mountpoints).'FreeSpace(GB)' -lt 500)
True
PS H:\> ((get-mountpoints).'FreeSpace(GB)' -lt 300)
False
PS H:\>
 
 
if((get-mountpoints).'FreeSpace(GB)' -lt 500){ send email}
User avatar
miketartaglia
Posts: 38
Joined: Mon Feb 02, 2015 7:44 am

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

Postby miketartaglia » Wed Dec 09, 2015 11:21 am

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
}

}
User avatar
jvierra
Posts: 9725
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Wed Dec 09, 2015 12:00 pm

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.

]$servers = Get-Content .\servers.txt
foreach ($server in $servers) {
if ($volume = Get-WmiObject -computer $server win32_volume -Filter 'DriveLetter IS Null AND Label like "%logs%"') {
if ($volume.Freespace -lt 500Gb) {
$body = 'Server log volume is filling up'
Send-MailMessage -Subject "LOG ALERT for $server" -Body $body -From DiskSpaceReport@bjs.com -To mtartaglia@BJS.COM -SMTPServer smtp.bjs.com
}
}
}


Notice that most of your coed is either not used or not required to send the mail you are trying to send.

Return to “Windows PowerShell”

Who is online

Users browsing this forum: unseenfeeling and 1 guest