MPAuthor - Using a PowerShell script to collect performance data in a SCOM rule - 101


 

In this example – we will take the most BASIC PowerShell script possible, and show how we can us it to collect Performance data in a SCOM rule.

We will use Silect’s free tool – MPAuthor.

First – the script:

I will simply count the number of items in the C:\Windows\Temp directory:

 

$TempCount = (Get-ChildItem C:\Windows\Temp).Count $TempCount

 

 

 

 

 

Here is the typical script output:

image

 

Now – the idea here is – what is the minimal amount of work required to get this into SCOM?

We need to add a few items to the script.

First, we need to load the MOMScript API for PowerShell:

$api = New-Object -comObject "MOM.ScriptAPI"

Next – since this is a Performance rule – we need a way to output the data in a way that SCOM understands.  These are called PropertyBags and here is an example for this use case, to load the PropertyBag type into a variable:

$bag = $api.CreatePropertyBag()

Lastly – we need to output the data we collected into a bag, which is a pair of items separated by a comma:

$bag.AddValue(“FilesInTemp”,$TempCount)

I just gave my perf counter a made up name, “FilesInTemp” and $TempCount represents the numerical value from my previous simple script. 

Lastly – we output the PropertBag:

$bag

Now lets see the script:

#Load the MOMScript API and the PropertBag provider $API = New-Object -comObject "MOM.ScriptAPI" $bag = $api.CreatePropertyBag() #Main script $TempCount = (Get-ChildItem C:\Windows\Temp).Count #Add the data into the PropertyBag $bag.AddValue("FilesInTemp",$TempCount) #Output the PropertyBag data for SCOM consumption: $bag

These are the minimal changes to a Powershell script to use it in a SCOM rule.  I want to add one more section – which will create an event log entry for troubleshooting purposes:

$api.LogScriptEvent("CountFilesInTemp.ps1",3280,0,"Count Files in Temp Directory Script is starting")

and

$api.LogScriptEvent("CountFilesInTemp.ps1",3281,0,"Count Files in Temp Directory Script is complete.  Number of files is $TempCount")

Now lets see the final script:

#Load the MOMScript API and the PropertBag provider $API = New-Object -comObject "MOM.ScriptAPI" $bag = $api.CreatePropertyBag() #Log an event that our script is starting $api.LogScriptEvent("CountFilesInTemp.ps1",3280,0,"Count Files in Temp Directory Script is starting") #Main script $TempCount = (Get-ChildItem C:\Windows\Temp).Count #Add the data into the PropertyBag $bag.AddValue("FilesInTemp",$TempCount) #Log an event that our script is complete $api.LogScriptEvent("CountFilesInTemp.ps1",3281,0,"Count Files in Temp Directory Script is complete. Number of files is $TempCount") #Output the PropertyBag data for SCOM consumption: $bag

We are ready to bring this into MPAuthor.

Load MPAuthor, and create a new empty MP. 

image

New rule:

image

 

Give the script a name an paste the script body in:

image

 

On the Performance Mapper screen, we can put in here whatever we want…. we can use discovered properties or just plain text.  Here is my example:

image

I made up static text for Object, Counter, and Instance, that I want for my Perf data in SCOM.  The “Value” I used is “FilesInTemp” which comes from whatever text you put in the PropertyBag (highlighted):

$bag.AddValue("FilesInTemp",$TempCount)

Click Next, and choose a Target class for the rule to run on.  I always choose “Windows Server Operating System” as a generic target. 

image

 

Then provide a good ID and Display Name:

image

 

Don’t create a view at this time.  However we DO want to save data to the Data Warehouse for reporting:

image

 

This is a script – so we need a schedule.  For testing – I will set this to run every 60 seconds.  In production, we would NEVER want a PowerShell script to run on all systems that often.

image

 

Looks good:

image

 

 

Lets save the MP and review the XML:  The primary MP entities created were:

  1. Datasource Module
  2. Probe Action Module
  3. Rule

image

 

The probe action module utilizes Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe and this is basically just the script itself.

The DataSource module is a composite datasource combining the System.Scheduler simple recurring schedule, and the value is passed to this by the Rule.  It also contains the Probe Example.PowerShell.Script.CountFilesInTemp.Rule.ProbeActionModuleType

The Rule is super simple.  It contains the values for Timeout, and Interval, calls the DataSource, and adds a Condition Detection which maps the data output by the script to Performance data using System.Performance.DataGenericMapper.  This – it has two write actions, which simply collect the performance data to the OpsDB and the DW:

<WriteAction ID="Microsoft.SystemCenter.CollectPerformanceData" TypeID="SC!Microsoft.SystemCenter.CollectPerformanceData" />
<WriteAction ID="Microsoft.SystemCenter.DataWarehouse.PublishPerformanceData" TypeID="SCDW!Microsoft.SystemCenter.DataWarehouse.PublishPerformanceData" />

At this point – lets import the MP into our lab SCOM environment for review.

On an agent – you can review the SCOM event log to see the progress:

Log Name:      Operations Manager
Event ID:      1200
Description:  New Management Pack(s) requested. Management group "OMMG1", configuration id:"".

Log Name:      Operations Manager
Event ID:      1201
Description:  New Management Pack with id:"Example.PowerShell.Script", version:"1.0.0.1" received.

Log Name:      Operations Manager
Event ID:      1210
Description:  New configuration became active. Management group "OMMG1", configuration id:"b845c23195a8ce07743ceae1aee1f2d9,fc14a16523ee42f1b45b0ca60c727751:0017BC89".

Log Name:      Operations Manager
Event ID:      3280
Description:  CountFilesInTemp.ps1 : Count Files in Temp Directory Script is starting

Log Name:      Operations Manager
Event ID:      3281
Description:  CountFilesInTemp.ps1 : Count Files in Temp Directory Script is complete.  Number of files is 807

Based on those last two events it looks good!  Lets create a performance view and see what's coming in:

Success!

image

 

I’ll attach my Example MP below.

Example.PowerShell.Script.xml.zip


Comments (4)

  1. Anonymous says:

    In a previous post – I demonstrated using MPAuthor to create a VERY simple Powershell script based

  2. Naidjel says:

    Didn't work((( don't show alert

  3. Naidjel says:

    Didn't work((( don't show alert

  4. jo says:

    its not show alert, because this is collection rule.. not alert rule

Skip to main content