December 19th, 2013
0 reactions

Windows PowerShell Script Workflow Debugging

PowerShell Team

Script workflow debugging support is one of two major script debugging enhancements added in Windows PowerShell 4.0. The other new feature is script debugging in remote sessions, which is described in “Windows PowerShell Remote Debugging”.

We introduced script workflow in Windows PowerShell 3.0. These are workflows written in the Windows PowerShell language that are compiled into XAML and then run in Windows Workflow Foundation. There is a subset of Windows PowerShell cmdlets that can be called inside a workflow and are executed as special Windows PowerShell workflow activities when the workflow runs. For a nice introduction to Windows PowerShell workflows see: PowerShell Workflows: The Basics and Getting Started with Windows PowerShell Workflow.

Your only real option for debugging script workflows in Windows PowerShell 3.0 was to look at trace output and add Write-Debug and Write-Verbose statements to your script workflow (Debug or Troubleshoot a Workflow). But in Windows PowerShell 4.0, we now allow you to set line breakpoints in script workflows. When you hit a breakpoint the execution of the next workflow activity is paused, allowing you to see the current point of workflow execution and examine variables, in much the same way that normal Windows PowerShell script debugging works. You don’t need to know anything about the intermediate XAML code that was generated for you that runs the workflow. We map execution of the activity back to the original script workflow you created so you can debug those script statements and forget about the XAML.

Script workflow debugging is integrated into Windows PowerShell’s script debugger so that the experience is as similar as possible to normal Windows PowerShell script debugging. You can add, remove, enable, and disable line breakpoints in script workflows and those breakpoints will halt the activity execution that corresponds to the script statement. You can also step into, out of and through workflows from Windows PowerShell script.

Script workflow debugging supports line breakpoints, but it does not support command or variable breakpoints. Conditional line breakpoints are supported by using the Action parameter in the line breakpoint. The value of the Action parameter is a script that is evaluated when script execution reaches the line. The Action parameter script uses the ‘break’ keyword to stop script execution at that point. For example a simple line breakpoint Action script could be:

if ($count -eq 5) { break }

Script execution will stop at the line breakpoint when the $count variable is equal to 5.

Workflow debugging is fully supported in Windows PowerShell ISE, as well as in the Windows PowerShell console.

Example 1: Debugging a workflow

In this example, I’ll show you some basic tasks in debugging a script workflow. We’ll set a line breakpoint and, at the breakpoint, we’ll display the statements that are executing and set a new conditional breakpoint. In this example we use the WFExample.ps1 script, which contains a workflow and a command that calls the workflow.