Script debugging

Debugging in PowerShell is similar to that in other programming languages. Script debugging is available in the console host as well as in the PowerShell ISE; ISE has both GUI and cmdlet-based debugging, whereas console has only cmdlets to debug your scripts.

The debugging feature in the PowerShell ISE is available under the Debug tab, which is self-explanatory. Let's take a look at the breakpoints and debug cmdlets:

(Get-Command -Name *Debug*).Where({$_.Source -ne 'Azure' -and $_.CommandType -eq 'cmdlet'})
Get-Command -Name *BreakPoint

Note that in the first snippet, I used the where method to ignore Azure and to get only the core cmdlets; the output is illustrated in the following image:

Script debugging

Managing breakpoints

Windows PowerShell has three types of breakpoints. They are as follows:

Line breakpoints

In this, the script pauses when the designated line is reached during the operation of the script. For example, run the following command:

$line = Set-psbreakpoint -script C:TempScriptdemo.ps1 -Line 4
$line | fl

The output is as follows:

Id       : 6
Script   : C:TempScriptdemo.ps1
Line     : 4
Column   : 0
Enabled  : True
HitCount : 0
Action   :  
Line breakpoints

Variable breakpoints

In this, the script pauses whenever the designated variable's value changes. Run the following command:

$Variable = Set-psbreakpoint -script C:TempScriptdemo.ps1 -Variable Server
$Variable

Command breakpoints

In this, the script pauses whenever the designated command is about to be run during the operation of the script. It can include parameters to further filter the breakpoint to only the operation that you want. The command can also be a function that you create. Run the following command:

$cmd = Set-PSBreakpoint -Command Get-Service
$cmd | Enable-PSBreakpoint

The output is illustrated in the following image:

Command breakpoints

The Disable-PSBreakpoint cmdlet disables the breakpoint temporarily, whereas the Remove-PSBreakpoint cmdlet removes the breakpoint permanently.

Debugging scripts

After setting up the required breakpoints, we can debug the scripts by simply executing the PS1 file. In this example, we have enabled only the variable breakpoint. Take a look at the following image:

Debugging scripts

The points marked in the figure are explained in the following list:

  • 1: Here, the variable breakpoint is hit for $server. We see it twice because $server is used in two places. To step over use the short cut key S.
  • 2: Now, we are in debug mode.

When we do step over, the script in ISE looks like the following image:

Debugging scripts

In Windows PowerShell 5.0, Wait-Debugger and Debug-Job are the two new debugging cmdlets.

  • Debug-Job: This debugs a running background, remote machine, or Windows PowerShell Workflow job
  • Wait-Debugger: This stops a script in the debugger before running the next statement in the script, as shown in the following image:
    Debugging scripts

In the following example, we have used the Set-PSBreakPoint cmdlet instead of the Wait-Debugger cmdlet:

$job = Start-Job -ScriptBlock { Set-PSBreakpoint C:TempScriptdemo.ps1 -Line 4; C:TempScriptdemo.ps1 }
$job
Debugging scripts

In the figure we can see that the state is AtBreakpoint.

The following cmdlet will enter debug mode of the running job:

Debug-Job $job -Verbose

Windows PowerShell 5.0 makes it easy for us to debug the PowerShell scripts. Using the Set-PSBreakPoint cmdlet, we can pause the execution of the script at a particular line before we proceed further. Just to avoid the risk! Debugging is easy in Windows PowerShell!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset