Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from  Instant Windows PowerShell Guide

Product type Book
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Pages 86 pages
Edition 1st Edition
Languages
Author (1):
Harshul Patel Harshul Patel
Profile icon Harshul Patel
Toc

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

  1. 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
    }
  2. The following command statement creates a new PowerShell workflow session named PSTestWorkFlow on the computer named PSTest using the PSDomain\PSAdmin privilege with the concurrent connection count as 100:

    PS C :\> New-PSWorkflowSession -ComputerName PSTest -Name PSTestWorkflow -Credential PSDomain\PSAdmin -ThrottleLimit 100
    

    Tip

    The New-PSWorkflowSession CMDLET's alias is nwsn.

  3. The following command statement creates a new session configuration object with defined values for these parameters: MaxPersistenceStoreSizeGB, MaxRunningWorkflows, MaxDisconnectedSessions, MaxConnectedSessions, and WorkflowShutdownTimeoutMSec:

    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 the CommandName 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 the PSComputerName 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 to PSSession by providing the PSSessionOption 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 the ThrottleLimit property.

  • Windows PowerShell Workflows has a new valid value, Suspend, for the ErrorAction 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.

You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013 Publisher: Packt ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}