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!

    Setting runtime variable from powershell script

    Scheduled Pinned Locked Moved Support
    9 Posts 2 Posters 20 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.
    • A Offline
      ashah_4271
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • Dan_WoolfD Offline
        Dan_Woolf inedo-engineer
        last edited by

        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

        A 1 Reply Last reply Reply Quote 0
        • A Offline
          ashah_4271 @Dan_Woolf
          last edited by ashah_4271

          @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
              );
          }
          
          1 Reply Last reply Reply Quote 0
          • Dan_WoolfD Offline
            Dan_Woolf inedo-engineer
            last edited by

            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

            1 Reply Last reply Reply Quote 0
            • A Offline
              ashah_4271
              last edited by

              @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

              1 Reply Last reply Reply Quote 0
              • Dan_WoolfD Offline
                Dan_Woolf inedo-engineer
                last edited by

                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

                1 Reply Last reply Reply Quote 0
                • A Offline
                  ashah_4271
                  last edited by

                  @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.

                  1 Reply Last reply Reply Quote 0
                  • Dan_WoolfD Offline
                    Dan_Woolf inedo-engineer
                    last edited by

                    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

                    1 Reply Last reply Reply Quote 0
                    • A Offline
                      ashah_4271
                      last edited by

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

                      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