Discovery 2 Classes with Powershell RRS feed

  • Question

  • Hello!

    I have a question. Is it possible to run a discoveryscript (Powershell) and return DiscoveryData for 2 different classes?

    I have two Classes

    - MyNode: All Servers running a special service

    - MyInstance: Name of Instances of the MyNode-Instances

    The script is running on each windows-server and looks for a spcial Service. If Service is find, than all Instances running on this windows server should be listed.


    ServerA: Service found with Instances Instance1, Instance2, Instance3

    Class MyServer
    |--> "ServerA"
    |--> "Instance1"
    |--> "Instance2"
    |--> "Instance3"

    Here the codefragment: 

    $discovery = $api.CreateDiscoveryData(0, $SourceId, $ManagedEntityId)
    #DiscoveryData for MyServer
    $myServer= $discovery.CreateClassInstance("$MPElement[Name='MyServer.Class']$")
    $myServer.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $ComputerIdentity)
    #DiscoveryData for MyInstance
    ForEach ($instance in $result) {
        $MyInstance = $discovery.CreateClassInstance("$MPElement[Name='MyInstance.Class']$")
        $MyInstance .AddProperty("$MPElement[Name='MyInstance.Class']/ServerName$", $TargetSystem)
        $MyInstance .AddProperty("$MPElement[Name='MyInstance.Class']/InstanceName$", $instanceName)
        $discovery.AddInstance($MyInstance )

    is this possible?

    rg Hansi

    • Edited by very_best Monday, June 22, 2020 7:03 AM
    Monday, June 22, 2020 7:03 AM


  • Hi rg,

    Yes, it can easily be done in a single PowerShell Script. You can lookup: https://social.technet.microsoft.com/wiki/contents/articles/14453.management-pack-service-model-exercise-7-creating-a-script-discovery-powershell.aspx for more information.  I will also strongly suggest you look at this video which will tell you exactly how to do it: https://channel9.msdn.com/Series/System-Center-2012-R2-Operations-Manager-Management-Packs/Mod11

    Please note that said script will run on every agent it is targeted to.  If you target the discovery to Windows Computer, then the PowerShell script will run on all Windows Computer, which may or may not be what you intend. It also costs more resources on the agents.  One workaround would be to still target it to Windows Computer and use overrides to limit the actual running.  However, by doing the workaround, you lose the biggest benefit (in my point of view) of SCOM: auto discovery of instances.  Best is to run 2 discoveries.  One registry (best) or WMI based which detects the presence of MyNode (if it is a service that you're looking for, just look for the presence of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyNode).  Then run a second discovery on MyNode which detects your MyInstances using PowerShell.  If MyNode and MyInstances have a  relationship, then you will need to rediscover the MyNode in the PowerShell script and set the relationship.  Basically, you'd be trading some work upfront for a cleaner environment and less maintenance afterwards.


    • Proposed as answer by CyrAz Monday, June 22, 2020 1:59 PM
    • Marked as answer by Stoyan ChalakovMVP Tuesday, June 23, 2020 11:26 AM
    Monday, June 22, 2020 12:26 PM