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 = "[email protected]"
$users = "[email protected]"
$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"

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='[email protected]'
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 easier.
¯\_(ツ)_/¯

Similar Messages

  • In Lookout 6.2 how can I send an email when an alarm has deactivated or cleared?

    I need to send an email when an alarm has deativated. Such as, AC power fails and the generator is running. Whne the AC power is restored, I want to send an email as well as when the generator stops running. I have created mailer objects to do this based on the data member for each being "false". The problem is, that if the system does a restart then emails go out for all of those data members because they start up as "false" in the normal condition. Anyone have a suggestion?
    Thanks
    Brad Adams
    Communications Group Inc.
    [email protected]

    Simplest way i can think of is a FlipFlop or a latchgate obect to ensure it did start first.  Reset the flipflop/latchgate when the email is sent.
    Mike
    Mike Crabtree - Lead Developer
    Destek of Nevada, Inc. / Digital Telemetry Systems, Inc.
    (866) 964-6948 / (760) 247-9512

  • How can I send an email to a group from my iPhone?

    I want to send an email to a group in my contacts, but when I select the group, the individuals display for selection.  How can I send an email by addressing it to the group? 

    I recently upgraded my wife's computer to mountain lion so we could sync it with her iPhone, allowing her to send emails to the groups she manages whether she's at home or on the road.
    Is it really the case that the iPhone is incapable of this?  The mac does it fine and the iPhone goes through the motions but then bails when you hit send, saying it isn't a valid address.
    This really makes no sense.  Isn't this the whole point of iCloud?

  • How can I send an email from Sharepoint Online using sandbox solution?

    How can I send an email from Sharepoint Online using sandbox solution?
    If possible I do not want to use workflow.
    Is It possible to do it without using workflow?

    hello Steven Andrews,
    when any user sends a message using contact us page in SharePoint online.
    1. We are inserting item in Contact Us List . - This is working fine
    for anonymous users also. We have used Office365 anonymous codeplex wsp and it is working fine. Anonymous user is able to insert new record in the Contact Us List.
    2. Once, new record is inserted in Contact Us list, we want to fire email notifying thanks to the user on his email id as well as to our company x person for notification of new inquiry. 
    We tried using Workflow having impersonation step for  anonymous user but it is not working for Anonymous users. Workflow is able to sent the email if someone logged into system but not working for Anonymous user although workflow is getting started
    but not able to send email although used Imperonsation step.
    We are stuck into implementing second step.

  • How can I send an email to a group in my address book, but hide the individual names and email addresses?

    how can I send an email to a group in my address book, but hide the individual names and email addresses?

    You used to be able to do this through leaving unchecked the box in preferences "when sending to a group show all member addresses". However, that feature failed some time ago (two or three years?) and the only way to hide the addresses now is to put the group in the BCC field.

  • How can I send an email to apple?

    How can I send an email to apple? I want to send them an e-mail but I dont find their e-mail on the site!

    hummam salahieh wrote:
    hey i need to know how can i stop my apple id from working i need to delete my apple id anyone can help plz
    Your issue has nothing to do with the subject of theis thread. The first thing you should do is to start your own thread. Use a properly descriptive subject line. Explain exactly what you were trying to do, what happened when you tried, what error messages you got and what troubleshooting steps you've already taken (saying "everything" is not useful information).
    Best of luck.

  • How can i send a picture when i have my imessage off?

    how can i send a picture when i have my imessage off?

    If you have MMS enabled on your carrier account and enabled on your phone you can send photos without iMessage.

  • How can I send an email to someone on my contact list?

    How can I send an email to someone on my contact lis

    Are you using Yahoo! mail? They have a big section of help pages here: [http://help.yahoo.com/kb/index?page=product&locale=en_US&y=PROD_MAIL_ML Yahoo! Mail Help]

  • How can i send an email with subject more than 50 char.

    Hi
    Can you please explain How can i send an email with subject more than 50 char?? I am using Cl_BCS=>SET_MESSAGE_SUBJECT but it is throwing an error " An exception of the type CX_SY_REF_IS_INITIAL has occured which is not caught" .
    Regards
    Alok

    Try using the following FM
    CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
          EXPORTING
            document_data              = i_mail_data
            put_in_outbox              = c_x
          TABLES
            packing_list               = i_objpack[]
            object_header              = i_objhead[]
            contents_bin               = i_objbin[]
            contents_txt               = i_objtxt[]
            receivers                  = i_receivers[]
          EXCEPTIONS
            too_many_receivers         = 1
            document_not_sent          = 2
            document_type_not_exist    = 3
            operation_no_authorization = 4
            parameter_error            = 5
            x_error                    = 6
            enqueue_error              = 7
          OTHERS                     = 8.
    Regards
    Avinash
    Edited by: Avinash Jagtap on Dec 2, 2009 7:00 AM
    Edited by: Avinash Jagtap on Dec 2, 2009 7:02 AM

  • How can I send an email notice of a calendar item

    How can I send an email notice of a calendar item?

    Are you sending it to another iPhone, or iPad?

  • How can i send group email

    how can i send grop email from mac pro

    Hi
    This is from Apple Support Site:
    http://docs.info.apple.com/article.html?path=Mail/10.7/en/mlhlp1014.html

  • My i phone was stolen and I was wondering what the exact thing to do is! Also how can i send an email to apple to tell them to lock my phone permanantely?? Helpp!!

    My i phone was stolen and I was wondering what the exact thing to do is! Also how can i send an email to apple to tell them to lock my phone permanantely?? Helpp!!

    Check this out https://discussions.apple.com/docs/DOC-3362, thanks to Kappy. Contact your carrier, they would be the only one that could possibly blacklist your phone, if they provide that service.

  • How can you send an email out to more than 65 persons at a time?

    I have an email notification list of 175 persons who regularly get communications from me. I have recently discovered that only 65 of them are getting these emails. How can I send to my entire list?
    Thanks!

    Is the list stored on a server or in Thunderbird?
    I'm not aware of a limit to the number of recipients of a message in Thunderbird when you use a list. A limit may be imposed by your service provider as an anti-spam measure. You may want to search around to learn more about any limits there.

  • HT2486 I have two email addresses for some of my contacts.  How can I send a email to both of the addresses using the one contact?

    I have two email addresses on some of my address contact cards.  How can I send emails to both addresses.. Now it will only send to one of them.

    Start typing the second address in, it will fill in.

  • How can i send several emails at one time

    Im trying to send an email out to several addresses and im getting a mailer demon message. How can i send this one email to several addresses?

    How many addresses are you trying to send to at once? Normally you can send a message to a reasonable number of recipients at once, but your mail provider may impose limitations.
    What does the mailer message say? Is it bouncing on a specific email address? (In which case, make sure you typed it correctly)
    Matt

Maybe you are looking for