How to create a monitor based on a Windows PowerShell script

適用於: System Center Operations Manager 2007

The following procedure shows how to create a monitor based on a custom data source module running a Windows PowerShell script by using Operations Manager 2007 Authoring console. 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.

Before you perform this procedure, you must first complete the prerequisite procedure How to Create a Class in which you create the class to act as the target.

The monitor has the following characteristics:

  • Targeted at a class that has 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.

Create probe action module to run Windows PowerShell script

  1. Select Type Library and then Probe Actions.

  2. Right-click in the Probe Actions pane and select New and then Composite Probe Action.

  3. In the Choose a unique identifier box, type MyMP.ProbeActionModule.MyTransactionPSScript. Click OK.

  4. On the General tab, in the Name box, type Transaction PowerShell Script Probe Action.

  5. On the Member Modules tab, do the following:

    1. Click Add to add a module.

    2. In the Choose Module Type box, select Microsoft.Windows.PowerShellPropertyBagProbe.

    3. In the Module ID box, type PSScript. Click OK.

    4. In the ScriptName box, type MyTransactionScript.ps1.

    5. In the TimeoutSeconds box, type 300.

    6. Click the Edit button. This starts the external editor.

    7. Paste the contents of the following script between the ScriptBody tags in the XML. Replace any text that might already exist.

      <![CDATA[
      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
      ]]>
      
    8. Paste the following XML after the </ScriptBody> tag.

        <Parameters>
          <Parameter>
            <Name>ComputerName</Name>
            <Value>$Config/ComputerName$</Value>
          </Parameter>
        </Parameters>
      
    9. Close the external editor to save the script back to the module.

    10. Click OK to save the module configuration.

    11. In the NextModule column for the PSScript module, select Module Output.

  6. On the Configuration Schema tab, do the following:

    1. In the Simple Configuration Schema section, click Add to add a parameter.

    2. In the Please enter the requested value box, type ComputerName. Click OK.

  7. On the Data Types tab, do the following:

    1. In the Input Data section, select This module required input data.

    2. In the Output Data section, in the Data Type: box select System.PropertyBagData.

  8. Click OK to save the module.

  9. Select File, and then click Save.

Create data source module to run probe action on schedule

  1. Select Type Library and then Data Sources.

  2. Right-click in the Data Sources pane and select New and Composite Data Source.

  3. In the Choose a unique identifier box, type MyMP.DataSourceModule.MyTransactionPSScriptTimed. Click OK.

  4. On the General tab, in the Name box, type Timed Transaction PowerShell Script Data Source

  5. On the Member Modules tab, do the following:

    1. Click Add to add a module.

    2. In the Choose Module Type box, select System.SimpleScheduler.

    3. In the Module ID box, type Schedule. Click OK.

    4. Click the button to the right side of the IntervalSeconds box and select Promote. This enters the text $Config/IntervalSeconds$

    5. Click the button to the right side of the SyncTime box and select Promote. This enters the text $Config/SyncTime$

    6. Click OK to save the module configuration.

    7. Click Add to add a new module.

    8. In the Choose Module Type box, select MyMP.ProbeActionModule.MyTransactionPSScript.

    9. In the Module ID box, type Probe. Click OK.

    10. Select the button to the right side of the ComputerName box and select Promote. This enters the text $Config/ComputerName$.

    11. Click OK to save the module configuration.

    12. In the NextModule column for the Schedule module, select Probe.

    13. In the NextModule column for the Probe module, select Module Output.

  6. On the Configuration Schema tab, do the following:

    1. Change the Type for the IntervalSeconds parameter to Integer.

    2. Clear the Required box next to the SyncTime parameter. The SyncTime parameter is optional for this module.

  7. On the Overrideable Parameters tab, do the following:

    1. Click Add, and then click IntervalSeconds.

    2. In the Choose a unique identifier box, type IntervalSeconds. Click OK.

    3. Change Configuration Element to Integer.

    4. Click Add, and then click SyncTime.

    5. In the Choose a unique identifier box, type SyncTime. Click OK.

  8. On the Data Types tab, make sure that the value in the Data Types box is System.PropertyBagData.

  9. Click OK to save the module.

  10. Select File, and then click Save.

