Share via


Sending Email with PowerShell and the Run.Net Object

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 https://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!

Comments

  • Anonymous
    January 01, 2003
    The comment has been removed

  • Anonymous
    January 01, 2003
    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.

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

  • Anonymous
    June 17, 2011
    The comment has been removed

  • Anonymous
    November 10, 2011
    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,