180 likes | 668 Views
Windows PowerShell. Introducing Workflows. Trevor Sullivan | Solution Architect | Project Leadership Associates. Workflow Requirements. PowerShell version 3.0 or later PowerShell Remoting enabled TCP 5985 and TCP 5986 (SSL) Microsoft.PowerShell.Workflow PSSession Configuration.
E N D
Windows PowerShell Introducing Workflows Trevor Sullivan | Solution Architect | Project Leadership Associates
Workflow Requirements • PowerShell version 3.0 or later • PowerShell Remoting enabled • TCP 5985 and TCP 5986 (SSL) • Microsoft.PowerShell.WorkflowPSSession Configuration
When to use Workflows We can take user input …. Hey, don’t forget about functions! Writing a process that is: • Long-running • Repeatable • Frequent • Parallelizable • Interruptible • Stoppable • Restart-able
Creating Workflows workflow Do-Something { param ( [int] $Interval , [int] $Param2 ) Run-Activity; } Do-Something
Activities vs. Cmdlets • Workflows are made up of Activities, not Cmdlets • Most core cmdlets converted to Activities • Some cmdlets explicitly excluded • Use InlineScript { … } to run non-Activity code • InlineScript { … } is an Activity, so it can be called directly inside the Workflow • If cmdlet is not explicitly excluded, it will implicitly run via InlineScript • Activities cannot use Dynamic Parameters • Activities have common parameters not available on cmdlet counterparts • Some activities can be called remotely without using Invoke-Command
Automatic Parameters -AsJob: Workflow Jobs -ComputerName: Invoke workflows remotely -PSPersist: Instructs Workflow engine to persist Workflow data to disk -PSComputerName: The name of the remote computer(s) to execute the Workflow on -PSConfigurationName: Optionally specify an alternate PSSession Configuration -PSConnectionRetryCount: Number of times that a remote connection will be attempted -PSConnectionRetryIntervalSec: Interval, in seconds, to delay connection retries -PSCredential: The credential that will be used to create remote connections -PSElapsedTimeoutSec: The maximum length of time that the Workflow should run
Workflow Persistence • Workflows are persisted to disk (by default)under: %LocalAppData%\Microsoft\Windows\PowerShell\WF\PS\default • How do I persist a workflow? • Checkpoint-Workflow • -PSPersistparameter • Suspend-Workflow • Use checkpoints sparingly for performance • Suspend-Workflow returns a PSWorkflowJob (even if -AsJob not specified)
Workflow Persistence • V.xml = Version of .NET Framework, Compression, Encryption • Def\VD.xaml = XAML representation of Workflow • Other files are compressed in a binary format, and are not human-readable XAML = eXtensible Application Markup Language
InlineScript InlineScript { } Begin InlineScript { } End Workflow Process Workflow Process • Runs in a separate process by default • Override this behavior by using a custom PSSession Configuration • Activities must use named (not positional) parameters (prior to PowerShell v4) • PowerShell commands inside InlineScript { … } are exempt • Try .. Catch blocks work directly inside of Workflow • Try .. Catch only catches terminating errors (not non-terminating errors)
Workflow: Parallel Tasks Use Parallel { … } inside of a Workflow to run Activities in parallel You can use multiple Parallel { … } blocks in a single Workflow Use Sequence { … }inside of a Parallel { … } block to run activities sequentially Use the ForEach -Parallel statement inside of a Workflow
Workflow Execution Options • New-PSWorkflowExecutionOption (creates custom PSSession configuration) • Encryption • Max persistence store size (in GB) • Path to persisted Workflow store • Allow specific activities • PSSession timeout (after Workflow is suspended)
Workflow Debugging Client(PowerShell 4.0) Server(PowerShell 4.0) Command Variable Line Breakpoints: Limitations Cannot debug nested workflows Cannot debug inside of individual activities Cannot debug Workflows executed as PowerShell background jobs MSDN – Debugging PowerShell Workflows
Resources • Hey, Scripting Guy! Blog from Microsoft • Richard Siddaway has a Workflow series from late 2012 http://goo.gl/f6sU3B • Help about_Workflows • Help about_WorkflowCommonParameters • Help about_Remote*
Contact pcgeek86@gmail.com tsullivan@projectleadership.net http://trevorsullivan.net http://projectleadership.net/blogs.php http://twitter.com/pcgeek86