A while back, I received an email request from an admin using our Notifcation Test MP to trigger alerts not only to test end-to-end notification, but to test alert forwarding through the Engyro TEC connector. Without getting into verbose detail, the Notification Test MP implements a task named “Notification Test” that is targeted to the Health Service Class. It logs an event which generates a critical alert, which triggers notification. (Read more about the MP HERE).
But our admin wanted to kick things up a notch. His request in this case was very straightforward:
“How can I run the ‘Notification Test’ task every few hours on a schedule to periodically test my Engyro TEC connector?”
His idea was that this would serve as sort of a ‘connector heartbeat’ of sorts. Interesting solution for sure. At a more basic level, the request is simply “how does one execute a task on demand or on a schedule?”. Not surprisingly, Powershell comes to the rescue here. We can use a simple Powershell script to call the “Notification Test” task.
How to make it happen
Here’s a quick study on how to call an agent task from Powershell and specify the computer on which you’d like it to run.
- Save the sample script below to a file with a .ps1 extension.
- Update the following variables at the top of the script:
- Be sure to change the name of the RMS assigned to the $rootMS variable.
- If it’s not the “Notification Test” task you’re interested in running, simply change the name of thename assigned to the $taskName variable.
- Finally, specify the name of the computer on which the task should run in the $mycomputer variable.
- Using the Task Scheduler in Windows, schedule the script to run at the desired interval.
#———-begin sample script————-
$rootMS = “myrms.contoso.com”
$taskName = “Notification Test”
$mycomputer = “mycomputer.contoso.com”
#Initialize the OpsMgr Provider
add-pssnapin “Microsoft.EnterpriseManagement.OperationsManager.Client”;
set-location “OperationsManagerMonitoring::”;
#set Management Group context to the provided RMS
new-managementGroupConnection -ConnectionString:$rootMS;
set-location $rootMS;
#Change to the All Computers Group
cd Microsoft.SystemCenter.AllComputersGroup
#Change to the path of the computer where you want to run the task (this task is targeted to the computer)
cd $mycomputer
#Create a variable to hold the task you want to start.
$task = Get-Task | where {$_.DisplayName -eq “$taskName”}
#The Start-Task Cmdlet will block until the task it is complete unless you start asynchronously
Start-Task -Task: $task -Asynchronous
#———-end sample script————-
If this calls to mind any ideas or questions, please leave them as a comment on this post.