How can I send an email when a function returns false?
Im trying to make this code work, but i still receive an email when the function is equal to false. Can anyone assist me in finding the issue? What the program does is look for servers that has a set limit (in GB) on specific drives.
#This Program goes through all the servers and informs any level below the limit
$ErrorActionPreference = "SilentlyContinue";
$scriptpath = $MyInvocation.MyCommand.Definition
$dir = Split-Path $scriptpath
#=================Function============
Function isLow($server, $drive, $limit)
{
$disk = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3 and DeviceID = '$drive'";
[float]$size = $disk.Capacity;
[float]$freespace = $disk.FreeSpace;
$sizeGB = [Math]::Round($size / 1073741824, 2);
$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
if($freeSpaceGB -lt $limit){
return $true;
}
else{
return $false;
}
}
#================Servers==============
#------------------###server1--------------
$server = "###server1";
$drive = "C:";
$lim = 25;
if(isLow $server $drive $lim)
{
$Alert += $server + " || " + $drive + " is low<br>"
}
$server = "###server1";
$drive = "D:";
$lim = 35;
if(isLow $server $drive $lim)
{
$Alert += $server + " || " + $drive + " is low<br>"
}
#-----------------###(more servers ect.)--------------
#================EMAIL===============
$smtpServer = "192.168.x.x"
$ReportSender = "sender@email.com"
$users = "user@email.com"
$MailSubject = "ALERT!!! Low DiskSpace"
foreach($user in $users){
if($true){
Write-Host "Sending Email notification to $user"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($user)
$msg.From = $ReportSender
$msg.Subject = $MailSubject
$msg.IsBodyHTML = $True
$msg.Body = $Alert
$smtp.Send($msg)
}
else($false){
Write-Host "No one is pass the limit"
}
}
December 19th, 2013 6:25pm
Try writing a small script that contains only the minimum amount of code needed to reproduce the problem.
Bill
December 19th, 2013 6:32pm
Hi,
if($true) is always going to be true. Try setting a variable to be true/false and then test for that instead.
If ($someVar) {send-mailmessage} Else {}
December 19th, 2013 6:34pm
Will try that, Bill
December 19th, 2013 6:52pm
Even if i set
If ($someVar) {send-mailmessage} Else {}
I still get an email with 0 servers not passing the limit, which is correct but i dont want to receive an email if 0 server are passing the limit.
I realize the problem is within the fuction
if($freeSpaceGB -lt $limit){
$email = "True";
}
else{
$email = "False";
}
Im gonna try out the strategy Bill suggested and see if i get that part working first.
December 19th, 2013 6:57pm
Even if i set
If ($someVar) {send-mailmessage} Else {}
<snip>
In that case, can't you just test on $email?
If ($email) {Send-MailMessage} Else {}
Now that I look at your code again, I don't see this $email variable anywhere.
December 19th, 2013 7:06pm
I've changed
if($freeSpaceGB -lt $limit){
return $true;
}
else{
return $false;
}
to
if($freeSpaceGB -lt $limit){
$email = "True";
}
else{
$email = "False";
}
Still get the same results
- Edited by
KillahHerb
Thursday, December 19, 2013 4:21 PM
December 19th, 2013 7:21pm
This is the code that gets the result:
if(isLow $server $drive $lim)
{
$Alert += $server + " || " + $drive + " is low<br>"
}
Here is how to test that.
if($alert){
# send mail
}else{
# don't send mail
}
December 19th, 2013 7:26pm
Eureka! wow over saw that completely -_-
Someone give this man a cookie ^
Thanks jrv!
December 19th, 2013 7:36pm
I'm still not sure whether or not the current version of isLow does what it is supposed to. If the script is still not working I suggest you re-post the whole thing.
a few other, unrelated suggestions:
- replace all instances of 1073741824 in the code with 1GB. This will make the intent more clear and it will be easier to be sure that the value is correct.
- put all of the server, drive, and lim values into a .csv file and replace all that repetitious code with a loop, somewhat along the lines of the (untested) code below. This will allow you to add or remove servers and drives, and change the limit values
without having to modify the code.
import-csv ./info.csv |
foreach {
if ( isLow $_.server $_.drive $_.lim ) {
$Alert += $_.server + " || " + $_.drive + " is low<br>"
}
December 19th, 2013 7:45pm
Using a CSV is good. Just load the "$servers" hash array from a CSV and the rest will work the same. The overall structure would work better if it was more like this.
Function isLow($server, $drive, $limit){
$disk = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter "DriveType = 3 and DeviceID = '$drive'"
if($disk.FreeSSpace -lt $limit){
$true
}else{
$false
}
}
$servers=@()
$servers=@{
Server="###server1"
Drive='C:'
Limit=25Gb
}
$servers=@{
Server="###server2"
Drive='D:'
Limit=15Gb
}
$servers=@{
Server="###server3"
Drive='C:'
Limit=50Gb
}
$results=foreach($servers in $servers){
if(isLow $server.Server $server.Drive $server.Limit){
'{0} || {1} is low<br>' -f $servers.Server,$server.Drive
}
}
if($results){
Write-Host 'Sending Email notification' -fore green
$mailprops=@{
SmtpServer='192.168.x.x'
From='sender@email.com'
To=$users
Subject='ALERT!!! Low DiskSpace'
Body=$results
BodyAsHtml=$true
}
Send-MailMessage @mailprops
}else{
Write-Host 'No one is past the limit' -ForegroundColor green
}
The biggest issue is to realize that this is a computer. If you type the same thing more than once then consider that the computer can do it for you. Once you learn to think like a computer all of this becomes e
December 19th, 2013 7:49pm
Too extend this, how would we make it show how much free GB it currently contains?
January 16th, 2014 1:36pm
Too extend this, how would we make it show how much free GB it currently contains?
Start by redesigning the script to give you an answer in Gb and not just a True/False. For that you need to open a new question.
January 16th, 2014 2:43pm