Management Pack Composition Exercise #2 - Creating a Monitor Based on a Windows PowerShell Script
This document is part of the Operations Manager Management Pack Authoring Guide .
Overview
The following procedure shows how to create a monitor based on a custom data source module running a Windows PowerShell script in the Operations Manager 2007 Authoring console and Visual Studio Authoring Extensions. This is the same procedure in How to create a monitor based on a custom module with the script that is written in Windows PowerShell instead of VBScript.
Product Versions
This exercise applies to the following products:
- System Center Operations Manager 2007 R2
- System Center 2012 Operations Manager
- System Center 2012 Operations Manager SP1
Prerequisites
Before you perform this procedure, you must first complete the following prerequisite procedures:
- Creating a New Management Pack - Create the management pack.
- Creating a Class - Create the class that is the target for the monitor.
- Creating a Monitor Based on a Custom Module – Create the Composition folder and monitor template group.
Revisions
The Microsoft System Center team has validated this procedure as of the original revision. We will continue to review any changes and periodically provide validations on later revisions as they are made. Please feel free to make any corrections or additions to this procedure that you think would assist other users
Sample Code
A sample of the completed code for each exercise is available in the TechNet Gallery. There is a separate sample for each exercise that includes the management pack completed at the end of that exercise and each preceding exercise. This strategy allows you to work through each exercise in order and then compare your results. For VSAE, this also includes the Visual Studio solution.
Details
The monitor created in this procedure has the following characteristics:
- Targeted at a class with only a single instance on an agent. Because there is a single instance, there is no requirement to support cookdown.
- Sets its state based on the comparison of the script value to specified threshold values.
- The monitor supports On Demand Discovery. This requires a probe action module to run the script.
- The script accepts only a single argument for the computer name of the target object’s agent.
- The script itself is only for testing and performs no real function. It simulates a script running a synthetic transaction and returning a status message of success or failure.
Authoring Console Procedure
Create Probe Action Module to Run Windows PowerShell Script
- In the Authoring Console, select Type Library and then Probe Actions.
- Right-click in the Probe Actions pane and select New and then Composite Probe Action.
- In the Choose a unique identifier box, type MyMP.ProbeActionModule.MyTransactionPSScript. Click OK.
- On the General tab, do the following:
- In the Name box, type Transaction PowerShell Script Probe Action.
- On the Member Modules tab, do the following:
Click Add to add a module.
In the Choose Module Type box, select Microsoft.Windows.PowerShellPropertyBagProbe.
In the Module ID box, type PSScript. Click OK.
In the ScriptName box, type MyTransactionScript.ps1.
In the TimeoutSeconds box, type 300.
Click the Edit… button. This starts the custom editor.
Paste the complete contents of the following script between the ScriptBody tags in the XML. Replace any text that might already exist:
<![CDATA[ sComputerName = WScript.Arguments(0) bTestSuccessful = True Set oAPI = CreateObject("MOM.ScriptAPI") oAPI.LogScriptEvent "MyTransactionScript.vbs" ,10,4, "Running script on " & sComputerName Set oBag = oAPI.CreatePropertyBag() Call oBag.AddValue("ComputerName",sComputerName) If bTestSuccessful = True Then Call oBag.AddValue("Result","Good") Else Call oBag.AddValue("Result","Bad") End If oAPI. Return (oBag) ]]>
Paste the following XML after the </ScriptBody> tag.
<Parameters> <Parameter> <Name>ComputerName</Name> <Value>$Config/ComputerName$</Value> </Parameter> </Parameters>
Close the external editor to save the script back to the module.
Click OK to save the module configuration.
In the NextModule column for the PSScript module, select Module Output.
- On the Configuration Schema tab, do the following:
- In the Simple Configuration Schema section, click Add to add a parameter.
- In the Please enter the requested value box, type ComputerName. Click OK.
- On the Data Types tab, do the following:
- In the Input Data section, select This module required input data.
- In the Output Data section, in the Data Type: box select System.PropertyBagData.
- Click OK to save the module.
- Select File, and then click Save.
Create Data Source Module to Run Probe Action on Schedule
- Select Type Library and then Data Sources.
- Right-click the Data Sources pane, select New, and then Composite Data Source.
- In the Choose a unique identifier box, type MyMP.DataSourceModule.MyTransactionPSScriptTimed. Click OK.
- On the General tab, do the following:
- In the Name box, type Timed Transaction PowerShell Script Data Source.
- On the Member Modules tab, do the following:
- Click Add to add a module.
- In the Choose Module Type box, select System.SimpleScheduler.
- In the Module ID box, type Schedule. Click OK.
- Click the button to the right side of the IntervalSeconds box and select Promote. This enters the text $Config/IntervalSeconds$.
- Click the button to the right side of the SyncTime box and select Promote. This enters the text $Config/SyncTime$.
- Click OK to save the module configuration.
- Click Add to add a new module.
- In the Choose Module Type box, select MyMP.ProbeActionModule.MyTransactionPSScript.
- In the Module ID box, type Probe. Click OK.
- Select the button to the right side of the ComputerName box and select Promote. This enters the text $Config/ComputerName$.
- Click OK to save the module configuration.
- In the NextModule column for the Schedule module, select Probe.
- In the NextModule column for the Probe module, select Module Output.
- On the Configuration Schema tab, do the following:
- Change the Type for the IntervalSeconds parameter to Integer.
- Clear the Required box next to the SyncTime parameter. The SyncTime parameter is optional for this module.
- On the Overrideable Parameters tab, do the following:
- Click Add and then IntervalSeconds.
- In the Choose a unique identifier box, type IntervalSeconds. Click OK.
- Change Configuration Element to Integer.
- Click Add and then SyncTime.
- In the Choose a unique identifier box, type SyncTime**.** Click OK.
- On the Data Types tab, do the following:
- Ensure the value in the Data Types box is System.PropertyBagData.
- Click OK to save the module.
- Select File, and then click Save.
Create Custom Monitor Type Using Custom Data Source
- Select Type Library and then Monitor Types.
- Right-click in the Monitor Types pane and select New and then Composite Monitor Type.
- In the Choose a unique identifier box, type MyMP.MyPSTransactionMonitorType. Click OK.
- On the General tab, do the following:
- In the Name box, type My Transaction PowerShell Script Monitor Type.
- On the States tab, do the following:
- Select 2 State Monitor Type.
- In the ID of state 1 box type Success.
- In the ID of state 2 box, type Failure.
- On the Member Modules tab, do the following:
Click Add to add a module.
In the Choose Module Type box, select System.PassThroughProbe.
In the Module ID box, type PassThru. Click OK.
Click OK to save the module configuration.
Click Add to add a module.
In the Choose Module Type box, select MyMP.ProbeActionModule.MyTransactionPSScript.
In the Module ID box, type Probe. Click OK.
Click the button to the right of the ComputerName box and select Promote. This will enter the text $Config/ComputerName$.
Click OK to save the module configuration.
Click Add to add a new module.
In the Choose Module Type box, select MyMP.DataSourceModule.MyTransactionPSScriptTimed.
In the Module ID box, type DataSource. Click OK.
Click the button to the right side of the IntervalSeconds box and select Promote . This enters the text $Config/IntervalSeconds$.
Click the button to the right side of the ComputerName box and select Promote. This enters the text $Config/ComputerName$.
Click Edit. This starts the custom editor.
After the line:
<``IntervalSeconds``>$Config/IntervalSeconds$</``IntervalSeconds``>
add the following line:
< SyncTime >$Config/SyncTime$</ SyncTime >
Close the editor to add the XML back the Authoring console. If you receive an error saying that the IntervalSeconds parameter is invalid according to its data type, click Ignore. This error message occurs because the IntervalSeconds parameter is configured as an integer, and the Authoring Console is reading $Config/IntervalSeconds$ as a string. This variable will be replaced with an integer value when the workflow is run so the error can be ignored.
Click OK to save the module configuration.
Click Add to add a new module.
In the Choose Module Type box, select System.ExpressionFilter.
In the Module ID box, type FilterSuccess. Click OK.
Clear the Required box next to the SyncTime parameter. The SyncTime parameter is optional for this module.
Click Configure to open the Expression dialog box.
Click Insert.
In the Parameter Name box type Property[@Name='Result'].
In the Operator box select Equals.
In the Value box type Good.
Click OK to save the expression.
Click OK to save the module configuration.
Click Add to add a new module.
In the Choose Module Type box, select System.ExpressionFilter.
In the Module ID box, type FilterFailure. Click OK.
Click Configure to open the Expression dialog box.
Click Insert.
In the Parameter Name box type Property[@Name='Result'].
In the Operator box select Equals.
In the Value box type Bad.
Click OK to save the expression.
Click OK to save the module configuration.
- On the Regular tab, do the following:
- Select Success.
- Check the Include box next to DataSource.
- Check the Include box next to FilterSuccess.
- In the Next Module box next to DataSource select FilterSuccess.
- In the Next Module box next to FilterSuccess select Monitor State Output.
- Select Failure.
- Check the Include box next to DataSource.
- Check the Include box next to FilterFailure.
- In the Next Module box next to DataSource select FilterFailure.
- In the Next Module box next to FilterFailure select Monitor State Output.
- On the On Demand tab, do the following:
- Check the box next to Use On Demand Detection.
- Select Success.
- Check the Include box next to PassThru.
- Check the Include box next to Probe.
- Check the Include box next to FilterSuccess.
- In the Next Module box next to PassThru select Probe.
- In the Next Module box next to Probe select FilterSuccess.
- In the Next Module box next to FilterSuccess select Monitor State Output.
- Select Failure.
- Check the Include box next to PassThru.
- Check the Include box next to Probe.
- Check the Include box next to FilterFailure.
- In the Next Module box next to PassThru select Probe.
- In the Next Module box next to Probe select FilterFailure.
- In the Next Module box next to FilterFailure select Monitor State Output.
- On the Configuration Schema tab, do the following:
- In the Type box next to IntervalSeconds select Integer.
- Click Add to add a parameter.
- In the Please enter the requested value box type SyncTime. Click OK.
- Clear the Required box next to SyncTime.
- On the Overrideable Parameters tab, do the following:
- Click Add, then IntervalSeconds.
- In the Choose a unique identifier box type IntervalSeconds. Click OK.
- In the Configuration Element box for IntervalSeconds, select Integer.
- Click Add, then SyncTime.
- In the Choose a unique identifier box, type SyncTime. Click OK.
- Click OK to save the module type.
- Select File, and then click Save.
Create Monitor Based on Custom Monitor Type
- Select Health Model, then Monitors.
- In the Monitors pane, expand MyMP.MyComputerRole1 and System.Health.EntityState.
- Right-click System.Health.AvailabilityState, select New, and then Custom Unit Monitor.
- In the Choose a unique identifier box, type MyMP.Monitor.MyPSTransactionMonitor. Click OK.
- On the General tab, do the following:
- In the Name box, type My PowerShell Transaction Monitor.
- On the Configuration tab, do the following:
- Click Browse for a type.
- In the Choose unit monitor type box, select MyMP.MyPSTransactionMonitorType. Click OK.
- In the IntervalSeconds box type 900.
- Clear the text in the ComputerName box. Click the button to the right side of the box, select (Host=Windows Computer), and then Principal Name (Windows Computer).
- On the Health tab, do the following:
- In the Health State box for Success select Healthy.
- In the Health State box for Failure select Critical.
- On the Alerting tab, do the following:
- Check the box next to Generate alerts for this monitor.
- In the Generate an alert when, select The monitor is in a critical health state.
- In the Alert name: box, type Test transaction failed.
- Click OK to save the monitor.
- Select File, then click Save.
Visual Studio Authoring Extensions Procedure
Create Probe Action Module to Run Windows PowerShell Script
The Probe Action module contains the script itself and can be called from tasks and other modules. In the monitor, it is used for One Demand Detection which runs when the user selects Recalculate Health.
- Create the script:
in Solution Explorer, right click the Composition folder, select Add and then select New Item.
Select PowerShell script File.
Change the name of the script to MyTransactionScript.ps1 and click Add.
Right-click MyTransactionScript.ps1 and select Open.
Copy the following code into the script window:
param($computerName) $testSuccessful = $ true $api = new - object -comObject 'MOM.ScriptAPI' $api.LogScriptEvent( 'MyScript.ps1' ,20,4,$computerName) $bag = $api.CreatePropertyBag() $bag.AddValue( 'ComputerName' ,$computerName) if ($testSuccessful -eq $true) {$bag.AddValue( 'Result' , 'Good' )} else {$bag.AddValue( 'Result' , 'Bad' )} $bag
Select File, and then click Save MyTransactionScript.ps1.
- Create the Probe Action module.
Expand the Composition folder.
Right click MyTransactionModules.mpx and select Open.
Copy the following XML into the configuration window after the existing <ProbeActionModuleType>. Note that you can use Intellisense to manually type in each entry.
<ProbeActionModuleType ID="MyMP.ProbeActionModule.MyTransactionPSScript" Accessibility="Internal" Batching="false" PassThrough="false"> <Configuration> <xsd:element minOccurs="1" name="ComputerName" type="xsd:string" /> </Configuration> <ModuleImplementation Isolation="Any"> <Composite> <MemberModules> <ProbeAction ID="PSScript" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagProbe"> <ScriptName>MyTransactionScript.ps1</ScriptName> <ScriptBody>$IncludeFileContent/Composition/MyTransactionScript.ps1$</ScriptBody> <Parameters> <Parameter> <Name>ComputerName</Name> <Value>$Config/ComputerName$</Value> </Parameter> </Parameters> <TimeoutSeconds>300</TimeoutSeconds> </ProbeAction> </MemberModules> <Composition> <Node ID="PSScript" /> </Composition> </Composite> </ModuleImplementation> <OutputType>System!System.PropertyBagData</OutputType> <InputType>System!System.BaseData</InputType> </ProbeActionModuleType>
Create Data Source Module to Run Probe Action on Schedule
The Data Source module calls the probe action module on a schedule. This is used for Regular Detection where the monitor is constantly running on the agent.
Copy the following XML into the configuration window after the existing <DataSourceModuleType>. Note that you can use Intellisense to manually type in each entry.
<DataSourceModuleType ID="MyMP.DataSourceModule.MyTransactionPSScriptTimed" Accessibility="Internal" Batching="false"> <Configuration> <xsd:element minOccurs="1" name="IntervalSeconds" type="xsd:integer" /> <xsd:element minOccurs="0" name="SyncTime" type="xsd:string" /> <xsd:element minOccurs="1" name="ComputerName" type="xsd:string" /> </Configuration> <OverrideableParameters> <OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int" /> <OverrideableParameter ID="SyncTime" Selector="$Config/SyncTime$" ParameterType="string" /> </OverrideableParameters> <ModuleImplementation Isolation="Any"> <Composite> <MemberModules> <DataSource ID="Schedule" TypeID="System!System.SimpleScheduler"> <IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds> <SyncTime>$Config/SyncTime$</SyncTime> </DataSource> <ProbeAction ID="Probe" TypeID="MyMP.ProbeActionModule.MyTransactionPSScript"> <ComputerName>$Config/ComputerName$</ComputerName> </ProbeAction> </MemberModules> <Composition> <Node ID="Probe"> <Node ID="Schedule" /> </Node> </Composition> </Composite> </ModuleImplementation> <OutputType>System!System.PropertyBagData</OutputType> </DataSourceModuleType>
Create Custom Monitor Type Using Custom Data Source
The monitor type calls the data source module and defines the logic for determining different health states.
Create the Monitor Type. This includes the health states and the configuration parameters for the monitor. Copy the following XML into the configuration window after the existing <UnitMonitorType>. Note that you can use Intellisense to manually type in each entry:
<``UnitMonitorType
ID``=``"MyMP.MyPSTransactionMonitorType"
Accessibility``=``"Internal"``>
``<``MonitorTypeStates``>
``<``MonitorTypeState
ID``=``"Success"
NoDetection``=``"false"
/>
``<``MonitorTypeState
ID``=``"Failure"
NoDetection``=``"false"
/>
``</``MonitorTypeStates``>
``<``Configuration``>
``<``xsd:element
minOccurs``=``"1"
name``=``"ComputerName"
type``=``"xsd:string"
/>
``<``xsd:element
minOccurs``=``"1"
name``=``"IntervalSeconds"
type``=``"xsd:integer"
/>
``<``xsd:element
minOccurs``=``"1"
name``=``"SyncTime"
type``=``"xsd:string"
/>
``</``Configuration``>
``<``OverrideableParameters``>
``<``OverrideableParameter
ID``=``"IntervalSeconds"
Selector``=``"$Config/IntervalSeconds$"
ParameterType``=``"int"
/>
``<``OverrideableParameter
ID``=``"SyncTime"
Selector``=``"$Config/SyncTime$"
ParameterType``=``"string"
/>
``</``OverrideableParameters``>
</``UnitMonitorType``>
Add the Data Source Module to the Monitor Type. The values of the configuration parameters provided by the monitor are passed to the module. Copy the following XML into the configuration window after the ending </OverideableParameters> tag. Note that you can use Intellisense to manually type in each entry:
<``MonitorImplementation``>
``<``MemberModules``>
``<``DataSource
ID``=``"DataSource"
TypeID``=``"MyMP.DataSourceModule.MyTransactionPSScriptTimed"``>
``<``IntervalSeconds``>$Config/IntervalSeconds$</``IntervalSeconds``>
``<``SyncTime``>$Config/SyncTime$</``SyncTime``>
``<``ComputerName``>$Config/ComputerName$</``ComputerName``>
``</``DataSource``>
``</``MemberModules``>
</``MonitorImplementation``>
Add a Pass Through Probe module to the Monitor Type. This module takes no parameters but is required for the On Demand detection since our probe action module is configured with PassThrough=False. Copy the following XML into the configuration window after the Data Source module. Note that you can use Intellisense to manually type in each entry:
<``ProbeAction
ID``=``"PassThru"
TypeID``=``"System!System.PassThroughProbe"
/>
Add the Probe module to the Monitor Type. This is used by the On Demand Detection. Copy the following XML into the configuration window after the PassThru module. Note that you can use Intellisense to manually type in each entry:
<``ProbeAction
ID``=``"Probe"
TypeID``=``"MyMP.ProbeActionModule.MyTransactionScript"``>
``<``ComputerName``>$Config/ComputerName$</``ComputerName``>
</``ProbeAction``>
Add Condition Detection modules to the Monitor Type for each of the health states. These include an expression that defines the criteria based on output from the script. Copy the following XML into the configuration window after the PassThru module. Note that you can use Intellisense to manually type in each entry:
<``ConditionDetection
ID``=``"FilterSuccess"
TypeID``=``"System!System.ExpressionFilter"``>
``<``Expression``>
``<``SimpleExpression``>
``<``ValueExpression``>
``<``XPathQuery
Type``=``"String"``>Property[@Name='Result']</``XPathQuery``>
``</``ValueExpression``>
``<``Operator``>Equal</``Operator``>
``<``ValueExpression``>
``<``Value
Type``=``"String"``>Good</``Value``>
``</``ValueExpression``>
``</``SimpleExpression``>
``</``Expression``>
</``ConditionDetection``>
<``ConditionDetection
ID``=``"FilterFailure"
TypeID``=``"System!System.ExpressionFilter"``>
``<``Expression``>
``<``SimpleExpression``>
``<``ValueExpression``>
``<``XPathQuery
Type``=``"String"``>Property[@Name='Result']</``XPathQuery``>
``</``ValueExpression``>
``<``Operator``>Equal</``Operator``>
``<``ValueExpression``>
``<``Value
Type``=``"String"``>Bad</``Value``>
``</``ValueExpression``>
``</``SimpleExpression``>
``</``Expression``>
</``ConditionDetection``>
Add Regular Detection to the Monitor Type. This defines the modules that are run to determine each health state. The module in the innermost node is run first followed by the others in the detection. Copy the following XML into the configuration window after the closing </MemberModules> tag. Note that you can use Intellisense to manually type in each entry:
<RegularDetections> <RegularDetection MonitorTypeStateID="Success"> <Node ID="FilterSuccess"> <Node ID="DataSource" /> </Node> </RegularDetection> <RegularDetection MonitorTypeStateID="Failure"> <Node ID="FilterFailure"> <Node ID="DataSource" /> </Node> </RegularDetection> </RegularDetections>
Add On Demand Detections to the Monitor Type. This defines the modules that are run to determine each health state. The module in the innermost node is run first followed by the others in the detection. Copy the following XML into the configuration window after the closing </MemberModules> tag. Note that you can use Intellisense to manually type in each entry:
<OnDemandDetections> <OnDemandDetection MonitorTypeStateID="Success"> <Node ID="FilterSuccess"> <Node ID="Probe"> <Node ID="PassThru" /> </Node> </Node> </OnDemandDetection> <OnDemandDetection MonitorTypeStateID="Failure"> <Node ID="FilterFailure"> <Node ID="Probe"> <Node ID="PassThru" /> </Node> </Node> </OnDemandDetection> </OnDemandDetections>
Save and Compile the Project:
- Select File, and then click Save MyTransactionModules.mptg.
- Select Build and then Build Solution.
- Ensure that you don't receive any errors.
Create Monitor Based on Custom Monitor Type
- Create the Monitor:
- In Solution Explorer, expand the Composition folder.
- Right click Monitors.mptg and select Open.
- Right click in the Monitors.mptg and select Add Template.
- Select Monitor (Unit).
- Configure the Monitor Properties:
- Right click the entry for NewUnitMonitor and select Properties Window.
- Change the ID to Monitor.MyPSTransactionMonitor.
- Change the Display Name to My PowerShell Transaction Monitor.
- Select the Monitor Target:
- Select Target and click the ellipse (...) button on the right of the field.
- Select MyMP.MyComputerRole1 and click OK.
- Select the Monitor Type:
- Select Monitor Type ID and click the ellipse (...) button on the right of the field.
- Select MyMP.MyPSTransactionMonitorType and click OK.
- Select Parent Monitor ID and click the ellipse (...) button on the right of the field.
- Select System.Health.AvailabilityState and click OK.
- Configure the Monitor:
Select Monitor Configuration and click the ellipse (...) button on the right of the field.
Copy the following XML into the configuration window between the <Configuration> tags. Note that you can use Intellisense to manually type in each entry.
<ComputerName>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</ComputerName> <IntervalSeconds>900</IntervalSeconds> <SyncTime />
Select Monitor Operational States and click the ellipse (...) button on the right of the field.
Next to Success, select Healthy.
Next to Failure, select Critical.
Click OK,
- Save and Compile the Project:
- Select File, and then click Save Monitors.mptg.
- Select Build and then Build Solution.
- Ensure that you don't receive any errors.