PowerShell – Running Parallel Workflows FAST !

Windows Workflows are really great in particular to execute parallel code.

When would you want to use this ?

Let’s take this example, where you want to know all IP addresses of the PC’s in your network.

It you had to scan them 1 by 1 sequentially it would be a SLOW process.

Therefore the new feature of Windows Workflows in PS v3.0 it the tool to use. In combination with the parallel execution parameter.

But you might run in to this error if you are trying to use Parameters in you Workflow statement.

Positional parameters are not supported in a Windows PowerShell Workflow. To invoke this command, use explicit parameter names with all values. For example: “Command -Parameter <value>”.
+ CategoryInfo          : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : PositionalParametersNotSupported

image

It took me a more then an hour to figure out what this meant and another hour to figure out how to correct it.

I ran this code and got the above error ?

CLS

import-module activedirectory

workflow Invoke-ForEachParallel-WF  {

param([string[]]$computerName) 

 # The contents of the foreach block will be executed in parallel
 # look at the keyword -parallel

 foreach -parallel ($computer in $computerName)
 { # "Executing on $computer"
   test-connection -count 1 $computer
  }

}

Invoke-ForEachParallel-WF  (Get-AdComputer -filter {(Name -Like "srv*") } | select name -ExpandProperty name)

Solution :

CLS

import-module activedirectory

workflow Invoke-ForEachParallel-WF  {

param([string[]]$ComputerName) 

 # The contents of the foreach block will be executed in parallel
 # look at the keyword -parallel

 foreach -parallel ($computer in $computerName)
 { # "Executing on $computer"
   test-connection -count 1 -ComputerName $computer
   # Look at the special syntax -ComputerName + variable $computer
  }

}

Invoke-ForEachParallel-WF  (Get-AdComputer -filter {(Name -Like "srv*") } | select name -ExpandProperty name)

Look at the special extra parameter “-ComputerName” value in the foreach code block

The code runs realy fast and will give you this kind of output.

image

Hope this will save you all some time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: