InlineScripts and Variables in PowerShell Workflows

Quick Tutorial:

1) Note that not all PowerShell operations translate into Workflow activities
2) Place all PowerShell-only cmdlets into an InlineScript block
3) Pull Workflow variables into the InlineScript block by prefixing the variable with $Using:
4) Save the output of the inline script by assigning it to a Workflow variable

Workflow InlineScriptExample
{
    $WorkflowVariable
    $CapturedScriptOutput = Inlinescript
    {
        $Using:WorkflowVariable
    }
}

 


Full Tutorial:

In order to understand variables as they apply to workflows in the context of PowerShell scripting, it is necessary to realize that workflows are not typical PowerShell scripts. Workflows (As apposed to functions) utilize the Windows Workflow Foundation engine for managing activities. You can express many Workflow activities using the same statements already used in PowerShell (Thanks Microsoft!). For example, the functionally equivalent PowerShell function:

function test {
    Get-WmiObject win32_Processor
}

would be expressed as the workflow:

Workflow test {
    Get-WmiObject win32_Processor
}

The syntax is the same, the output is the same, but the way we arrived is different. This is all important to understand because it will help us to comprehend how variables can be moved between workflows and typical PowerShell scripts so that we can take advantage of the functionality of both.

When do I need to utilize PowerShell capabilities in my Workflow?

Suppose I want to perform a ToUpper() operation on a string I am using in my workflow.ToUpper() is a PowerShell operation that does not translate into a Windows Workflow activity. If I try to perform the operation, PowerShell ISE will instruct me to include the statement in an InlineScript block.

Workflow test
{
    InlineScript
    {
        #Insert PowerShell only commands here
    }
}

When I use an InlineScript, I am literally launching a PowerShell instance to do the work that I can’t do within the workflow context. For this reason, all variables must be hand-delivered (figuratively speaking) to the InlineScript, and any data we want to save out must be returned to the Workflow.

Passing Workflow Variables to InlineScripts and Capturing Return Values

Now that we understand what an InlineScript block is really doing, working with variables within it becomes simple. If I have a variable that I need to use within the InlineScript, I need to use the “Using:” scope modifier. The variable is then passed in. If then I need to get a value out of the InlineScript, I return the variable I need. For Example:

Workflow Get-AirForceValues
{
    $Values = "Integrity-Service-Excellence"

    #Starts InlineScript activity invoking a new process
    #Saves return data to $Values
    $Values = InlineScript
    {
        #Passes the $Values variable to the InlineScript
        $NewValues = $Using:Values

        $NewValues = $NewValues.Toupper()
        $NewValues
    }
    $Values
}

Running this Workflow then returns the string “INTEGRITY-SERVICE-EXCELLENCE”. You can see that we were able to make a workflow variable visible to the InlineScript and then get back data when the InlineScript terminated.

Leave a Reply

Your email address will not be published. Required fields are marked *