DevOps | Scripts | Automation

PowershellWorkflow

What are the PowerShell Workflow methods?

In the previous article we learned what is the PowerShell workflow, how does it work, and why we need PowerShell workflow. In this article, we will expand Workflow functionality by introducing its methods. There are few methods supported by the Windows PowerShell Workflow to design the script to make it a combination of parallel and sequential.

Below are the techniques supported by Windows PowerShell WorkFlow.

  • Sequence
  • Parallel
  • InlineScript
  • Foreach -Parallel

a) Sequence

Whatever you write inside the sequence block will be executed sequentially. For example,

Workflow SequenceTest{
    sequence{
        "Line1"
        "Line2"
        "Line3"
        "Line4"
        "Line5"
    }
}

SequenceTest

All the lines are executed in the sequence.

b) Parallel

Inside the Parallel block, commands will be executed in the random order. There is a no specific sequence.

Workflow ParallelTest{
    Parallel{
      "Line1"
      "Line2"
      "Line3"
      "Line4"
      "Line5"
    }
}

ParallelTest

In the above example, you won’t find any difference because at the small scale Parallel block doesn’t make much difference but once we write multiple lines of commands which take time for the execution, the parallel block produces the output of the command that gives the output earlier.

c) Foreach -Parallel

Foreach -Parallel is different from the Foreach-Object -Parallel (used in PSv7 or above). It runs the loop parallelly and one of the most helpful features in Workflow construction.

We have a Servers.txt stored at the C:\temp and we will retrieve each server name parallelly using the foreach -parallel loop as shown below.

Writing a workflow with foreach -parallel loop.

Workflow ParallelLoopTest{
    foreach -parallel($server in (Get-Content C:\Temp\Servers.txt)){
        Write-Output "Server: $server"
    }
}

ParallelLoopTest

Outputs are not in the sequence.

d) InlineScript

In the earlier article, we discussed that there are few limitations of the workflow. It can’t run all the commands that are supported by PowerShell for example, Invoke-Command, Write-Host, Try-Catch block, etc. InlineScript block was introduced to overcome those problems. Although passing variables to it from the outside block and storing value sometimes becomes tricky but this is very helpful when we write a script.

Workflow InlineFlow{
   Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock{
       $ser = Get-Service -Name WInRm
       Write-Output "Service $($ser.Name) is in $($ser.Status) status"
   }
 
}

InlineFlow

The above example will generate an error output because Invoke-Command is not supported directly inside the workflow. We need to use the InlineScript block to run the above command.

Workflow InlineFlow{
  InlineScript{
         Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock{
        $ser = Get-Service -Name WInRm
        Write-Output "Service $($ser.Name) is in $($ser.Status) status"
   }
  }
}

InlineFlow

In the next article, we will learn how to pass the variables and combining the multiple methods together.