In September of 2010 Tao Yang wrote a fantastic Powershell script (v2.0 here) for command channel notifications in System Center Operations Manager. Here is my adaptation of his script. 

 NOTE: I've tested this thoroughly in my lab and I suggest you do the same before using in any other environments. This script is meant to be an example and therefore use it at your own risk. 

 

DOWNLOAD HERE

Email Alert Format Example:  

From: SCOM-Alert [mailto:scom@YOURDOMAIN.COM]
Sent: Monday, August 4, 2014 12:06 PM
To: Tyson Paul
Subject: Notified, MyDev: MS01.contoso.com \Microsoft Windows Server 2012 R2 Datacenter, Synthetic Transaction (core monitoring test)

 Severity: Critical Error

Severity: Critical Error
Alert Description: Event Description: This is a synthetic transaction test only. Disregard this event. Note: This alert was generated by entering the below command into a command pompt on the server in question:> EventCreate /T ERROR /ID 101 /L APPLICATION /SO TEST /D
Command Channel: MyDev
Source: Microsoft Windows Server 2012 R2 Datacenter
Path: MS01.contoso.com
Principal Name: MS01.contoso.com
Alert Name: Synthetic Transaction (core monitoring test)
Alert Resolution State: Notified (2)
Alert Rule Name: Synthetic Transaction
Alert Rule Description: EventCreate /T ERROR /ID 101 /L APPLICATION /SO TEST /D "This is a synthetic transaction test only. Disregard this event. Note: This alert was generated by entering the below command into a command pompt on the server in question:> EventCreate /T ERROR /ID 101 /L APPLICATION /SO TEST /D"
Time Raised: 8/4/2014 11:19:12 AM
Alert ID: 14df46a7-6dd0-4357-beb1-f20234d480c2

SCOM Operations Console Info:

Operations Console Login Info

SCOM Web Console link:

Web Console

Research It:

Bing It!
 
ADMIN DIAGNOSTIC INFO --------------------
**Use this command to view the full details of this alert in SCOM Powershell console: get-SCOMalert -Id "14df46a7-6dd0-4357-beb1-f20234d480c2" | format-list *
This email sent from: MS01.contoso.com
CF1: Alert.NetBIOSName: MS01
CF2: Alert.NetBIOSDomain Name: CONTOSO
CF3: Alert.PrincipalName: MS01.contoso.com
CF4: SubscriberList: Subscribers:SUBSCRIBER_DevTeam;SUBSCRIBER_AdminsTeam;
CF5: Management Pack Name: Operations Monitoring
CF6: Alert Class Name: Microsoft.Windows.OperatingSystem
CF7: Alert.Category Alert
CF8: Workflow Type RULE
CF9: Reserved RESERVED FOR FUTURE USE
CF10: Helpful POSH Command get-SCOMAlert -ID "14df46a7-6dd0-4357-beb1-f20234d480c2" | format-list *
Context: 
Note: This context data is only relevant to the moment/time at which this alert was sent.


type : Microsoft.Windows.EventData 
time : 2014-08-04T11:19:12.0000000-07:00 
sourceHealthServiceId : 19123C92-3F32-9ABD-858E-63C775E7663B 
EventOriginId : {DFD73E06-7861-42DF-BC01-67029B15B62E} 
PublisherId : {09136E67-2E99-8CCD-D1E6-DB0EFFEB77A1} 
PublisherName : TEST 
EventSourceName : TEST 
Channel : Application 
LoggingComputer : MS01.contoso.com 
EventNumber : 101 
EventCategory : 0 
EventLevel : 1 
UserName : CONTOSO\tpaul 
RawDescription : RawDescription 
LCID : 1033 
CollectDescription : CollectDescription 
Params : Params 
EventData : EventData 
EventDisplayNumber : 101 
EventDescription : EventDescription 
Keywords : 36028797018963968 



 

Knowledge Article:


 

Summary

Simply copy the contents in the Description field into a command prompt and execute on any Win2003 or newer server. An alert should be created and show up in the console.


Note:

The context information provided in this notification is limited and is not always helpful. To see more detailed information about this alert, log into the appropriate SCOM console for the applicable datacenter (SCOM management group) and use the Health Explorer to find more details about the state change event for the object.

 

 

Directions:

In the script ( SCOMEnhancedEmailNotification.ps1 )  There are instructions at the top that describe a few of the settings (lines of code) that must be configured before use. Simply edit the "Settings" area within the script. Use Notepad or your favorite Powershell script editor. I prefer Notepad++.
This is very simple and there are examples for each setting within the script comments at the top.

 

Here's what I recommend: 
PERFORM THESE TEST PROCEDURES IN A LAB ENVIRONMENT (NON PRODUCTION):
 

Remove all management servers from your Notifications Resource Pool except for one single server. This will help you troubleshoot during testing if needed.

Create this folder on your test mgmt server: "C:\SCOM_SCRIPTS\Notifications"

Put the script into that new folder. 

