Orchestrate your scripts with workflows – Part 1
Availables within PowerShell v3, workflows were created to bring you a solution to orchestrate and coordinate your multimachines sequences. Workflows are robust, reliable and handle long running tasks!
According my experience, many peoples know about them, but not so many administrators use them. Let’s have a full visit about using them, and building them.
Write a simple workflow
Writting a workflow is like writting a function, it’s really easy !
Workflow Get-ComputerDisk { InlineScript { Get-PSDrive } }
PS F:\PowerShell> Get-ComputerDisk Name Used (GB) Free (GB) Provider Root CurrentLocation PSComputerName ---- --------- --------- -------- ---- --------------- -------------- Alias localhost C 106,27 5,18 C:\ ...ON\Documents localhost Cert \ localhost D ,04 ,06 D:\ localhost E 46,31 2,42 E:\ localhost Env localhost F 205,90 676,78 F:\ localhost Function localhost G G:\ localhost H 177,64 120,45 H:\ localhost HKCU HKEY_CURRENT_USER localhost HKLM HKEY_LOCAL_MACHINE localhost Variable localhost WSMan localhost
Why InlineScript ?
The InlineScript activity runs commands in a shared Windows PowerShell session in a workflow. This activity is valid only in workflows.
It means variables outside the InlineScript can only be used by specifying $using:$yourvar. Exemple from technet page
workflow Test-Workflow { $a = 3 # Without $Using, the $a workflow variable is not visible # in inline script. InlineScript {"Inline A0 = $a"} # $Using imports the variable and its current value. InlineScript {"Inline A1 = $Using:a"} } PS C:\> Test-Workflow Inline A0 = Inline A1 = 3
You can also execute scriptblock on remote target with InlineScript, we’ll see how later in this post.
Advanced workflows technics
Declare parameters
Like a function, you can use parameters with your workflow (of course). By the way, there are limits declaring parameters in a workflow, you can’t use sophisticated parameters, like running test-path, using .net type shortcut like ipadress, etc..
Workflow Test-Workflow { param ( [String]$a = "Ille-Et-Vilaine", [String]$b = "Bzh" ) InlineScript { "Departement: $USING:a" "Region: $USING:b" } }
PS F:\PowerShell> Test-Workflow Departement: Ille-Et-Vilaine Region: Bzh
PS F:\PowerShell> Test-Workflow -a "Test" -b "PowerShell" Departement - Test Region - PowerShell
If you use InlineScripts, you don’t have to declare parameters in param section if you use Workflows common parameters with the scope parameter-level ! You can find the complete list of theses parameters here. I’ll explain the ones i found the most usefull.
Parallelism
Sequences ( a squence is a group of statements) can be used between parallel brackets in order to parallelize them. As an exemple is better than all words, here we go !
workflow Start-ParallelWorkflow { Parallel { # Sequence 1 Sequence { Do-Something } # Sequence 2 Sequence { Do-Something2 } } }
Another possibility available only in workflows is foreach with -parallel switch !
workflow Start-ParallelWorkflow { Foreach -parallel ($a in 1..5) { Write-Host $a } }
Run a workflow
Locally
The easiest solution is to invoke the workflow in the same script like done in the previous example.
Just use the workflow name like you’ll do for a function or a cmdlet !
Remotely
If you want to use remoting in your workflows it’s also possible, and a piece of cake with InlineScript !
To do this, create the following workflow as example
workflow Test-InlineScriptRemoting { InlineScript { Write-Host "I'm running on $Using:PScomputerName" } InlineScript { Write-Host "I'm running on $Using:PScomputerName" } -PSComputerName 127.0.0.1 }
And run it!
Test-InlineScriptRemoting -PSComputerName srvdc01
And taddaah!
PS F:\PowerShell> Test-InlineScriptRemoting -PSComputerName srvdc01 I'm running on srvdc01 I'm running on 127.0.0.1
Ok, this is all for this first part. Next time will talk about:
- Common parameters
- Controlling the flow of your workflows
- Handle XAML in a workflow
- Troubleshooting workflows
Hope you enjoy it!
See ya !