다음을 통해 공유


Management Pack Service Model Exercise #7 - Creating a Script Discovery (PowerShell)


Overview

The following procedures show how to create a PowerShell script discovery using  the Operations Manager 2007 Authoring console and Visual Studio Authoring Extensions.  This exercise is part of the System Center Operations Manager Management Pack Authoring Center.

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:

Revisions

The Microsoft System Center team has validated this procedure as of the original version.  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

Sample code is available in the TechNet Gallery for this exercise so that you can validate your results. 

Details

This procedure implements the same discovery as in the procedure How to Create a Script Discovery (VBScript) by using Windows PowerShell instead of VBScript. Both can be in the same management pack, but one should be disabled so that they don't attempt to discover the same instances.

The discovery created in this procedure has the following characteristics:

  • Searches for class on any agent with an instance of MyComputerRole1.
  • Discovers three instances of a class named My Application Component. 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 the Windows Computer class. This hosting class has no key property.

Authoring Console Procedure

The Operations Manager Authoring console does not include a wizard for creating this kind of discovery. Therefore, a custom discovery must be created.

  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 MyApplicationComponent (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.

      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. Note that you can use Intellisense to manually type in each entry.  Type the < character to be prompted with valid 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. Close the external editor, and save the configuration in the Authoring console.

  7. Click OK.
  8. Select File, and then click Save.

Visual Studio Authoring Extensions Procedure

  1. Create the script:

    1. In Solution Explorer, right click the name of the solution and select Add and then New Item

    2. Select PowerShell script file.

    3. In the Name box, type DiscoverApplicationComponents.ps1 and click Add

    4. Copy the following code into the script:

      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
      
    5. Select File, and then click DiscoverApplicationComponents.ps1.

  2. Create the Discovery:

    1. In Solution Explorer, right click Right click Discoveries.mptg and click Open.
    2. Right click in the  template window and select Add Template.
    3. Select Discovery (Custom) and click OK.
  3. Configure the Discovery Properties:

    1. Right click the entry for NewDiscovery and select Properties Window.
    2. Change the ID to Discovery.MyApplicationComponent.PowerShell.
    3. Change the Display Name to Discover MyApplicationComponent (PowerShell).
  4. Select the Discovery Target:

    1. Select Target and click the ellipse (...) button on the right of the field. 
    2. Select MyMP.MyComputerRole1 and click OK.
  5. Select and Configure the Discovery Data Source"

    1. Select Data Source Type ID and click the ellipse (...) button on the right of the field. 

    2. Select Microsoft.Windows.TimedPowerShell.DiscoveryProvider and click OK.

    3. Select Data Source Configuration and click the ellipse (...) button on the right of the field.

    4. Copy the following XML into the configuration window between the <Configuration> tags.  Note that you can use Intellisense to manually type in each entry.  Type the < character to be prompted with valid tags.

      <!-- IntervalSeconds specifies how often we will run the discovery. -->
      <IntervalSeconds>14400</IntervalSeconds>
       
      <!-- SyncTime specifies the minutes after the hour to synchronize execution of the discovery. -->
      <SyncTime />
       
      <!-- ScriptName specifies the name of the script. -->
      <ScriptName>DiscoverMyApplicationComponent.ps1</ScriptName>
       
      <!-- ScriptBody is the text of the script. In this case, a variable is used to specify the script file in the VSAE project -->
      <ScriptBody>$IncludeFileContent/DiscoverApplicationComponents.ps1$</ScriptBody>
       
      <!-- Parameters for the script. -->
      <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 is the seconds that the script must be running before it is automatically ended. -->
      <TimeoutSeconds>300</TimeoutSeconds>
      
  6. Save and Compile the Project:

    1. Select File, and then click Save Discoveries.mptg.
    2. Select Build and then Build Solution.
    3. Ensure that you don't receive any errors.

See Also