Book Image

Instant Windows PowerShell Guide

By : Harshul Patel
Book Image

Instant Windows PowerShell Guide

By: Harshul Patel

Overview of this book

Windows PowerShell has become a booming scripting language over the last couple of years. It has extensive support with an ample number of vendor products, providing a standardized platform for automation and administration. It has massive support for all Microsoft products which creates a layer that can easily automate everything. In the latest version, the PowerShell team has introduced much more functionality with thousands of CMDLETs, part of various modules.This book is a quick reference guide to enable you to get the most out of the latest Windows PowerShell techniques. In this book, you will find new enhancements in the latest version of PowerShell with some helpful examples. This book enables you to quickly move from older versions of PowerShell to Version 3.0 and Version 4.0.This practical, example-oriented book helps you to overcome the difficulty of using and discovering CMDLETs by providing precise information about everything that has been newly introduced in the latest version of Windows PowerShell. It also focuses on the new configuration management system with the help of DSC as a new feature of Windows PowerShell v4.0.You will learn how to use the newly introduced CMDLETs and parameters to perform daily routine tasks. You will also learn how to administer the servers remotely and maintain persistent sessions to provide continuity. You will gain an insight into writing efficient scripts by using various parameters, snippets, and workflows to gain more productivity. You will also be introduced to various modules like CimCmdlets, PSScheduledJob, PSDesiredStateConfiguration, and so on in order to enhance your scripts with the latest instrumentation. Finally this book will make you aware of the capabilities of PowerShell v4.0 and how to fully leverage the functionality introduced in the new version.
Table of Contents (7 chapters)

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.