Поделиться через


OpsMgr: Using Wizards to create Powershell-based Monitors

In System Center Operations Manager, wizard-based monitoring involves creation of single management pack elements – eg. monitors or rules - using predefined wizards in the Operations Console. This approach is used to extend monitoring for existing classes. The advantage is, working in the Operations Console is fast and easy. A user can create a monitor or a rule in a few minutes without any coding or debugging. The main disadvantage is that there is no way to create custom monitoring logic, and limited to available wizards. As it stands, there are still no wizards available for a user to create PowerShell-based monitors or rules. The user will have to use the Operations Manager 2007 Authoring Console or Visual Studio Authoring Extensions (VSAE) to create Powershell-based monitors or rules and investing a considerable amount of time and effort to author the management pack in the process.

 

As Powershell becomes more commonly used and widely deployed in the IT industry, it is therefore important for the IT Pro to be able to leverage Operations Manager for application, server or service monitoring using Powershell scripts.

As my first post, I would like to demonstrate how a wizard can be configured and used to create a Powershell-based monitor that finds and alerts on certificates that are about to expire:

image

image

 

First, create a custom module (PSProbe) that will take a Powershell script as its value, and run the script according to a schedule (Scheduler).

image

 

Then, select this module as a data source module (Wei.Out.There.TimedPowershell.PropertyBagProvider) in a custom monitor type so that a Powershell script can be tested or used.

image

 

Assign the custom monitor type name as the Type Definition ID in a custom UIPageSet element (Wei.Out.There.TimedPowershell.TwoStateMonitorPageSet) where UIPageReferences referencing wizard pages can be defined.

image

 

 

image

Finally, stick the monitor type into this folder item: “Microsoft.SystemCenter.Authoring. MonitorTypeFolder.Scripting.Generic“ and it will appeared under the Scripting\Generic folder in the “Create a unit monitor” wizard after the management pack containing this configuration is imported, as show in Figure 1.

If the management pack with the datasource, monitor type and UIPageSet elements is sealed, a custom unsealed management pack can be selected to save the custom Powershell-based monitor configurations after completing the monitor wizard, as shown in Figure 1.

                            Figure 1

image

 

The remaining steps involve going through all the pages of the wizard and plugging values into the dialog boxes:

                                                                  Figure 2

image

The following UIPageReference element in the management pack references this wizard page in Figure 2:

<UIPageReference ID="Wei.Out.There.TimedPowershell.TwoStateMonitorPageSet.Reference1" PageID="Health!System.Health.MonitorGeneralPage" Usage="CreationAndUpdate">

 

                            Figure 3

image

The following UIPageReference element in the management pack references this wizard page in Figure 3:

<UIPageReference ID="Wei.Out.There.TimedPowershell.TwoStateMonitorPageSet.Reference2C" PageID="System!System.SimpleSchedulerPage" Usage="CreationOnly">

 

 

                                                         Figure 4

 

image

The following UIPageReference element in the management pack references this wizard page in Figure 4:

<UIPageReference ID="Wei.Out.There.TimedPowershell.TwoStateMonitorPageSet.Reference3" PageID="Windows!Microsoft.Windows.ScriptPage" Usage="CreationAndUpdate">

