How to Create a Windows PowerShell Discovery

適用於: System Center Operations Manager 2007

The following procedure shows how to create a Windows PowerShell discovery in the Operations Manager 2007 Authoring console. Before you perform this procedure, you must first complete the prerequisite procedure How to Create a Class in which you create the classes to be discovered. This procedure implements the same discovery as in the procedure How to Create a Script Discovery by using Windows PowerShell instead of VBScript. The Operations Manager Authoring console does not include a wizard for creating this kind of discovery. Therefore, a custom discovery must be created.

The discovery has the following characteristics:

  • Searches for a class on any agent with an instance of MyComputerRole1.

  • Discovers three instances of a class named MyApplicationComponent. The class has a single key property called ComponentName and a non-key property named Version.

  • The discovered class is hosted by a class named MyComputerRole1 which is hosted by Windows Computer. This hosting class has no key property.

To create a Windows PowerShell discovery

  1. Select Health Model, and then select Discoveries.

  2. Right-click in the Discoveries pane, select New, and then select Custom Discovery.

  3. In the Choose a unique identifier box, type MyMP.Discovery.MyApplicationComponent.PowerShell.

  4. On the General page, do the following:

    1. In the Name box, type Discover Application Components with PowerShell.

    2. In the Target box, select MyMP.MyComputerRole1.

  5. On the Discovered Classes page, do the following:

    1. Click Add above the Discovered classes and their attributes pane, and select Add discovered type.

    2. In the Management Pack Class Chooser box, select MyMP.MyApplicationComponent, and then click OK.

  6. On the Configuration page, do the following:

    1. Select Browse for a type.

    2. In the Choose module type box, select Microsoft.Windows.TimedPowerShell.DiscoveryProvider.

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

    4. In the IntervalSeconds box, type 14400. This is the equivalent of 4 hours.

    5. In the SyncTime box, clear the existing text.

    6. In the ScriptName box, type DiscoverMyApplicationComponent.ps1.

    7. In the TimeoutSeconds box, type 300. This is the equivalent of 5 minutes.

    8. Click Edit. This starts the external editor.

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

      <![CDATA[
      param($sourceId,$managedEntityId,$computerName)
      
      $api = new-object -comObject 'MOM.ScriptAPI'
      $discoveryData = $api.CreateDiscoveryData(0, $SourceId, $ManagedEntityId)
      
      for ($i=1; $i -le 3; $i++)
      {
          $instance = $discoveryData.CreateClassInstance("$MPElement[Name='MyMP.MyApplicationComponent']$")
          $instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $computerName)
          $instance.AddProperty("$MPElement[Name='MyMP.MyApplicationComponent']/ComponentName$", 'Component' + $i)
          $discoveryData.AddInstance($instance)
      }
      
      $discoveryData
      ]]>
      
    10. Add the following XML after the ScriptBody tags and before the TimeoutSeconds tags:

      <Parameters>
         <Parameter>
            <Name>sourceID</Name>
            <Value>$MPElement$</Value>
         </Parameter>
         <Parameter>
            <Name>managedEntityID</Name>
            <Value>$Target/Id$</Value>
         </Parameter>
         <Parameter>
            <Name>computerName</Name>
            <Value>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Value>
         </Parameter>
      </Parameters>
      
    11. The complete configuration of the discovery in the external editor should now resemble the following:

      <Configuration p1:noNamespaceSchemaLocation="C:\Users\Username\AppData\Local\Temp\MyMP.Discovery.MyApplicationComponent.PowerShell.xsd" xmlns:p1="http://www.w3.org/2001/XMLSchema-instance">
        <IntervalSeconds>14400</IntervalSeconds>
        <SyncTime></SyncTime>
        <ScriptName>DiscoverMyApplicationComponent.ps1</ScriptName>
        <ScriptBody>
      <![CDATA[ 
      param($sourceId,$managedEntityId,$computerName)
      
      $api = new-object -comObject 'MOM.ScriptAPI'
      $discoveryData = $api.CreateDiscoveryData(0, $SourceId, $ManagedEntityId)
      
      for ($i=1; $i -le 3; $i++)
      {
         $instance = $discoveryData.CreateClassInstance("$MPElement[Name='MyMP.MyApplicationComponent']$")
         $instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $computerName)
         $instance.AddProperty("$MPElement[Name='MyMP.MyApplicationComponent']/ComponentName$", 'Component' + $i)
         $discoveryData.AddInstance($instance)
      }
      
      $discoveryData
      ]]>
      </ScriptBody>
        <Parameters>
          <Parameter>
            <Name>sourceID</Name>
            <Value>$MPElement$</Value>
          </Parameter>
          <Parameter>
            <Name>managedEntityID</Name>
            <Value>$Target/Id$</Value>
          </Parameter>
          <Parameter>
            <Name>computerName</Name>
            <Value>$Target/Host/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Value>
          </Parameter>
        </Parameters>
        <TimeoutSeconds>300</TimeoutSeconds>
      </Configuration>
      
    12. Close the external editor, and save the configuration in the Authoring console.

  7. Click OK.

  8. Select File, and then click Save.

另請參閱

概念

Discovery Scripts