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 . 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)
Taking a look at the highlighted sections in the screen shot above, you need to update the following:
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!
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).