Additional notes: The Powershell script uses the command to find certificates that are about to expire from the Hey, Scripting Guy ! Blog (https://blogs.technet.com/b/heyscriptingguy/archive/2013/03/05/use-powershell-to-find-certificates-that-are-about-to-expire.aspx), and was modified to return its output in a Propertybag.

 

 

                                                                      Figure 5

image

 

image
image

These following UIPageReference elements in the management pack reference these wizard page in Figure 5:

<UIPageReference ID="Wei.Out.There.TimedPowershell.TwoStateMonitorPageSet.Reference4" PageID="System!System.ExpressionBuilderPage" Usage="CreationAndUpdate"> <UIPageReference ID="Wei.Out.There.TimedPowershell.TwoStateMonitorPageSet.Reference5" PageID="System!System.ExpressionBuilderPage" Usage="CreationAndUpdate"> <UIPageReference ID="Wei.Out.There.TimedPowershell.TwoStateMonitorPageSet.Reference6" PageID="Health!System.Health.OperationalStatesConfigPage" Usage="CreationAndUpdate">

Additional notes: The Expression Builder Pages builds expression that looks for a particular value from the Propertybag that the data source outputs (Property[@Name='State'] ).

 

 

                                                                      Figure 6

image

The following UIPageReference element in the management pack references this wizard page in Figure 6:

<UIPageReference ID="Wei.Out.There.TimedPowershell.TwoStateMonitorPageSet.Reference7" PageID="Health!System.Health.AlertingPage" Usage="CreationAndUpdate">

Additional notes: the name of the value in the Propertybag is specified in the alert context variable: $Data/Context/Property[@Name='Description']$

 

 

Click the create button to save the monitor configuration.

To force an alert, set the threshold to a high value like 1000 days ( $thresholdindays = 1000). An example of the active alert generated is as follows:

image

 

 

PLEASE NOTE: The specify script parameters featuredoes not work in this release as the XSLT to transform the input into the correct data type has yet to be configured:

image

 

 

The end product can be a sealed library management pack with a few unsealed management packs with custom Powershell scripts referencing it, or one single unsealed management pack with datasource, monitor type, UIPageSet elements and custom Powershell script(s) (it should have a similar configuration as per documented in https://technet.microsoft.com/en-us/library/ff381420.aspx ).

With this solution, what used to take considerable time and effort to accomplish will now be a relatively fast and straight forward process.

Attached with this post are both the sealed and unsealed versions of the library management packs.

A copy of the certificate expiry monitor management pack that references the sealed library pack is available here.

A sample management pack is also available at the TechNet Gallery.

   

Disclaimer:   

All information on this blog is provided on an as-is basis with no warranties and for informational purposes only. Use at your own risk. The opinions and views expressed in this blog are those of the author and do not necessarily state or reflect those of my employer.

PowershellMonitorTypeMP.zip

Comments

  • Anonymous
    October 03, 2013
    Cool post man!

  • Anonymous
    October 31, 2013
    This is very cool! I have tested your solution, and i cannot seem to make it work. I am testing with SCOM 2012 installed on server 2012. I have created a simple script to detect files in a folder. I have created a monitor with this, targeted at Server 2008 R2 servers, but i am getting no alerts. Do you have an idea?

  • Anonymous
    May 09, 2014
    Hi, great example. I am trying something similar, I inserted a PS Script but its not working.  A simple script to see the status of all automatic services in a stopped state. Here is something that I think looks meaningful. But putting that into the Powershell script based monitor wizard is not netting any alerts. Would love to have multiple eyes to check if this will indeed bring forth the desired result or there is an error within the script. $API = new-object -comObject "MOM.ScriptAPI" $bag = $api.CreatePropertyBag() $ServiceStatus = Get-WmiObject -class win32_service |where {$.startmode -eq'Auto'-and $.State -eq 'Stopped' -and $_.name -ne 'wuauserv'} if ($serviceStatus.state -eq 'Stopped' ) {$bag.value("State","bad") $bag.Value("$ServiceStatus.Name:"+ "$ServiceStatus.state") } else {$bag.value("State","good") $bag.Value("$ServiceStatus.Name:"+ "$ServiceStatus.state") } $bag $API.Return($BAG) Thanks in advance. Kapil Dham

  • Anonymous
    May 11, 2014
    This is great but I think the reason why it is not working on other scripts is because you need to have a parameter which is not required on the management pack. Would you mind sharing the exact PS1 script you used on this example? Thanks!

  • Anonymous
    May 12, 2014
    Finally got it working!!!! Here is the code. The only thing left is now to modify Alert description to contain the values that are returned in Alert Context. Any suggestions there?

Always use the param statement to access parameters  param($myArg1)  

Get access to the scripting API  

$API = new-object -comObject "MOM.ScriptAPI"   $bag = $api.CreatePropertyBag() $ServiceStatus = Get-WmiObject -class win32_service |where {$.startmode -eq'Auto'-and $.State -eq 'Stopped' -and $_.name -ne 'wuauserv'} if (!$ServiceStatus) #if there are no automatic services that are stopped, we are healthy. { write-host “good” $bag.addvalue(“State”,”good”) } if ($servicestatus) #if there are any automatic services that are stopped, we are unhealthy. { write-host “bad” $bag.addvalue(“State”,”bad”) foreach ($service in $servicestatus) #enumerate the stopped services { $bag.addValue(“$($Service.Name):”,“$($Service.state)”) } } $bag $API.Return($BAG) Alert Context: Date and Time: 5/12/2014 1:43:19 PM Property Name Property Value State bad BMR Boot Service: Stopped clr_optimization_v4.0.30319_32: Stopped clr_optimization_v4.0.30319_64: Stopped Schedule: Stopped sppsvc: Stopped Big help if we can channel the Alert context with in the Alert Description....then we are golden. Thanks,. Kapil Dham All for SCOM and SCOM for All!!!!!!!!!!

  • Anonymous
    May 12, 2014
    in this script, how can I get the Alert context that contains service name as mentioned in above comment within Alert Description? $API = new-object -comObject "MOM.ScriptAPI"   $bag = $api.CreatePropertyBag() $ServiceStatus = Get-WmiObject -class win32_service |where {$.startmode -eq'Auto'-and $.State -eq 'Stopped' -and $_.name -ne 'wuauserv'} if (!$ServiceStatus) #if there are no automatic services that are stopped, we are healthy. { write-host “good” $bag.addvalue(“State”,”good”) } if ($servicestatus) #if there are any automatic services that are stopped, we are unhealthy. { write-host “bad” $bag.addvalue(“State”,”bad”) foreach ($service in $servicestatus) #enumerate the stopped services { $bag.addValue(“$($Service.Name):”,“$($Service.state)”) } } $bag $API.Return($BAG)

  • Anonymous
    May 12, 2014
    still I cannot pass any parameters on my script. here's a simple script that I'm currently testing but cannot generate an alert. param($computerName) $testSuccessful = "False" $api = new-object -comObject "MOM.ScriptAPI" $api.LogScriptEvent("MyScript.ps1",20,4,"Powershell Test Monitoring") $bag = $api.CreatePropertyBag() $bag.AddValue("ComputerName",$computerName) if ($testSuccessful -eq "True") { $bag.AddValue("Result","Good") } else { $bag.AddValue("Result","Bad") } $bag $api.Return($bag)

  • Anonymous
    November 18, 2014
    how can i create performance counters in SCOM 2012 r2 using powershell script

  • Anonymous
    January 07, 2016
    What is the complete PSCertExpiry.PS1 script? I can't seem to get it working.

  • Anonymous
    February 17, 2017
    Hi Wei,This is a cool library MP, very useful in creating powershell script monitors. I wanted to know if we can create a similar service monitor library MP which will have suppression settings and allow us to skip specific number of service failures. I read that this can be added to a monitor and wanted to know if we could create a library mp which will allow us to create a service monitor with these settingshttp://blog.scomskills.com/new-matchcount-configuration-in-scom-2012/