locked
PowerShell script discovery not returning data RRS feed

  • Question

  • I was wondering if anyone could help me with this because I can't seem to pinpoint what's going wrong. I've written a powershell based discovery script to discover instances of a custom class. I can see that the script is running because it logs to the event log and I can see that the script successfully finds my class instances, based on the log information. However, there are no instances of my class in SCOM. Here is my XML:

    <?xml version="1.0" encoding="utf-16"?>
    <Discovery ID="DU.ParallelsSessionHost.Discovery" Enabled="false" Target="MicrosoftWindowsLibrary7585010!Microsoft.Windows.Computer" ConfirmDelivery="false" Remotable="true" Priority="Normal">
      <Category>Discovery</Category>
      <DiscoveryTypes>
        <DiscoveryClass TypeID="DU.ParallelsSessionHost">
          <Property TypeID="DU.ParallelsSessionHost" PropertyID="ParallelsFarm" />
          <Property TypeID="DU.ParallelsSessionHost" PropertyID="ParallelsGroup" />
          <Property TypeID="SystemLibrary7585010!System.ConfigItem" PropertyID="ObjectStatus" />
          <Property TypeID="SystemLibrary7585010!System.ConfigItem" PropertyID="AssetStatus" />
          <Property TypeID="SystemLibrary7585010!System.ConfigItem" PropertyID="Notes" />
          <Property TypeID="SystemLibrary7585010!System.Entity" PropertyID="DisplayName" />
        </DiscoveryClass>
      </DiscoveryTypes>
      <DataSource ID="DS" TypeID="MicrosoftWindowsLibrary7585010!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
        <IntervalSeconds>86400</IntervalSeconds>
        <SyncTime></SyncTime>
        <ScriptName>Discover-ParallelsSH.ps1</ScriptName>
        <ScriptBody>
    function Discover-ParallelsSH
    {
    &lt;#
    .SYNOPSIS
        Discover Parallels Session hosts using the telegraf.conf file
    .NOTES
        Author - jf@07-04-20
    &gt;
    
        param($SourceId, $ManagedEntityId, $ComputerName, $ComputerDisplayName)
    
        $scriptname = "Discover-ParallelsSH.ps1"
        $whoami = whoami
        $momapi = New-Object -comObject MOM.ScriptAPI
        $momapi.LogScriptEvent(
            $ScriptName,
            12345,
            0,
            "Test script is starting. `n Running as ($whoami)."
            )
    
        $discoverydata = $momapi.CreateDiscoveryData(0, $SourceId, $ManagedEntityId)
    
        if (test-path "C:\Program Files\Parallels\Telegraf\telegraf.conf")
        {
            $momapi.LogScriptEvent(                                                     
                $ScriptName,                                                        
                12345,                                                              
                0,                                                                  
                "$scriptname - discovered Parallels component."   
                )
    
            $file = get-content "C:\Program Files\Parallels\Telegraf\telegraf.conf"
    
            if ($file[1] -like "*RD Session Host*")
            {
                $momapi.LogScriptEvent(
                    $ScriptName,
                    12345,
                    0,
                    "$scriptname - discovered Parallels Session Host."
                    )
    
                $farm = $file[2]
                $group = $file[3]
    
                $instance = $discoverydata.CreateClassInstance("$MPElement[Name='DU.ParallelsSessionHost']$")
                $instance.AddProperty("$MPElement[Name='MicrosoftWindowsLibrary7585010!Microsoft.Windows.Computer']/PrincipalName$", $ComputerName)
                $instance.AddProperty("$MPElement[Name='MicrosoftWindowsLibrary7585010!Microsoft.Windows.Computer']/NetbiosComputerName$", $ComputerDisplayName)
                $instance.AddProperty("$MPElement[Name='DU.ParallelsSessionHost']/ParallelsFarm$", $farm)
                $instance.AddProperty("$MPElement[Name='DU.ParallelsSessionHost']/ParallelsGroup$", $group)
                $discoverydata.AddInstance($instance)
            }
            else
            {
                $momapi.LogScriptEvent(
                    $ScriptName,
                    12345,
                    0,
                    "$scriptname - server is not a Session Host, exiting."
                    )
            }
        }
        
        $discoverydata
    }
    
    Discover-ParallelsSH($SourceId, $ManagedEntityId, $ComputerName, $ComputerDisplayName)
    </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/Property[Type="MicrosoftWindowsLibrary7585010!Microsoft.Windows.Computer"]/PrincipalName$</Value>
          </Parameter>
          <Parameter>
            <Name>ComputerDisplayName</Name>
            <Value>$Target/Property[Type="MicrosoftWindowsLibrary7585010!Microsoft.Windows.Computer"]/NetbiosComputerName$</Value>
          </Parameter>
        </Parameters>
        <TimeoutSeconds>300</TimeoutSeconds>
        <StrictErrorHandling>false</StrictErrorHandling>
      </DataSource>
    </Discovery>



    • Edited by Joseph Fenly Friday, April 17, 2020 8:22 AM spelling
    Wednesday, April 15, 2020 2:02 PM

Answers

  • You need to add the Params as well at the start of the script in addition to the function.  (Personally, I would ditch the function altogether, so delete line 1 and the last bracket and what's after it).

    What happens is that the main portion of the script does not get the variables so it sends nothing to the function.  You should note that there would not be any events 12345 in the agent's log or, there would be an error event right after it.

    • Proposed as answer by CyrAz Friday, June 19, 2020 8:37 PM
    • Marked as answer by Stoyan ChalakovMVP Tuesday, June 23, 2020 11:25 AM
    Friday, June 19, 2020 7:09 PM

All replies

  • Hi Joseph

    At a quick glance the script looks OK. If you're sure it's returning data then something to help troubleshoot would be the Operations Manager event log on your management server that the client is reporting to. See if there are any events that correspond to the time your script would be running. They would say something along the lines of there being issues inserting discovery data into the database and will give you some information on what's wrong. This can show up issues around the class/properties you're returning.

    Hope this helps.

    Steve

    Tuesday, April 21, 2020 12:36 AM
  • You need to add the Params as well at the start of the script in addition to the function.  (Personally, I would ditch the function altogether, so delete line 1 and the last bracket and what's after it).

    What happens is that the main portion of the script does not get the variables so it sends nothing to the function.  You should note that there would not be any events 12345 in the agent's log or, there would be an error event right after it.

    • Proposed as answer by CyrAz Friday, June 19, 2020 8:37 PM
    • Marked as answer by Stoyan ChalakovMVP Tuesday, June 23, 2020 11:25 AM
    Friday, June 19, 2020 7:09 PM