Powershell function for sending email

Sending email is a common task.. here is a simple but functional …Function 🙂


function sendMail($BODYemail,$SUBemail,$TOemail) {

Write-Host "Sending Email"

#SMTP server name
$smtpServer = "smtpserver.MYDOMAIN.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 = "FROMEMAIL@MYDOMAIN.COM"
$msg.ReplyTo = "REPLYTOEMAIL@MYDOMAIN.COM"
$msg.To.Add("$TOemail")
$msg.subject = "$SUBemail"
$msg.body = "$BODYemail"
$msg.IsBodyHTML = $true

#Sending email
$smtp.Send($msg)

}

Send some info

# To the User
$TOemail = "$OBJ_ContactPointEmail"
$BODYemail = $MSG_Header + $MSG_USERBODY + $MSG_Foot
$SUBemail = "Follow-up information on your request [$INVAR_SR]"

sendMail $BODYemail $SUBemail $TOemail

If ($INT_CountSAG -ne 0 )
{
$TOemail = "MYEMAIL@MYDOMAIN.COM"
$BODYemail = $SAGMessage
$SUBemail = "SAG Follow up"
sendMail $BODYemail $SUBemail $TOemail
}

If ($INT_CountErrors -ne 0 )
{
$TOemail = "MYEMAIL@MYDOMAIN.COM"
$BODYemail = $MSG_ERRORBODY + $MSG_SAGADMIN
$SUBemail = "Add users to groups errors"
sendMail $BODYemail $SUBemail $TOemail
}

Getting Started with powershell

When I first heard about PowerShell (an eternity ago), I basically ignored it. I thought – “My Batch files and VBScripts work just fine.”

However – as more and more Microsoft tools began to transition to utilizing PowerShell I took it upon myself to learn.

Now.. I can’t imaging going back to simple VBScript (.. I admit I still have some VB I still use.. because those scripts still work!)

So how does one get started learning PowerShell?

1. Pick a project: You need a project in order to focus your mind. If you just try to jump out and read commands – all that will do is familiarize you, and that’s essential tool, but it won’t get it cemented into your brain. So think of something simple you want to do. For our sample – lets do something simple:

  • Get all the people in an active directory group.

2. Type it up: In your mind, break down the steps of what you want to do in as many steps as you can think of – think like a computer, what does it need to do to get the info you want. For our example:

  • define a group name to a variable
  • query ad for the group
  • look up the members of the group
  • display the members

3. Documented Code: One of the nice thing about doing this is that it helps you formulate your thoughts on what needs to happen in small workable chunks. Another is that you can use your comments as the beginning of comments for your code. It’s always important to comment your code, because when others review it – it will help them follow along…. including when that other is yourself 6 months later trying to revise your own code! In powershell we use the # sign to document a comment

  • # define a group name to a variable
  • # query ad for the group
  • # look up the members of the group
  • # display the members

4. Replace ideas with Code: The next step is to replace the ideas – with actual powershell. Don’t hesitate to use your favorite search engine and look up how to do each component.


# define a group name to a variable
$MYGROUPNAME = "Test-Group"

# query ad for the group
$MYGROUPOBJECT = get-adgroup -Identity $MYGROUPNAME -Properties *

# look up the members of the group
$MYGROUPMEMBERS = $MYGROUPOBJECT.members

# display the members
echo $MYGROUPMEMBERS

This is a simple script (and it could be a lot simpler – one line in fact), but as you learn – the principles of how to learn stay the same. Break it down into smaller components and take it one section at a time until you are familiar with coding in PowerShell!

“Stop and do it right the first time”

… so like any homeowner I’ve had to fix a toilet or two in my time.  When a task comes up at work – I am the guy who is always saying “stop and do it right the first time”… so of course I am also the guy who doesn’t take his own advice on household chores.

So back to my toilet.   The tank was constantly running.  It’s a 15 year old toilet so I knew right away that the parts in the tank with the “5 year warranty” probably needed replacing…   Call me “Powers of Deduction Man!” – my superpower is knowing the obvious.

So I run off to my local Home Depot and get a kit… do i get  complete kit? nah. I grab the kit with just the most common pieces.

An hour later i’ve replaced the parts, and while things are better – I have a slow leak from the top tank into the bowl (not the floor thankfully).  Which I attempt to patch.     Six months later… my leak is back.  And it quickly gets … worse.

So back to Home depot for the … other half of the kit.    So kids – the lesson here..

“Stop and do it right the first time”