How can we use variables inside the inline script in another inline script in PowerShell workflow in Azure runbooks

Ponkam, Sumanth (Cognizant) 50 Reputation points
2024-05-22T17:36:04.3233333+00:00

“In my Runbook workflow, I’d like to use the variables created in one inline script block within another inline script block.” Please suggest how can do this

Workflow

{

inline script

{

$a=1

}

inline script

{

$b=$a -- fist inline block variable

write-output $b

}

}

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,159 questions
0 comments No comments
{count} votes

Accepted answer
  1. AnuragSingh-MSFT 20,986 Reputation points
    2024-05-23T04:55:28.02+00:00

    @Ponkam, Sumanth (Cognizant), You cannot directly pass variables/values from one inlinescript to another inlinescript in PowerShell workflows. To achieve this requirement, a variable at workflow scope is required which can be accessed through $using scope modifier.

    The following example workflow shows a sample scenario:

    workflow test
    {
        $a = 0 #variable at workflow scope
        $a = InlineScript 
             {
                $b = 3;
                $b  #returned value gets assigned to workflow variable "$a"
             }
      
    
       $a = InlineScript
            {
                $b = $Using:a - 1;
                $b
            }
       Write-Output "New value of variable 'a' = $a"
    }
    test
    

    For more details, see variables in InlineScript

    Hope this helps.

    If the answer did not help, please add more context/follow-up question for it. Else, if the answer helped, please click Accept answer so that it can help others in the community looking for help on similar topics.


0 additional answers

Sort by: Most helpful