Download - Intro to PowerShell Workflow
![Page 1: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/1.jpg)
Intro to PowerShell WorkflowJEFF HICKS
![Page 2: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/2.jpg)
AgendaWhat is Workflow?
Workflow Syntax
Running Workflows
Practical Workflows
Best Practices
![Page 3: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/3.jpg)
Think!
Just because you can use a PowerShell workflow doesn’t mean you should.
![Page 4: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/4.jpg)
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
![Page 5: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/5.jpg)
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
![Page 6: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/6.jpg)
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.
![Page 7: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/7.jpg)
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
![Page 8: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/8.jpg)
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
![Page 9: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/9.jpg)
Requirements: RemotingWorkflows connect to machines using WSMan protocol
Connects to the default Workflow session configurationPS C:\> get-pssessionconfigurationmicrosoft.powerShell.workflow
Important defaults:◦ Max persistence storage 10GB
◦ Max memory per shell 1GB
◦ Admin permissions required
Be very careful of changing this configuration
![Page 10: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/10.jpg)
High LevelWorkflow Configure-Server {#magic happens
}
PS C:\> configure-server –pscomputername SRV01
Script converted to
Workflow XAML
Workflow endpoint
Workflow engine
![Page 11: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/11.jpg)
Limitations and Gotchas
All objects and data must be
"serializable"
Must use full cmdlet andparameter names –these are activities
No positional
parameters
No Begin/Process/End scriptblocks
No "eventing"
![Page 12: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/12.jpg)
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!
![Page 13: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/13.jpg)
Workflow Architecture
Workflow Engine PowerShell Client
Session
PowerShell
Workflow
“Service”
WinRM Client
Client
PSRP/PowerShell
Managed Node
Remoting
Commands/API
Operation Host Process
Operations
Host
WSMan/CIM
Managed Node
![Page 14: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/14.jpg)
Building Workflows
Don't simply replace
"Function" with
"Workflow"
Start new and plan out your activities
Minimize sharing of
data or variables
across activities
PowerShell turns your workflow into XAML
Workflow is a new
command type in
PowerShell 3.0
PS C:\> get-command -commandtype Workflow
![Page 15: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/15.jpg)
Syntax: Sequence
Execute a collection of activities in order
Can be executed with Parallel
Watch out for scope!
![Page 16: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/16.jpg)
Syntax: SequenceSequence {
MyCommand1
}
Sequence {
MyCommand2
MyCommand3
MyCommand4
}
This command runs
Then these commands run in sequence
![Page 17: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/17.jpg)
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
![Page 18: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/18.jpg)
ParallelParallel {
MyCommand1
MyCommand2
MyCommand3
Sequence {
MyCommand4
MyCommand5
} #sequence
} #parallel
These commands run in sequence
Commands and sequence run
simultaneously
![Page 19: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/19.jpg)
ForEach -ParallelForEach -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
![Page 20: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/20.jpg)
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
![Page 21: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/21.jpg)
InlineScriptWorkflow New-Audit {Inlinescript {
$procs = Get-WmiObject Win32_process
$procs | Select-Object ProcessID,Name,
@{Name="RunTime";Expression={(Get-Date) -$_.ConvertToDateTime($_.CreationDate)}}, @{Name="Owner";Expression={"$($_.GetOwner().Domain)\$($_.GetOwner().User)"}}}
}
![Page 22: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/22.jpg)
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
![Page 23: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/23.jpg)
Syntax: Common Parameters
All workflows have a set of
common parameters They do not need
to be defined
•PSComputerName
•PSCredential
•PSConnectionRetryCount
•PSActionRetryCount
•PSPersist
![Page 24: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/24.jpg)
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
![Page 25: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/25.jpg)
Running Workflows
Load workflow
Specify remote computers
Specify credentials
Run as a job
Use a Workflow session
![Page 26: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/26.jpg)
Invoke-AsWorkflowRun 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
![Page 27: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/27.jpg)
Workflow SessionsRun 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
![Page 28: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/28.jpg)
Best PracticesTry 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
![Page 29: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/29.jpg)
ResourcesThe Lonely Administrator (http://jdhitsolutions.com/blog)
PowerShell in Depth
http://PowerShell.org
http://blogs.technet.com/b/heyscriptingguy
![Page 30: Intro to PowerShell Workflow](https://reader033.vdocument.in/reader033/viewer/2022052623/559f799f1a28abba5c8b4706/html5/thumbnails/30.jpg)
Thank You
http://jdhitsolutions.com/blog
@JeffHicks
http://plus.google.com/+JefferyHicks