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!

Setting runtime variable from powershell script



  • Re: Setting runtime variable from powershell script

    Here is my otter script:

    ##AH:UseTextMode
    
    
    # Call DELETE on ElasticSearch
    with retry = 3
    {
        Execute-PowerShell >>$indexName=(Get-Date).AddMonths(-1).ToString("yyyy.MM.*")
    Write-Host "Index for the last Month is $indexName">>
        (
            Debug: true,
            Verbose: true
        );
    
        Get-Http $elasticUrl/$indexName
        (
            Method: DELETE
        );
    }
    

    I created variable called "$indexName" and "$elasticUrl" in my Settings -> variables
    As seen in my otter script above, I need to pass $indexName from first step to the ohter.

    Thanks


  • inedo-engineer

    Hi @ashah_4271,

    Since this is such a simple piece of PowerShell, I think the easiest way to handle this would be to use the $PSEval variable function. This should work for you:

    set $indexName = $PSEval((Get-Date).AddMonths(-1).ToString("yyyy.MM.*"));
    

    Can you please give this a try and let me know if that works?

    Thanks,
    Dan



  • @Dan_Woolf,
    This is what I get:

    Cannot bind argument to parameter 'Name' because it is null.

    Here is my Otter script block looks like

    ##AH:UseTextMode
    
    
    # Call DELETE on ElasticSearch
    with retry = 3
    {
        Execute-PowerShell 'set $indexName = $PSEval((Get-Date).AddMonths(-1).ToString("yyyy.MM.*"));'
        (
            Debug: true,
            Verbose: true
        );
    
        Get-Http $elasticUrl/$indexName
        (
            Method: DELETE
        );
    }
    

  • inedo-engineer

    Hi @ashah_4271,

    I'm sorry, I should have calrified. I sent you some OtterScript to try. Can you please try this?

    ##AH:UseTextMode
    
    
    # Call DELETE on ElasticSearch
    with retry = 3
    {
        set $indexName = $PSEval((Get-Date).AddMonths(-1).ToString("yyyy.MM.*"));
    
        Get-Http $elasticUrl/$indexName
        (
            Method: DELETE
        );
    }
    

    Thanks,
    Dan



  • @Dan_Woolf ,
    I am now getting the following error:

    Error: Unhandled exception: System.Management.Automation.IncompleteParseException: Missing closing ')' in expression.
       at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()
       at System.Management.Automation.PowerShell.EndInvoke(IAsyncResult asyncResult)
       at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Inedo.Extensions.Scripting.PowerShell.PowerShellScriptRunner.<RunAsync>d__27.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Inedo.Extensions.Scripting.PowerShell.ExecutePowerShellJob.StandardRunner.<ExecuteAsync>d__25.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Inedo.Extensions.Scripting.PowerShell.ExecutePowerShellJob.<ExecuteAsync>d__43.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Inedo.Agents.LocalJobExecuter.<ExecuteJobAsync>d__0.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Inedo.Extensions.Scripting.Functions.PSEvalVariableFunction.Evaluate(IVariableFunctionContext context)
       at Inedo.BuildMaster.BuildMasterVariableEvaluationContext.<TryEvaluateFunctionAsync>d__11.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Inedo.ExecutionEngine.Variables.FunctionTextValue.<EvaluateAsync>d__12.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Inedo.ExecutionEngine.Variables.ProcessedString.<EvaluateValueAsync>d__15.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Inedo.ExecutionEngine.Executer.ExecuterThread.<EvaluateExpressionAsync>d__76.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Inedo.ExecutionEngine.Executer.ExecuterThread.<ExecuteAsync>d__53.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Inedo.ExecutionEngine.Executer.ExecuterThread.<ExecuteNextAsync>d__37.MoveNext()
    

    For PSEval, I don't see any issue with ')'.

    Can you please advice.
    Thanks


  • inedo-engineer

    Hi @ashah_4271,

    I'm not sure why this syntax error is happening, but I have a work-around for you. Can you please try:

    ##AH:UseTextMode
    
    
    # Call DELETE on ElasticSearch
    with retry = 3
    {
        set $indexPSScript = >> (Get-Date).AddMonths(-1).ToString("yyyy.MM.*") >>;    
        set $indexName = $PSEval($indexPSScript);
    
        Get-Http $elasticUrl/$indexName
        (
            Method: DELETE
        );
    }
    

    Thanks,
    Dan



  • @Dan_Woolf,
    that seems to be working..
    What does >> mean? I can define multi-line powershell script within that block and then call PSEval to run that code block?

    Thanks a million for your assistance.


  • inedo-engineer

    Hi @ashah_4271,

    In summary, >> is SWIM syntax for OtterScript to create a string literal that allows multi-line strings. You could also use set $indexName = $PSEval('(Get-Date).AddMonths(-1).ToString("yyyy.MM.*")'); because that PowerShell is a single line and only has double quotes in it. I find the SWIM syntax works a bit better when embedding PowerShell inside of Otter Script because it removes risk when using single or double quotes within PowerShell. This is what I mixed up the first time. Simple PowerShell can normally use implicit string literals in the $PSEval function, but since there are double quotes, that breaks implicit string literals.

    You can learn more about string literals in OtterScript in our Strings & Values documentation.

    Thanks,
    Dan



  • @Dan_Woolf,
    Thanks. This will help me a lot.


Log in to reply
 

Inedo Website HomeSupport HomeCode of ConductForums GuideDocumentation