none
Pass class ID as variable to ("$MPElement[Name='ClassID']$") in discovery RRS feed

  • Question

  • So i have some discovery

    It works with lot of classes at once, it read class parameters and also its ID`s to create them

    I need to pass class name $variable to CreateClassInstance method.

    Something like

    $Instance = $Discovery.CreateClassInstance("$MPElement[Name='$ClassName']$")

    Problem is that VSAE always check syntax for $MPElements and i just can`t compile it at all even with scriptblocks like

    $ScriptBlock = @'
        $Instance = $Discovery.CreateClassInstance("$MPElement[Name='{0}']$")
    '@ -f $ClassName
    
    . $([scriptblock]::Create($ScriptBlock))

    Only way i can see is just to use something like SWITCH in powershell, but it looks very stupid

    Switch ($class) 
    {
    'Class1' {$Instance = $Discovery.CreateClassInstance("$MPElement[Name='Class1']$")}
    'Class1' {$Instance = $Discovery.CreateClassInstance("$MPElement[Name='Class2']$")}
    #.....
    'Class100' {$Instance = $Discovery.CreateClassInstance("$MPElement[Name='Class100']$")
    }
    					
    So, any way to pass variable to MPElement ?


    none


    • Edited by SComrrr Tuesday, September 10, 2019 12:58 PM
    Tuesday, September 10, 2019 12:54 PM

All replies

  • I normally do this

    $MPElement = "$" + "MPElement

    This way its just looked at as a string rather than trying to use it as an actual variable.
    I did a PowerShell script that creates MPs using Powershell and theres a lot of variables that have the exact same thing,

    If you create those variables using the format above should be fine


    Website: www.walshamsolutions.com Technical Blog: https://www.walshamsolutions.com/technical-blog Personal Blog: https://www.walshamsolutions.com/personal-blog Twitter: Dwalshampro

    Tuesday, September 10, 2019 5:26 PM
  • Good to know, I had tried this once and it worked in VASE and actually imported.  Unfortunately when the workflow ran it failed. Thus please keep us posted on your tests!
    Tuesday, September 10, 2019 6:34 PM
  • Also with the $Classname variable because you have the ' next to it then it wont take the variable you are using for it
    What you could do is either put the variable in brackets like this

    ($ClassName)

    Or you could do it where you still have the ' character where its like

    ("$MPElement[Name=("'" + $ClassName + "'")]$")


    Website: www.walshamsolutions.com Technical Blog: https://www.walshamsolutions.com/technical-blog Personal Blog: https://www.walshamsolutions.com/personal-blog Twitter: Dwalshampro

    Tuesday, September 10, 2019 9:05 PM
  • both variants failed

    $cmd = '$MP'+ "Element[Name='$ClassName']$"
    					
    $Instance = $Discovery.CreateClassInstance($cmd)


    that looks like ok, but :

    Failed to run the PowerShell script due to exception below, this workflow will be unloaded.
    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.EnterpriseManagement.HealthService.MalformedDataItemException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

    that not working too

    $MPElement = "$" + "MPElement

    Incorrect expression specified: $MPElement = ".


    so , any solution  ?


    none

    Wednesday, September 11, 2019 11:35 AM
  • $MPElement = "$" + "MPElement"

    Apologies missing the last quotation on the end of it

    Website: www.walshamsolutions.com Technical Blog: https://www.walshamsolutions.com/technical-blog Personal Blog: https://www.walshamsolutions.com/personal-blog Twitter: Dwalshampro

    Wednesday, September 11, 2019 4:10 PM
  • Of course i saw that 

    I mean it didn't work too

    Can you pls provide 100% working example ? Still can't make it work

    Update: so, looks like no way to pass var 

    Even if no errors in VSAE- that wont work on agent with strange errors :(



    none


    • Edited by SComrrr Thursday, September 12, 2019 1:51 PM
    Thursday, September 12, 2019 10:27 AM
  • How are you detecting the class names on your variable $ClassName?

    Website: www.walshamsolutions.com Technical Blog: https://www.walshamsolutions.com/technical-blog Personal Blog: https://www.walshamsolutions.com/personal-blog Twitter: Dwalshampro

    Thursday, September 12, 2019 1:57 PM
  • Just from csv file

    So there is a column with ClassID in it


    none

    Thursday, September 12, 2019 2:04 PM
  • Should be able to pass it through.
    Do you have the full script which imports the CSV file and references the class ID?
    Might be worth me trying it to see if I can get it to work.

    Or I can try a test one and see what I get


    Website: www.walshamsolutions.com Technical Blog: https://www.walshamsolutions.com/technical-blog Personal Blog: https://www.walshamsolutions.com/personal-blog Twitter: Dwalshampro

    Thursday, September 12, 2019 2:29 PM
  • Its very simple

    Something like

    $csv =  Import-Csv -Path $FullPath -Delimiter ';'
    
    
    foreach ($line in $csv) 
    {
    
    	Switch ($line.class) $ class in column with ClassID
    	{
    	
    		{ 'Siron.Monitoring.Input.Sat.0950.Once.Class'} 	 { $Instance = $Discovery.CreateClassInstance("$MPElement[Name='Siron.Monitoring.Input.Sat.0950.Once.Class']$") }
    	}
    }
    But there much more switch statements - there are a lot of classes.. its reason why i want to pass classid to CreateInstance


    none


    • Edited by SComrrr Thursday, September 12, 2019 2:48 PM
    Thursday, September 12, 2019 2:37 PM