Open the script and customize the necessary variables: smtp server, port, smtp sender, web console url, etc.  See help notes at top of script. 

Make sure that your default action account is a local administrator on your test mgmt server. 

Make sure that your execution policy is not blocking the command channel execution. 

  • View policy: Get-ExecutionPolicy 
  • Or just run the following to be sure the policy is "unrestricted". In an elevated Powershell console on the mgmt server, run this command:
    Set-ExecutionPolicy Unrestricted
  • Note: a reboot may be necessary after modifying the execution policy.

 

 

 

Create the command channel:

  • Full path of the command file: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
  • Command line parameters: C:\SCOM_SCRIPTS\Notifications\SCOMEnhancedEmailNotification_v2.1.ps1 -Description 'MyDev' -alertID '$Data/Context/DataItem/AlertId$' -SubscriptionID '$MPElement$'  -ErrorLogFile C:\SCOM_SCRIPTS\Notifications\Errors.log
  •  * IF additional Ops Manager event log details are desired,  use the -Verbose switch in the command line.
  • Startup folder for the command line:  C:\SCOM_SCRIPTS\Notifications

 

 

Subscribers:

  • Create a Test Subscriber with your email address. You can configure multiple subscribers, each with one or more recipients. The script will send to To, CC, and BCC. Notice the "TO" and "CC" preceding the addresses. If "TO", "CC", or "BCC" are omitted, the default becomes "TO".

 

 

 


 Subscriptions:

 

 

  • Pick one or more "Subscribers"

 

 

 

Testing:

  • Generate a test alert with this command from regular command prompt (not Powershell console), paste the following into command window: 

    EventCreate /T ERROR /ID 101 /L APPLICATION /SO TEST /D "This is a synthetic transaction test only. Disregard this event. Note: This alert was generated by entering the below command into a command pompt on the server in question:> EventCreate /T ERROR /ID 101 /L APPLICATION /SO TEST /D

 

  • Watch Task Manager on the test mgmt server, look for a Powershell.exe process to spin up. Usually the process will take 10 - 15 seconds to send the mail.
  • You may want to adjust your max asynchronous process limit for Powershell as described here:
    http://www.scomnifties.com/powershell-script-to-set-asynchronous-process-limit-on-management-servers/
  • You should receive the email if you configured everything correctly.
  • Copy the script folder to your other management servers that will be in the Notifications Resource Pool.
  • Put each management server alone in the Notifications Resource Pool, one at a time,  and test each with the Synthetic Transaction procedure described above.

Troubleshooting:

If the script doesn't appear to be running, open Task Manager, sort by Name, and watch for a new Powershell.exe process to spin up under the Default Action Account context/user . The script takes from 10-20 seconds to complete. If the new Powershell.exe process doesn't open or opens briefly and then disappears, you have a problem.

Make sure the script itself is not being blocked from running. Right-click the script, select Properties. If you see and "Unblock" button, click it.

Check the Notifications folder for any errors that might appear in the error log. Use the -Verbose command line switch for additional Ops Manager event logging detail.

If needed, you can manually run the script. You must first set the required variables near the top of the script.

############################## UNCOMMENT FOR MANUAL TESTING ########
# $Description = "MyDescription"
# $alertID=''
# $SubscriptionID=''
# $ErrorLogFile='C:\SCOM_SCRIPTS\Notifications\ErrorLogFile.txt'
# $Verbose = $true
############################## FOR TESTING #############################

 An additional method to gain visibility into any errors that might occur (before the script has a chance to write error output to the event log) is to append this at the end of the command line parameters:

 ; Write-Output "$($Error)" |  Out-File -Filepath C:\SCOM_SCRIPTS\Notification_Errors.txt

Example:

"C:\SCOM_SCRIPTS\Notifications\SCOMEnhancedEmailNotification.ps1" -Description "MyDev" -alertID '$Data/Context/DataItem/AlertId$' -SubscriptionID '$MPElement$'  -ErrorLogFile "C:\SCOM_SCRIPTS\Errors.log" -Verbose ; Write-Output "$($Error)" |  Out-File -Filepath C:\SCOM_SCRIPTS\Notification_Errors.txt

This will output any errors that occur when the workflow attempts to launch the command channel.

Also, I've seen some weird things happen when copying/pasting from online sources into Powershell scripts. Make sure you are not copying some weirdly encoded characters. I'm pretty sure this blog doesn't use any strange encoding but to keep your sanity, copy the code portions into Notepad++ and force the encoding to be UTF-8, then copy from there into the Console wizard.

*Please let me know if you have any suggestions to improve this article.

Page History:

2014/9/16: Updated script. Improved notification subject line format.
2014/10/15: Updated command channel arguments examples, removed some quotes
2014/10/17: Updated troubleshooting section. Also updated the script to add the Subscribers to the CF4 field on "New" alerts. Previously subscribers would be empty for new alerts.

 

Disclaimer: Sample scripts in this guide are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.