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!

Inline PSExec Variable Evaluation



  • Can someone verify if the following behaviors are intended when writing scripts directly inside a PSExec operation?

    1. Runtime variables are not evaluated when quoted.
    2. Runtime variables are evaluated to empty when the script text is a single line.

    I've created a small plan that illustrates the behavior.

    ##AH:UseTextMode
    template foobar
    {
        # General
        for server MyServer
        {
            set $var1 = foo;
    
            set $var2 = bar;
    
            Execute-PowerShell echo '$var1\$var2\test';
    
            Execute-PowerShell >>echo '$var1';
    echo '$var2';
    echo '$var1\$var2\test';>>;
    
            Execute-PowerShell echo $var1\$var2\test;
    
            Execute-PowerShell >>echo $var1;
    echo $var2;
    echo $var1\$var2\test;>>;
    
            Execute-PowerShell >>echo $var1;
    echo $var2;
    echo '$var1\$var2\test';>>;
    
            Execute-PowerShell >>echo '$var1';
    echo '$var2';
    echo $var1\$var2\test;>>;
        }
    }
    

    Also can someone provide a clear example of defining and accessing the inner values of a List type and a Map type variable? I see there is a ListItem function and a MapItem function but I am having trouble successfully using them.

    Finally, is there any way to search the Q&A knowledge base other than tags? I'm sure this variable question has been asked before but the tag search seems pretty limited.

    Product: BuildMaster
    Version: 5.5.3



  • Variable replacement in PowerShell is a bit tricky because of the way it transparently does string interpolation; this means that, the PowerShell parser simply sees 'hello $world' as a string; the variable is interpolated (e.g. like string.format) at runtime. This way, you can build strings like 'hello $w' + 'orld' and get the same result.

    I mention this, because we use the PowerShell Parser to locate variables (see PowerShellScriptRunner.cs), and then add matching OtterScript variables at runtime.

    Unfortunately this makes sense once you understand it, but it's not intuitive. Can you suggest a better place for us to document this behavior?

    Regarding the List/Map functions... all runtime variables values in OtterScript are immutable, which means you always need to assign things to have the effect of adding to lists. For example.

    set @MyList = @ListAdd(@MyList, $item);
    set $item4 = $ListItem(@MyList, 4);
    
    set %MyMap = %MapAdd(%MyMap, X, $item);
    set $itemX = $MapItem(%MyMap, X);
    

    It's fairly easy for us to add example usages to functions (see VariableFunctions in the code). So if you can also suggestion examples to add (or of course, pull requests are always appreciated ^_^), please let me know



  • Thank you. The source code makes it explicitly clear what is going on. I was assuming that the OtterScript variables were being parsed out directly from the text input rather than being passed to the PSParser before hand. This also explains my troubles with the List and Map variable types as I was attempting to utilize them inline in my PSExec steps. I would suggest mentioning this parse behavior in the PSExec documentation link text and in the "Inline Script Execution" section of link text



Inedo Website HomeSupport HomeCode of ConductForums GuideDocumentation