PowerShell v3: Workflow is the Flagship Feature

PowerShell v3's flagship new feature is called Workflow. It's incredible. Here's how it works.

Don Jones

October 20, 2011

2 Min Read
ITPro Today logo in a gray background | ITPro Today

Of course, this is all subject to my standard PowerShell v3 caveat - keep in mind we're dealing with pre-release code.

One of the much-awaited (for me) new features in PowerShell v3 is called workflow. Essentially, it's a special scripting construct that PowerShell turns into Windows Workflow Foundation (WWF) code, and then executes under WWF. Within a workflow, you can't use PowerShell's Switch construct, because WWF doesn't have an equivalent. But you can use something called parallel foreach, because WWF has its own version of that. Parallel foreach can't be used elsewhere in PowerShell.

Here's an example workflow:

workflow WorkflowParallelForEach1{$counter = 0        foreach ($parallel_xx in 0..10)        {            if( $parallel_xx%2 -eq 0){"IfValue" + $parallel_xx}else{Sleep -seconds 5"ElseValue" + $parallel_xx}$counter += $parallel_xx              }        $counter}WorkflowParallelForEach1


That illustrates the parallel foreach capability. This foreach still enumerates through objects (in this case, the numbers 1 through 10), but it does so in parallel, meaning iterations can happen simultaneously rather than sequentially.

WWF has been around for a while, making its debut in .NET Framework 3.5. Until now, though, it was primarily the domain of developers, who needed a dedicated set of language elements and Visual Studio tools to effectively work with it.

Basically, Workflow is the idea of running longer, more complex scripts that are intended to perform many different tasks, in a specific sequence, over time. Those tasks may be interrupted: The network may fail, computers may need to reboot, and so forth. Workflow "checkpoints" itself as it goes, picking up and resuming the script as needed to "survive" these outages. Microsoft describes workflows as "recoverable and interruptible," as well as parallelized - that latter characteristic being helpful when you need to manage hundreds of computers instead of just one.

The neat part of PowerShell's workflows is that they're easy to write. Essentially, it's just a function that uses the keyword "workflow" instead of "function." From there, PowerShell takes over and generates the necessary WWF code. 

All of this is wrapped into a module, PSWorkflow, that you have to import into memory in order for workflows to function. The module includes a couple of commands designed to work with workflow code, but for the most part you won't need to worry about them. The above code, for example, runs as-is within the PowerShell ISE. The CTP's CAB file includes a dozen or more additional workflow examples, so you can really start to see some of what this amazing new feature can do for you.
 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like