Definitions – What is a
Workflow
A robust multi-machine orchestration
engine
Designed for long running unattended tasks
across potentially thousands of machines
Persistent states can survive reboots and
network interruptions
Previously built in Visual Studio
Definitions - What is
PowerShell Workflow?
• Persistence via check points
• Suspend and Resume capabilitiesRobust
• Parallel tasks
• Connection pooling
• Connection throttling
Performance
and
Scalability
• Use existing cmdlets*
• No need to master XAML or use Visual Studio
• Built in parameters for multi-machine management
PowerShell
Based
Workflow Scenarios
Server deployment
Server configuration/remediation
User provisioning
Private cloud deployments
Sharepoint configuration
Any business workflow that can be orchestrated with command line tools.
Key Workflow Concepts
Workflow is a series of orchestrated activities
Workflow activities are isolated
All data and objects are serialized
Objects are strongly typed
Workflows use static scoping
Requirements
Built on .NET Framework 4.0 and
Windows Workflow Foundation
Requires PowerShell 3.0
Requires PowerShell 3.0 remoting
Leverages PowerShell's job
infrastructure
Requirements: Remoting
Workflows connect to machines using WSMan protocol
Connects to the default Workflow session configuration
PS C:> get-pssessionconfiguration
microsoft.powerShell.workflow
Important defaults:
◦ Max persistence storage 10GB
◦ Max memory per shell 1GB
◦ Admin permissions required
Be very careful of changing this configuration
Limitations and Gotchas
All objects
and data
must be
"serializable"
Must use full
cmdlet and
parameter
names –
these are
activities
No
positional
parameters
No Begin/
Process/End
scriptblocks
No
"eventing"
Limitations and Gotchas
No Traps -
Use
Try/Catch
Not intended
to be
interactive –
don’t use
Write-Host.
No comment
based help -
must use
MAML
formatted
files
Pay close
attention to
scope!
Syntax: Parallel
Execute a collection of activities independently and in parallel
ForEach -Parallel
• The parameter only works in a workflow
• Run a set of commands in parallel for each object in a collection
Parallel key word
• Run a set of commands simultaneously and in parallel
• Runs in a new scope
• Often used to run a series of Sequences
ForEach -Parallel
ForEach -parallel ($item in $object) {
#do something with each object
MyCommand1 $item
MyCommand2 $item
}
For each item in the
collection…
Run these commands
All object processed
simultaneously
Syntax: InlineScript
Send PowerShell commands to remote machine(s)
Runs out-of-process
Runtime command validation
This is really a series of Invoke-Command activities
Use for all other non-activity commands
Syntax: Scope and Variables
Workflows uses static scopes, i.e. PowerShell won't
"search" for a variable
Assume each activity is isolated
Can use $Workflow:MyVar for “global” references
• Read-only
• Available from InlineScript
Access "out of scope" variables with $Using:MyVar
Syntax: Common Parameters
All workflows
have a set of
common
parameters They do not need
to be defined
•PSComputerName
•PSCredential
•PSConnectionRetryCo
unt
•PSActionRetryCount
•PSPersist
Syntax: Persistence
Workflows can be
made persistent to
survive interruptions
Planned reboots
Network interruptions
By default entire
workflow restarts
unless…
Set persistence per
activity
•Checkpoint-Workflow
•Persist
•Suspend-Workflow
Set persistence for
the entire workflow
-PSPersist common
parameter
Set to $True:
-pspersist $true
Invoke-AsWorkflow
Run any command or expression as a workflow
Run as an InlineScript
Executed from the console
Great for ad-hoc remote management without writing a workflow
Invoke-AsWorkflow -CommandName get-eventlog -
Parameter @{Logname="System";Newest=10;
Entrytype="Error"} -pscomputername $computers -asjob
Workflow Sessions
Run Workflows from within a special PSSession
Workflows can be monitored and managed from within the session
Great for long running workflows or those that might be suspended and
resumed
Session can be disconnected and reconnected
Best Practices
Try to test locally and interactively
Use Write-Verbose for tracing and troubleshooting
Use a workflow-aware editor like the PowerShell ISE
Take advantage of parallelism
Plan your activities…this is not just another way to script
Resources
The Lonely Administrator (http://jdhitsolutions.com/blog)
PowerShell in Depth
http://PowerShell.org
http://blogs.technet.com/b/heyscriptingguy