none
Powershell discovery ignores loop but works in VSAE MP Simulator RRS feed

  • Question

  • Hello

    Here is some class

    <ClassType Extension="false" Singleton="false" Hosted="true" Base="Windows!Microsoft.Windows.LocalApplication" Abstract="false" Accessibility="Internal" ID="VTBC.SWIFT.Folder.Class">
    <Property ID="Path" Type="string" Key="true"/>
    </ClassType>


    And some very simple discovery 

      <Discovery ID="VTBC_SWIFT_Folders_monitoring.NewDiscovery" Target="Windows!Microsoft.Windows.Computer" Enabled="true" ConfirmDelivery="false" Remotable="false" Priority="Normal">
            <Category>Discovery</Category>
            <DiscoveryTypes>
              <DiscoveryClass TypeID="VTBC.SWIFT.Folder.Class" />
            </DiscoveryTypes>
            <DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedPowerShell.DiscoveryProvider">
              <IntervalSeconds>300</IntervalSeconds>
              <SyncTime />
              <ScriptName>test.ps1</ScriptName>
              <ScriptBody><![CDATA[param($sourceID, $managedEntityID, $ComputerName)
    
    [array]$folders = '111111111111111111
    2222222222222
    3333333333333
    444444444444
    5555555555555
    66666666666666s' -Split [Environment]::NewLine 
                    
    $i=0
    
    $oAPI      = New-Object -comObject MOM.ScriptAPI
    $Discovery = $oAPI.CreateDiscoveryData(0, $sourceID, $managedEntityID)
    
    ForEach ($folder in $folders)
    {
    	$oAPI.LogScriptEvent("DiscoverSWIFTfolder.ps1", 19997, 0, "$sourceID, $managedEntityID, $ComputerName, $folder")
    	
    	$Instance = $Discovery.CreateClassInstance("$MPElement[Name='VTBC.SWIFT.Folder.Class']$")
    
    	$Instance.AddProperty("$MPElement[Name='VTBC.SWIFT.Folder.Class']/Path$", $folder)
    	$Instance.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", $ComputerName)
    	$instance.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$",$folder)
    
    	$Discovery.AddInstance($Instance)
    
    	$oAPI.LogScriptEvent("DiscoverSWIFTfolder.ps1", 19998, 0, "Discovered: $folder on $ComputerName")
    	
    	$i++
    	$oAPI.LogScriptEvent("DiscoverSWIFTfolder.ps1", 19999, 1, $i)
    }
    
    	$Discovery
    
    ]]></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="Windows!Microsoft.Windows.Computer"]/PrincipalName$</Value>
                </Parameter>
              </Parameters>
              <TimeoutSeconds>60</TimeoutSeconds>
              <StrictErrorHandling>false</StrictErrorHandling>
              <!--his should always be set to false in the case of discoveries-->
            </DataSource>
          </Discovery>

    Well, it works as should in VSAE


    But in SCOM:

    Whats wrong ? Why loop ignored ? I put counter inside foreach loop - $i in always 1 on server! 


    none

    Wednesday, January 30, 2019 10:13 AM

Answers

  • The PoSH module within OpsManager is funky at times

    Try -Split '\n' instead of -Split [Environment]::NewLine. 

    Otherwise if not too complex, assign the array directly instead of splitting

    $folders = '111111111111111111','2222222222222','3333333333333','444444444444','5555555555555','66666666666666s'

    • Marked as answer by SComrrr Wednesday, January 30, 2019 2:36 PM
    Wednesday, January 30, 2019 2:03 PM

All replies

  • One more ...bug?

    Lets log it...

    $oAPI.LogScriptEvent("DiscoverSWIFTfolder.ps1", 777, 1, $folder)

    Result

    Why $folder is array ? How can it be if  

    ForEach ($folder in $folders).....


    none

    Wednesday, January 30, 2019 1:24 PM
  • The PoSH module within OpsManager is funky at times

    Try -Split '\n' instead of -Split [Environment]::NewLine. 

    Otherwise if not too complex, assign the array directly instead of splitting

    $folders = '111111111111111111','2222222222222','3333333333333','444444444444','5555555555555','66666666666666s'

    • Marked as answer by SComrrr Wednesday, January 30, 2019 2:36 PM
    Wednesday, January 30, 2019 2:03 PM
  • Without looking at your logic the least bit, please export your session variables for inspection (minus any passwords of course):

    mkdir c:\temp -Force

    $allvars = Get-Variable $allvars | Export-Clixml -Force -Path "c:\temp\allvars.xml"


    You can then inspect them yourself with:

    $allvars = Import-Clixml C:\temp\allvars.xml

    $allvars


    If you still can't figure it out after reviewing your expected vs returned logic, then post the allvars.xml for more help.


    • Edited by Tom_Riddle Wednesday, January 30, 2019 2:08 PM formatting
    • Proposed as answer by Tom_Riddle Wednesday, January 30, 2019 2:27 PM
    Wednesday, January 30, 2019 2:07 PM
  • Thx man! You are right its bug in scom ps module

    Module cant work with  *-Split [Environment]::NewLine*

    Without it all is ok


    none

    Wednesday, January 30, 2019 2:36 PM
  • PoSH module in OpsManager is not 100% PoSH so yes some flakes to see with some of the advanced PoSH stuff.  

    Glad to be of help

    Thursday, January 31, 2019 4:13 PM
  • I actually have a functional MP where I use this exact powershell line :

    $instances = $stdout.Split([environment]::NewLine,[System.StringSplitOptions]::RemoveEmptyEntries)
    

    I've already noticed that -split and .split had different behaviors (even in "regular" powershell), that might be worth a try...

    Tuesday, February 5, 2019 5:37 PM
  • Operations Manager has it's own System.Management.Automation library as resourse of Microsoft.Windows.Library management pack.

    Vladimir Zelenov | http://systemcenter4all.wordpress.com

    Monday, September 9, 2019 2:29 PM