Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



PowerShell 3.0 Workflow

Getting Started with Workflow in PowerShell 3.0PowerShell 3.0 Workflow

This technique for understanding PowerShell's Workflow is very much my own making.  It is designed to illustrate the three keywords 'Workflow, Parallel and Sequence'.

Note: You need PowerShell version 3 or later.

 ♦

Example 1: Simple PowerShell v3 Workflow

The only point of this basic example is to demonstrate how Workflow operates with a cmdlet or 'wrapper'.  Once I grasped this cmdlet / wrapper idea it was relatively straightforward to add 'Sequence' and 'Parallel' sections to my workflow.

# Most Basic PowerShell Workflow Example
Workflow Verb-Noun {
"Stuff!"
}
Clear-Host
Verb-Noun

Important: Nothing happens if you omit the last line: Verb-Noun

Expected result merely:
Stuff!

Example 2a: To Create a Workflow Called Guy-ParaSeq

The underlying PowerShell task here is to execute Get-Process and then Get-Service, but padded with a few literal lines of "In x of y".  In truth, this is an example to learn about workflow rather than performing any useful tasks.

From a Workflow perspective, take the time to trace the three parallel items, and the two sequence items both in the script and in the results.

Principle: Get this example working, then experiment by re-ordering the elements.

# PowerShell 3.0 Workflow Example
Workflow Guy-ParaSeq {
# Commands within Parallel execute in any order
Parallel {
"In parallel 1 of 3"
   Sequence {
   "In sequence 1 of 2"
   Get-Process -Name Power*
   "In sequence 2 of 2"
                   } # Closes Sequence
"In parallel 2 of 3"
Get-Service -Name W32Time
"In parallel 3 of 3"
   }  # Closes Parallel
}  # Closes Guy-ParaSeq cmdlet

Clear-Host
Guy-ParaSeq

Note 1:  Observe how 'In sequence 2 of 2' has to wait for Get-Process to finish, however, 'In parallel 2 of 3' does not have to wait.

Note 2: It's crucial to run Guy-ParaSeq on the last line.

Example 2b: Difference Sequence for Get-Process -Name Power*

# Windows PowerShell Workflow Example
Workflow Guy-ParaSeq1 {
Parallel {
"In parallel 1 of 3"
Get-Process -Name Power*
   Sequence {
   "In sequence 1 of 2"
   "In sequence 2 of 2"
                   }
"In parallel 2 of 3"
Get-Service -Name W32Time
"In parallel 3 of 3"
   }  # Closes Parallel
}  # Closes cmdlet

Clear-Host
Guy-ParaSeq1

Note 3: I hope that these two examples will encourage you to experiment with the order of Parallel and Sequence items.

Free Monitor for Your Network: SolarWinds Real-time Traffic AnalyzerSolarwinds Real-time Traffic Analyzer

The main reason for monitoring your network is to check at a glance which servers are available.  If there is a network problem you want an interface to show the scope of the problem immediately.

Even when all servers and routers are available, sooner or later you will be curious to know who, or what, is hogging the precious network's bandwidth.  A GUI showing the top 10 users always makes interesting reading.

Another reason to monitor network traffic is to learn more about your server's response times and the consumption of resources.  To take the pain out of capturing frames and analysing the raw data, Guy recommends that you download a copy of the SolarWinds free Real-time NetFlow Analyzer.

Example 3: PowerShell Workflow with InlineScript

A number of ordinary PowerShell commands give errors in workflow, the way around these difficulties is to employ InlineScript.

Workflow Guy-ParaSeqInline {
Parallel { Get-Process -Name Svchost*
Sequence {
      "In sequence 1 of 2"
       InlineScript {
       Start-Sleep 10
                  }
       "In sequence 2 of 2"
                 } # Closes Sequence
"In parallel 1 of 2"
"In parallel 2 of 2"
InlineScript {
$a = 9
$b = $a+2
$b
                 }
Get-Service -Name W32Time
    }  # Closes Parallel
}

Clear-Host
Guy-ParaSeqInline

Note 4:  Observe how this script employs Start-Sleep to emphasise the parallel and sequence portions, if you don't see what I mean then experiment by moving the commands around and pay attention to 'In parallel x of y'.

Workflow Background

Good project management takes a task and breaks it down into steps, each with resources and a time-line.  Workflow charts display which tasks can be tackled simultaneously, for instance fuelling cars at a big gas (petrol) station, and which are constrained by the need for activities to complete in sequence, for example, an insurance claim form.

Workflow in PowerShell
In script performance terms we can make gains by identifying which jobs can be executed in parallel.  This pre-supposes that we are comfortable with PowerShell's jobs, a factor which rules out PowerShell novices.

See more tasks for PowerShell »

Summary of PowerShell's Updateable Help

The first point to remember is that this cmdlet is only available on PowerShell v 3.0 and later.  I found Update-Help particularly difficult to configure so that it delivered the package of revised help files.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell v 3.0

PowerShell Tutorials  • What's New in PowerShell 3.0  • PowerShell 3.0 Foreach-Object

PowerShell Show-Command  • Out-GridView -PassThru  • PowerShell Ordered Hash Tables

PowerShell Version 3.0  • PowerShell 3.0 Get-ChildItem  • PowerShell 3.0 Update-Help

 *


Custom Search

Site Home

Guy Recommends: SolarWinds Free IP SLA MonitorSolarwinds IP Sla Monitor

SolarWinds IP SLA Monitor offers so much more than just uncovering network bottlenecks, the real joy is learning about router traffic.

To find out what's happening on the network between your computers and their routers, download your free copy of the of IP SLA Monitor.

Article by: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: