WorkFlow sessions (Advanced)
Windows PowerShell 3.0 has the capability to maintain workflows and execute them in the environment. It is built on Windows Workflow Foundation, which is written in XAML.
Getting ready
WorkFlow
is an enhanced version of a function where you can create an execution sequence by using various command statements. It maintains a reliable session throughout the execution, which serves the reboot and disconnection of network problems in between the execution.
How to do it...
There are two ways to execute a workflow in the console:
Using
Invoke-AsWorkFlow
Using
New-PSWorkflowSession
Note
You can create a workflow using the
Workflow
keyword.For example:
Workflow <WorkFlowName> { # param block # logic block }
The following command statement creates a new PowerShell workflow session named
PSTestWorkFlow
on the computer namedPSTest
using thePSDomain\PSAdmin
privilege with the concurrent connection count as100
:PS C :\> New-PSWorkflowSession -ComputerName PSTest -Name PSTestWorkflow -Credential PSDomain\PSAdmin -ThrottleLimit 100
Tip
The
New-PSWorkflowSession
CMDLET's alias isnwsn
.The following command statement creates a new session configuration object with defined values for these parameters:
MaxPersistenceStoreSizeGB
,MaxRunningWorkflows
,MaxDisconnectedSessions
,MaxConnectedSessions
, andWorkflowShutdownTimeoutMSec
:PS C:\ > New-PSWorkflowExecutionOption -MaxPersistenceStoreSizeGB 20 -MaxRunningWorkflows 10 -MaxDisconnectedSessions 50 -MaxConnectedSessions 20 -WorkflowShutdownTimeoutMSec 1000 SessionThrottleLimit : 100 PersistencePath : C:\Users\Harshul\AppData\Local\Microsoft\Windows\PowerShell\WF\PS MaxPersistenceStoreSizeGB : 20 PersistWithEncryption : False MaxRunningWorkflows : 10 AllowedActivity : {PSDefaultActivities} OutOfProcessActivity : {InlineScript} EnableValidation : True MaxDisconnectedSessions : 50 MaxConnectedSessions : 20 MaxSessionsPerWorkflow : 5 MaxSessionsPerRemoteNode : 5 MaxActivityProcesses : 5 ActivityProcessIdleTimeoutSec : 60 RemoteNodeSessionIdleTimeoutSec : 60 WorkflowShutdownTimeoutMSec : 1000
Note
If we don't supply any parameter to the
New-PSWorkflowExecutionOption
CMDLET, it creates an output with all the default values specified in the console.
How it works...
The following are the CMDLETs that are responsible for executing the PowerShell Workflow.
New-PSWorkflowSession
The New-PSWorkflowSession
CMDLET creates a PowerShell session to dedicatedly run workflows. This CMDLET provides all the parameters that come with the New-PSSession
CMDLET. This CMDLET uses the Microsoft.PowerShell.Workflow
session configuration that has all the relevant information to run PowerShell workflows.
New-PSWorkflowExecutionOption
The New-PSWorkflowExecutionOption
CMDLET is useful to create custom session configuration options for PowerShell workflow sessions.
There's more…
The following are a few more workflow enhancements.
Invoke-AsWorkflow
The Invoke-AsWorkflow
CMDLET executes commands or expressions as a workflow in the console. The following are its parameters:
-CommandName <String>
: This parameter specifies the CMDLET name or the function name that executes as the workflow-Expression <String>
: This parameter specifies the utility name or the expression that executes as the workflow-Parameter <Hashtable>
: This parameter passes parameter names and values (defined in the hash table) to CMDLET or the function that is specified with theCommandName
parameter
Common parameters of WorkFlow
Common parameters of WorkFlow
provide an extension to default parameters that cover almost all the necessary activities within a workflow execution. These common parameters are availed by the Windows PowerShell WorkFlow Engine. The following are a few samples of these common parameters:
-AsJob <SwitchParameter>
: This parameter creates a workflow job and returns to the command prompt immediately after the execution. In the background, it creates a parent job and child jobs for the respective targeted computers.-PSComputerName <String[]>
: This parameter specifies the list of computers running the workflow. By default, it takes the name of the local computer as its input.-PSCredential <PSCredential>
: This parameter represents the credential that has the privilege to run the workflow. By default, it takes the current user's credential. This parameter only works when thePSComputerName
parameter is used.-PSPersist <Boolean>
: This parameter saves the state of the workflow for each activity defined in the workflow. The PowerShell Workflow uses this latest saved state in case there is an interruption. This parameter accepts the following three values:Default
: By default, it only saves the state at the beginning and the end of the workflow$True
: It saves the state at the beginning, at the end, and after each activity is performed$False
: It only saves the state if it is specified in the workflow
-PSSessionOption <PSSessionOption>
: It passes advanced configurations toPSSession
by providing thePSSessionOption
object to it. This parameter takes default values from the$PSSessionOption
preference variable.
Note
Asynchronous workflow jobs are no longer deleted when the time-out period that is specified by the PSElapsedTimeoutSec
workflow common parameter has elapsed.
Workflow features introduced in PowerShell 4.0
We can now debug Windows PowerShell workflows and scripts that are running on remote computers.
In case there is a server failure, Windows PowerShell Workflow would again reconnect based on the server's uptime.
We can now limit the connection for the
Forech –Parellel
command statement by using theThrottleLimit
property.Windows PowerShell Workflows has a new valid value,
Suspend
, for theErrorAction
common parameter.A workflow endpoint now automatically closes if there are no active sessions or jobs. This mechanism prevents unnecessary resource consumptions of the workflow server.