Intro to PowerShell Workflow

Post on 10-Jul-2015

647 views 3 download

Transcript of Intro to PowerShell Workflow

Intro to PowerShell WorkflowJEFF HICKS

AgendaWhat is Workflow?

Workflow Syntax

Running Workflows

Practical Workflows

Best Practices

Think!

Just because you can use a PowerShell workflow doesn’t mean you should.

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: 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

High LevelWorkflow Configure-Server {#magic happens

}

PS C:\> configure-server –pscomputername SRV01

Script converted to

Workflow XAML

Workflow endpoint

Workflow engine

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"

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!

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

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

Syntax: Sequence

Execute a collection of activities in order

Can be executed with Parallel

Watch out for scope!

Syntax: SequenceSequence {

MyCommand1

}

Sequence {

MyCommand2

MyCommand3

MyCommand4

}

This command runs

Then these commands run in sequence

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

ParallelParallel {

MyCommand1

MyCommand2

MyCommand3

Sequence {

MyCommand4

MyCommand5

} #sequence

} #parallel

These commands run in sequence

Commands and sequence run

simultaneously

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

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

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)"}}}

}

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

•PSConnectionRetryCount

•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

Running Workflows

Load workflow

Specify remote computers

Specify credentials

Run as a job

Use a Workflow session

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

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

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

ResourcesThe Lonely Administrator (http://jdhitsolutions.com/blog)

PowerShell in Depth

http://PowerShell.org

http://blogs.technet.com/b/heyscriptingguy

Thank You

http://jdhitsolutions.com/blog

jhicks@jdhitsolutions.com

@JeffHicks

http://plus.google.com/+JefferyHicks