Page 1 of 1

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

Posted: Wed Dec 09, 2015 7:25 am
by miketartaglia
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 9: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 10:27 am
by miketartaglia
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 10: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 10:43 am
by dan.potter

Code: Select all

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

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

Posted: Wed Dec 09, 2015 11:21 am
by miketartaglia
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 12:00 pm
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.

Code: Select all

]$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.