Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

Powershell - Reusing Mail Function

$
0
0

I have a function stored in: sendmail.ps1

function sendMail ($errortype){
switch ($errortype) 
    { 
    "1"  { $message = "Uh.. something's broken" } 
    "2"  { $message = "This can't be right!"} 
    "3"  { $message = "Looks good, captain."} 
    }

     Write-Host "Sending Email"

     #SMTP server name
     $smtpServer = "SMTP.EXAMPLE.COM"

     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage

     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)

     #Email structure 
     $msg.From = "noreply-error@EXAMPLE.COM"
     $msg.ReplyTo = "noreply-error@EXAMPLE.COM"
     $msg.To.Add("list@CONTOSO.COM")
     $msg.subject = "[SUBJECT] Error"
     $msg.body = "$message Please check the daily logs."

     #Sending email 
     $smtp.Send($msg)
  
}

#Calling function
sendMail

The function works great...
Now, I have called the function in my main script: DAILYSCRIPT.PS1

if (something isn't right) {
. "c:\scripts\sendmail.ps1"
sendMail -errortype 1
 exit
} else { keepdoingwhatyou'redoing}

The crazy thing... when my DAILYSCRIPT runs, it sends 2 messages.
One with a $message, the other without.

Any clues?


Viewing all articles
Browse latest Browse all 15370


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>