Inedo Community Forums Forums
    • Recent
    • Tags
    • Popular
    • Login

    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

    Scheduled Pinned Locked Moved Support
    powershellbuildmaster
    12 Posts 3 Posters 30 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      jstarbird_7831
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • benB Offline
        ben inedo-engineer
        last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • ? This user is from outside of this forum
          Guest
          last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • benB Offline
            ben inedo-engineer
            last edited by

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

            1 Reply Last reply Reply Quote 0
            • ? This user is from outside of this forum
              Guest
              last edited by

              Great. Thanks for clarifying.

              1 Reply Last reply Reply Quote 0
              • ? This user is from outside of this forum
                Guest
                last edited by

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

                1 Reply Last reply Reply Quote 0
                • J Offline
                  jstarbird_7831
                  last edited by

                  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.

                  1 Reply Last reply Reply Quote 0
                  • benB Offline
                    ben inedo-engineer
                    last edited by

                    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.

                    1 Reply Last reply Reply Quote 0
                    • ? This user is from outside of this forum
                      Guest
                      last edited by

                      The new version of the windows extension was pushed earlier!

                      1 Reply Last reply Reply Quote 0
                      • J Offline
                        jstarbird_7831
                        last edited by

                        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;
                        }
                        
                        1 Reply Last reply Reply Quote 0
                        • benB Offline
                          ben inedo-engineer
                          last edited by

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

                          1 Reply Last reply Reply Quote 0
                          • ? This user is from outside of this forum
                            Guest
                            last edited by

                            Thanks. That of course fixed it.

                            1 Reply Last reply Reply Quote 0

                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                            With your input, this post could be even better 💗

                            Register Login
                            • 1 / 1
                            • First post
                              Last post
                            Inedo Website Home • Support Home • Code of Conduct • Forums Guide • Documentation