{"id":1901,"date":"2013-12-19T15:36:20","date_gmt":"2013-12-19T15:36:20","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2013\/12\/19\/windows-powershell-script-workflow-debugging\/"},"modified":"2024-02-22T14:43:14","modified_gmt":"2024-02-22T22:43:14","slug":"windows-powershell-script-workflow-debugging","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/windows-powershell-script-workflow-debugging\/","title":{"rendered":"Windows PowerShell Script Workflow Debugging"},"content":{"rendered":"<p>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 \u201c<a href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2013\/12\/19\/windows-powershell-remote-debugging.aspx\"><span style=\"color: #0000ff;\">Windows PowerShell Remote Debugging<\/span><\/a>\u201d.<\/p>\n<p>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: <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/12\/26\/powershell-workflows-the-basics.aspx\"><span style=\"color: #0000ff;\">PowerShell Workflows: The Basics and Getting Started with Windows PowerShell Workflow.<\/span><\/a><\/p>\n<p>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 (<a href=\"http:\/\/www.powershellcookbook.com\/recipe\/VQjF\/debug-or-troubleshoot-a-workflow\"><span style=\"color: #0000ff;\">Debug or Troubleshoot a Workflow<\/span><\/a>). 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\u2019t 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.<\/p>\n<p>Script workflow debugging is integrated into Windows PowerShell\u2019s 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.<\/p>\n<p>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 \u2018break\u2019 keyword to stop script execution at that point. For example a simple line breakpoint Action script could be:<\/p>\n<p>if ($count -eq 5) { break }<\/p>\n<p>Script execution will stop at the line breakpoint when the $count variable is equal to 5.<\/p>\n<p>Workflow debugging is fully supported in Windows PowerShell ISE, as well as in the Windows PowerShell console.<\/p>\n<h5>Example 1: Debugging a workflow<\/h5>\n<p>In this example, I\u2019ll show you some basic tasks in debugging a script workflow. We\u2019ll set a line breakpoint and, at the breakpoint, we\u2019ll 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.<\/p>\n<p><a href=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/7367.image_thumb_137016FF.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-14536\" src=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/7367.image_thumb_137016FF.png\" alt=\"WFExample.ps1\" width=\"724\" height=\"734\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/7367.image_thumb_137016FF.png 724w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/7367.image_thumb_137016FF-296x300.png 296w\" sizes=\"(max-width: 724px) 100vw, 724px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/5127.image_thumb_40F136C2.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-14537\" src=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/5127.image_thumb_40F136C2.png\" alt=\"WFExample.ps1 pt 2\" width=\"727\" height=\"497\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/5127.image_thumb_40F136C2.png 727w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/5127.image_thumb_40F136C2-300x205.png 300w\" sizes=\"(max-width: 727px) 100vw, 727px\" \/><\/a><\/p>\n<p>There are some limitations to workflow debugging.<\/p>\n<p>\u00b7 Nested workflow debugging is not supported. You cannot step into a workflow that\u2019s being called by another workflow. Breakpoints set in a nested workflow will not be hit.<\/p>\n<p>\u00b7 You cannot debug Windows PowerShell activities as they execute.<\/p>\n<p>\u00b7 You can get the value of workflow variables, both built-in and created by assignment expressions, at a debugger stop. (To see the value of any variable, type the variable name or use the Get-Variable cmdlet at the debugger prompt). However, you cannot set a workflow variable at the debugger prompt, like you can when debugging a standard (non-workflow) script.<\/p>\n<p>\u00b7 You cannot debug a script workflow that runs as a job. (To run a workflow script asynchronously as a Windows PowerShell workflow job, use the AsJob common parameter of script workflows). To debug, remove the AsJob parameter from your script temporarily, and put it back when you\u2019re finished debugging.<\/p>\n<h5>Example2: Run a Script Workflow as a job<\/h5>\n<p><a href=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/8765.image_thumb_0E8D6343.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-14539\" src=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/8765.image_thumb_0E8D6343.png\" alt=\"two line breakpoints\" width=\"725\" height=\"721\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/8765.image_thumb_0E8D6343.png 725w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/8765.image_thumb_0E8D6343-150x150.png 150w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/8765.image_thumb_0E8D6343-300x298.png 300w\" sizes=\"(max-width: 725px) 100vw, 725px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/30\/2018\/10\/5460.image_thumb_26B0CD9E.png\"><img decoding=\"async\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/30\/2018\/10\/5460.image_thumb_26B0CD9E.png\" alt=\"image\" width=\"728\" height=\"146\" border=\"0\" \/><\/a><\/p>\n<h4>Debug script workflows on remote machines<\/h4>\n<p>In the previous post I showed how to debug scripts running on remote machines. In this post, I have shown how to debug script workflows. Now I\u2019ll put the two together to show how to debug script workflows on remote machines.<\/p>\n<p>For remote debugging both client and remote machines must be running Windows PowerShell 4.0. Remote debugging is supported only in the Windows PowerShell console and not in Windows PowerShell ISE.<\/p>\n<h5>Example 3: Debugging a workflow in a remote session<\/h5>\n<p>In this example we\u2019ll debug the workflow in the WFExample1.ps1 script. But, unlike our first example, the script is running in a remote session.<\/p>\n<p><a href=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/0285.image_thumb_187254AE.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-14541\" src=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/0285.image_thumb_187254AE.png\" alt=\"remote session\" width=\"732\" height=\"473\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/0285.image_thumb_187254AE.png 732w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/0285.image_thumb_187254AE-300x194.png 300w\" sizes=\"(max-width: 732px) 100vw, 732px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h5>Example 4: Debugging workflow script on multiple remote machines<\/h5>\n<p>You can run workflows on remote machines. By default they run on the local machine, but you can specify one or more remote machines by including the \u2013PSComputerName common parameter. Underneath the covers Windows PowerShell is using its job infrastructure to run the workflow on the remote machines. The workflow debugger handles all of this and modifies the command prompt when you are stopped in the debugger to indicate the machine that the debug stop pertains to. In addition the workflow debugger supports debugging workflows on multiple machines simultaneously. In this case, the debugger stops (whether due to a breakpoint hit or step action) alternate between machines in the order that the debug stop events arrive at the debugger.<\/p>\n<p><a href=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/6177.image_thumb_7F0A5173.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-14543\" src=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/6177.image_thumb_7F0A5173.png\" alt=\"debugger\" width=\"737\" height=\"718\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/6177.image_thumb_7F0A5173.png 737w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/6177.image_thumb_7F0A5173-300x292.png 300w\" sizes=\"(max-width: 737px) 100vw, 737px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/8737.image_thumb_37B4FB81.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-14544\" src=\"http:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/8737.image_thumb_37B4FB81.png\" alt=\"debugger 2\" width=\"738\" height=\"527\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/8737.image_thumb_37B4FB81.png 738w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2013\/12\/8737.image_thumb_37B4FB81-300x214.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/p>\n<p>Script workflow debugging is a powerful new feature that lets you debug workflow script in both local and remote PowerShell sessions. With its integration with PowerShell\u2019s script debugger, it gives you a much improved and seamless way to debug your script workflows.<\/p>\n<p>Paul Higinbotham [MSFT]\nWindows PowerShell Development<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u201cWindows PowerShell Remote Debugging\u201d. We introduced script workflow in Windows PowerShell 3.0. These are workflows written in the Windows PowerShell language that are compiled [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>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 \u201cWindows PowerShell Remote Debugging\u201d. We introduced script workflow in Windows PowerShell 3.0. These are workflows written in the Windows PowerShell language that are compiled [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/1901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=1901"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/1901\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=1901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=1901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=1901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}