Jim Britt's [MSFT] Blog

This blog will contain lessons learned as it relates to automation specific to Opalis / System Center Orchestrator.

Sending Email with PowerShell and the Run.Net Object

Sending Email with PowerShell and the Run.Net Object

  • Comments 5
  • Likes

Hello readers. Now that TechEd NA 2011 is over (what a great event) I thought I’d take this opportunity to share a very simple solution I have leveraged in my workflow development within Opalis for providing communication on workflow success / failure of status information and details.  Sometimes it is as simple as “Your xyz workflow has successfully completed” or “A failure has occurred with xyz application @5:45 PM on Server server01 at triggered workflow X and activity y”.  Of course these details can be included in a ticket within System Center Service Manager as an example, but sometimes it is really valuable to be notified when things are done or they have gone wrong.

Why use PowerShell?

Why use PowerShell you ask?  After all there is already an activity natively available in Opalis (2 in fact) that will allow you to send emails without any custom coding.  Well, for me I like to have the ability to leverage PowerShell.  It gives me the flexibility to modify things on the fly.  To give some context, I used to code webpages in Notepad Smile.  That hit a nerve with some of you – you know who you are haha.

Let's get to the solution

$PSEmailServer = "{Published Data}"
send-mailmessage -to "{Published Data}"`
-from “ActionServerActionAccount@contoso.com”`
-subject "Testing email from start object (body as html)"`
-body "<B>This is a test with Body as HTML http://www.contoso.com</B>"`
-BodyAsHtml

The code example is above (also shown directly below in the Run.Net object leveraging PowerShell)image

Taking a look at the highlighted sections in the screen shot above, you need to update the following:

  • Update your SMTP server (shown as published data above)
  • Update the “To:” to the alias you are sending the email to (shown as published data above)
  • The “From” in this case will be the Action Server Action Account
  • Then format your email with published data and HTML code as you see fit.  You can also just type in text with published data where appropriate without worrying about HTML code at all.

Note: The Action Server Action Account is being leveraged in this example (and would be the authenticated user by default for the Run.Net object).  The assumption is that this account will have an email account on your mail server.

Hint: If any of you are wondering how you get the larger window for editing your PowerShell (as shown above), right click in the code window and select “expand”.  Candidly, it took me a bit to figure that one out so I wanted to share that tidbit for those of you new to Opalis / Orchestrator.

That’s it.  Just a quick simple post on using PowerShell to send notifications.  Happy Automating!

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Another option you have to breaking up the command line with backticks would be to use splatting.

    Splatting is a way to pass a hashtable (or dictionary) of parameters to a command.

    First you declare your hashtable and add any key/value pairs (with the key being a parameter).  Then you pass the variable referencing the hashtable, but instead of using a "$" to identify the value, you use "@" which tells PowerShell to match up the keys with parameters of the same name and assign their values as the arguments.

    To update your example,

    $PSEmailServer = "{Published Data}"`

    $MailParameters = @{

      to = "{Published Data}"

     from = "ActionServerActionAccount@contoso.com"

     subject = "Testing email from start object (body as html)"

     body = "<B>This is a test with Body as HTML http://www.contoso.com</B>"

     BodyAsHtml = $true

    }

    send-mailmessage @MailParameters

  • Great tip Steven thanks. I actually put the backticks in there for readability. It isn't needed in the object I have in Opalis. However, I like the edit thanks for the comment.

  • why putting steven logic in a function doesn't work. I want to pass $from and $to as parameters to the my sendMail() function

    Thanks,

  • Mahmoud, are you trying to do this through Opalis or via some other means where you are using PowerShell?  

  • Mahmoud try taking off the ` character in the first line at the very end. That fixed it for me(if you are using Steven's example above).