Create monitor type using custom data source

  1. Select Type Library and then Monitor Types.

  2. Rightclick in the Monitor Types pane and select New, and then click Composite Monitor Type.

  3. In the Choose a unique identifier box, type MyMP.MyPSTransactionMonitorType. Click OK.

  4. On the General tab, in the Name box, type My Transaction PowerShell Script Monitor Type.

  5. On the States tab, do the following:

    1. Select 2 State Monitor Type.

    2. In the ID of state 1 box, type Success.

    3. In the ID of state 2 box, type Failure.

  6. On the Member Modules tab, do the following:

    1. Click Add to add a module.

    2. In the Choose Module Type box, select System.PassThroughProbe.

    3. In the Module ID box, type PassThru. Click OK.

    4. Click OK to save the module configuration.

    5. Click Add to add a module.

    6. In the Choose Module Type box, select MyMP.ProbeActionModule.MyTransactionPSScript.

    7. In the Module ID box, type Probe. Click OK.

    8. Click the button to the right side of the ComputerName box and select Promote. This enters the text $Config/ComputerName$

    9. Click OK to save the module configuration.

    10. Click Add to add a new module.

    11. In the Choose Module Type box, select MyMP.DataSourceModule.MyTransactionPSScriptTimed.

    12. In the Module ID box, type DataSource. Click OK.

    13. Click the button to the right side of the IntervalSeconds box and select Promote . This enters the text $Config/IntervalSeconds$

    14. Click the button to the right side of the ComputerName box and select Promote. This enters the text $Config/ComputerName$

    15. Click Edit to start the custom editor. The SyncTime parameter must be added manually with an XML editor. This is because the Authoring Console only populates the Configuration dialog box by using required parameters for the selected module. The SyncTime parameter was configured not to be required.

    16. After the line

      <IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
      

      Add the following line

      <SyncTime>$Config/SyncTime$</SyncTime>
      
    17. Close the editor to add the XML back the Authoring Console.

      注意

      If you receive an error that says 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.

    18. Click OK to save the module configuration.

    19. Click Add to add a new module.

    20. In the Choose Module Type box, select System.ExpressionFilter.

    21. In the Module ID box, type FilterSuccess. Click OK.

    22. Click Configure to open the Expression dialog box.

    23. Click Insert.

    24. In the Parameter Name box type Property[@Name='Result'].

    25. In the Operator box select Equals.

    26. In the Value box type Good.

    27. Click OK to save the expression.

    28. Click OK to save the module configuration.

    29. Click Add to add a new module.

    30. In the Choose Module Type box, select System.ExpressionFilter.

    31. In the Module ID box, type FilterFailure. Click OK.

    32. Click Configure to open the Expression dialog box.

    33. Click Insert.

    34. In the Parameter Name box type Property[@Name='Result'].

    35. In the Operator box select Equals.

    36. In the Value box type Bad.

    37. Click OK to save the expression.

    38. Click OK to save the module configuration.

  7. On the Regular tab, do the following:

    1. Select Success.

    2. Check the Include box next to DataSource.

    3. Check the Include box next to FilterSuccess.

    4. In the Next Module box next to DataSource select FilterSuccess.

    5. In the Next Module box next to FilterSuccess select Monitor State Output.

    6. Select Failure.

    7. Check the Include box next to DataSource.

    8. Check the Include box next to FilterFailure.

    9. In the Next Module box next to DataSource select FilterFailure.

    10. In the Next Module box next to FilterFailure select Monitor State Output.

  8. On the On Demand tab, do the following:

    1. Check the box next to Use On Demand Detection.

    2. Select Success.

    3. Check the Include box next to PassThru.

    4. Check the Include box next to Probe.

    5. Check the Include box next to FilterSuccess.

    6. In the Next Module box next to PassThru select Probe.

    7. In the Next Module box next to Probe select FilterSuccess.

    8. In the Next Module box next to FilterSuccess select Monitor State Output.

    9. Select Failure.

    10. Check the Include box next to PassThru.

    11. Check the Include box next to Probe.

    12. Check the Include box next to FilterFailure.

    13. In the Next Module box next to PassThru select Probe.

    14. In the Next Module box next to Probe select FilterFailure.

    15. In the Next Module box next to FilterFailure select Monitor State Output.

  9. On the Configuration Schema tab, do the following:

    1. In the Type box next to IntervalSeconds select Integer.

    2. Click Add to add a parameter.

    3. In the Please enter the requested value box type SyncTime. Click OK.

    4. Clear the Required box next to SyncTime.

  10. On the Overrideable Parameters tab, do the following:

    1. Click Add and then IntervalSeconds.

    2. In the Choose a unique identifier box type IntervalSeconds. Click OK.

    3. In the Configuration Element box for IntervalSeconds select Integer.

    4. Click Add and then SyncTime.

    5. In the Choose a unique identifier box type SyncTime. Click OK.

  11. Click OK to save the module type.

  12. Select File, and then click Save.

Create monitor based on custom monitor type

  1. Select Health Model and then Monitors.

  2. In the Monitors pane, expand MyMP.MyComputerRole1 and then System.Health.EntityState.

  3. Right-click System.Health.AvailabilityState and select New. and then select Custom Unit Monitor.

  4. In the Choose a unique identifier box, type MyMP.MyPSTransactionMonitor. Click OK.

  5. On the General tab, in the Name box, type My PowerShell Transaction Monitor.

  6. On the Configuration tab, do the following:

    1. Click Browse for a type.

    2. In the Choose unit monitor type box select MyMP.MyPSTransactionMonitorType. Click OK.

    3. In the IntervalSeconds box type 900.

    4. Clear the text in the ComputerName box. Click the button to the right side of the box and select (Host=Windows Computer) and then Principal Name (Windows Computer).

  7. On the Health tab, do the following:

    1. In the Health State box for Success select Healthy.

    2. In the Health State box for Failure select Critical.

  8. On the Alerting tab, do the following:

    1. Check the box next to Generate alerts for this monitor.

    2. In the Generate an alert when select The monitor is in a critical health state.

    3. In the Alert name: box type Test PowerShell transaction failed.

  9. Click OK to save the monitor.

  10. Select File, and then click Save.