JScript Discovery

The other day I got a email asking if I had some example code for discovering SCOM objects by using JScript. My first thought was that it shouldn’t be to hard, but after searching the internet there was next to nothing on using JScript in SCOM discoveries.  I was interested if I could get it to work, so here is the result.

The code snippet is a complete discovery for the application component MPAlchemy.JScript.Example.Class1.

var SourceID = WScript.arguments(0);
var ManagedEntityId = WScript.arguments(1);
var PrincipalName = WScript.arguments(2);

    var scriptAPI = new ActiveXObject('MOM.ScriptAPI');

oDiscoveryData = scriptAPI.CreateDiscoveryData(0, SourceID, ManagedEntityId);

var oInstance = oDiscoveryData.CreateClassInstance("$MPElement[Name='MPAlchemy.JScript.Example.Class1']$");

oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", PrincipalName);
oInstance.AddProperty("$MPElement[Name='MPAlchemy.JScript.Example.Class1']/Name$", PrincipalName);
oInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", PrincipalName);

oDiscoveryData.AddInstance(oInstance);

scriptAPI.Return(oDiscoveryData);

Below you will find the complete listing for the management pack.

<ManagementPack ContentReadable="true" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <Manifest>
    <Identity>
      <ID>MPAlchemy.JScript.Example</ID>
      <Version>1.0.0.0</Version>
    </Identity>
    <Name>MPAlchemy.JScript.Example</Name>
    <References>
      <Reference Alias="SC">
        <ID>Microsoft.SystemCenter.Library</ID>
        <Version>6.1.7221.0</Version>
        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
      </Reference>
      <Reference Alias="Windows">
        <ID>Microsoft.Windows.Library</ID>
        <Version>6.1.7221.0</Version>
        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
      </Reference>
      <Reference Alias="Health">
        <ID>System.Health.Library</ID>
        <Version>6.1.7221.0</Version>
        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
      </Reference>
      <Reference Alias="System">
        <ID>System.Library</ID>
        <Version>6.1.7221.0</Version>
        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
      </Reference>
    </References>
  </Manifest>
  <TypeDefinitions>
    <EntityTypes>
      <ClassTypes>
        <ClassType ID="MPAlchemy.JScript.Example.Class1" Accessibility="Internal" Abstract="false" Base="Windows!Microsoft.Windows.LocalApplication" Hosted="true" Singleton="false">
          <Property ID="Name" Type="string" Key="true" CaseSensitive="false" Length="256" MinLength="0" />
        </ClassType>
      </ClassTypes>
    </EntityTypes>
  </TypeDefinitions>
  <Monitoring>
    <Discoveries>
      <Discovery ID="MPAlchemy.JScript.Example.Discovery" Enabled="true" Target="Windows!Microsoft.Windows.Computer" ConfirmDelivery="false" Remotable="true" Priority="Normal">
        <Category>Discovery</Category>
        <DiscoveryTypes>
          <DiscoveryClass TypeID="MPAlchemy.JScript.Example.Class1" />
        </DiscoveryTypes>
        <DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedScript.DiscoveryProvider">
          <IntervalSeconds>60</IntervalSeconds>
          <SyncTime />
          <ScriptName>ExampleDiscovery.js</ScriptName>
          <Arguments>$MPElement$ $Target/Id$ $Target/Property[Type="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Arguments>
          <ScriptBody><![CDATA[
var SourceID = WScript.arguments(0);
var ManagedEntityId = WScript.arguments(1);
var PrincipalName = WScript.arguments(2);

    var scriptAPI = new ActiveXObject('MOM.ScriptAPI');

oDiscoveryData = scriptAPI.CreateDiscoveryData(0, SourceID, ManagedEntityId);

var oInstance = oDiscoveryData.CreateClassInstance("$MPElement[Name='MPAlchemy.JScript.Example.Class1']$");

oInstance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", PrincipalName);
oInstance.AddProperty("$MPElement[Name='MPAlchemy.JScript.Example.Class1']/Name$", PrincipalName);
oInstance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", PrincipalName);

oDiscoveryData.AddInstance(oInstance);

scriptAPI.Return(oDiscoveryData);]]></ScriptBody>
          <TimeoutSeconds>60</TimeoutSeconds>
        </DataSource>
      </Discovery>
    </Discoveries>
  </Monitoring>
  <Presentation>
    <Views>
      <View ID="MPAlchemy.JScript.Example.StateView" Accessibility="Internal" Enabled="true" Target="MPAlchemy.JScript.Example.Class1" TypeID="SC!Microsoft.SystemCenter.StateViewType" Visible="true">
        <Category>AvailabilityHealth</Category>
        <Criteria />
      </View>
    </Views>
    <Folders>
      <Folder ID="MPAlchemy.JScript.Example.ViewFolder" Accessibility="Internal" ParentFolder="SC!Microsoft.SystemCenter.Monitoring.ViewFolder.Root" />
    </Folders>
    <FolderItems>
      <FolderItem ElementID="MPAlchemy.JScript.Example.StateView" Folder="MPAlchemy.JScript.Example.ViewFolder" />
    </FolderItems>
  </Presentation>
  <LanguagePacks>
    <LanguagePack ID="ENU" IsDefault="true">
      <DisplayStrings>
        <DisplayString ElementID="MPAlchemy.JScript.Example">
          <Name>MPAlchemy JScript Example</Name>
        </DisplayString>
        <DisplayString ElementID="MPAlchemy.JScript.Example.Class1">
          <Name>MPAlchemy JScript Example Class1</Name>
        </DisplayString>
        <DisplayString ElementID="MPAlchemy.JScript.Example.Class1" SubElementID="Name">
          <Name>Name</Name>
          <Description>Name</Description>
        </DisplayString>
        <DisplayString ElementID="MPAlchemy.JScript.Example.Discovery">
          <Name>MPAlchemy JScript Example Discovery</Name>
          <Description />
        </DisplayString>
        <DisplayString ElementID="MPAlchemy.JScript.Example.StateView">
          <Name>State</Name>
          <Description />
        </DisplayString>
        <DisplayString ElementID="MPAlchemy.JScript.Example.ViewFolder">
          <Name>MPAlchemy JScript Example</Name>
          <Description />
        </DisplayString>
      </DisplayStrings>
    </LanguagePack>
  </LanguagePacks>
</ManagementPack>

Download the management pack here: MPAlchemy.JScript.Example.zip

Advertisements
This entry was posted in JScript, Management Pack, OpsMgr 2012 and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s