Welcome to the Inedo Forums! Check out the Forums Guide for help getting started.

If you are experiencing any issues with the forum software, please visit the Contact Form on our website and let us know!

Returning a list from a PSEval execution



  • I'd like to return a list of values back from a short Powershell script. I can get the PSEval to work but once the value is back in BM I can't get it to let loop through it.

    I can only get the PSEval to run if I do this:
    set $var = @PSEval($PsScript);
    I cannot tell if it's actually returning the values yet though. If I output it to the log it just says System.Collections.Hashtable.

    If I tried to do set @var it would fail saying I could not assign a scalar to a vector.

    But when I try to use the $var it is seen as a scalar so I cannot use it in a loop. Is there someway I could convert it?

    What I'm trying to accomplish is I'll be looking up some things in a database to use during deployments so I need to be able to do that and feed those values into the deployment where they will be looped thru and applied. Ideally this would be in the form of a list of mapped values.

    Here is the short Otterscript I have to test this:

    set $PsScript = >>
    `$var1 = 'serviceName'
    `$var2 = 'type'
    `$serviceList = @{}
    for (`$i=1;`$i -le 5; `$i++) {
        `$serviceList.Add(`$var1+`$i,`$var2+`$i)
    }
    return `$serviceList
    >>;
        
    set $testmap = @PSEval($PsScript);
    

    Product: BuildMaster
    Version: 5.6.8


  • inedo-engineer

    It looks like that @PSEval call is returning the string "System.Collections.Hashtable".

    I did some research and the reason is that PSEval returns the output of the script as a string.

    Using this PowerShell script:

    $var1 = 'serviceName'
    $var2 = 'type'
    $serviceList = @{}
    for ($i=1;$i -le 5; $i++) {
        $serviceList.Add($var1+$i,$var2+$i)
    }
    

    And this OtterScript:

    pscall PsScript
    (
        serviceList => %serviceList
    );
    foreach $key in @MapKeys(%serviceList)
    {
        set $value = $MapItem(%serviceList, $key);
        Log-Information Key: $key;
        Log-Information Value: $value;
    }
    

    I've submitted a patch that will make it do what you want.



  • So does it work with PSCall or is that the patch you've submitted? Sorry wasn't clear to me.

    If PSCall works now that would be fine for me. I just didn't realize you could get values back from PSCall.


  • inedo-engineer

    The patch I submitted makes PSCall able to use output variables that are % and @ in addition to the $ variables it already supports.



  • Great. Thanks for clarifying.



  • Thanks Ben! I've merged the pull request and we'll be deploying the updated extension shortly.



  • When will this fix be pushed out? I've upgraded to 5.6.9 and all the extensions are showing they are up-to-date. I'm following the example given above but I still get the cannot assign a Scalar value to a Map Variable.


  • inedo-engineer

    The latest version of Windows.bmx is 5.6.3, which doesn't include the fix:

    https://github.com/Inedo/inedox-windows/compare/BuildMaster-5.6.3.1...master

    I'll see if I can get someone to publish it.



  • The new version of the windows extension was pushed earlier!



  • Still not working for me. Still getting the Cannot assign a scalar value to a map variable.
    This is on 5.6.9 and with the latest update to the Windows extension.

    Here is what I have done.

    I have a test Powershell script saved into Script Assests:

    $var1 = 'serviceName'
    $var2 = 'type'
    $hashlist = @{}
    for ($i=1;$i -le 5; $i++) {
        $hashlist.Add($var1+$i,$var2+$i)
    }
    

    and then in the Plan I'm calling it like this:

    PSCall TestApp::TestToWriteOutMappedValues
    (
    items => %hashlist
    );

    # Loop
    foreach $key in @MapKeys(%items)
    {
        set $value = $MapItem(%items,$key);
    
        Log-Information Begin $key    ----    $value;
    }

  • inedo-engineer

    You have the order of the output parameter reversed. It goes PowerShellVariableName => OtterScriptVariableName, so it should be hashlist => %items.



  • Thanks. That of course fixed it.



Inedo Website HomeSupport HomeCode of ConductForums GuideDocumentation