在 Windows PowerShell 调试环境中可以设置三种断点:变量断点、命令断点和行断点。在以上三种断点中,只有行断点在 Windows PowerShell ISE 调试环境中突出显示并可以使用菜单或键盘快捷方式加以设置。另外两种断点也能进行设置,但需使用 Set-PSBreakpoint cmdlet 从命令窗格进行设置。您可以删除、启用、禁用和列出所有断点。本节介绍如何使用可用菜单在 Windows PowerShell ISE 中执行调试任务,以及如何通过编写脚本从命令窗格中执行更多的命令。
管理断点
设置断点
在 Windows PowerShell 脚本或函数中设置行断点。只有保存脚本后才能在其中设置断点。
设置断点的方法有两种:一是右键单击要在其中设置断点的行,然后单击“切换断点”;二是单击要在其中设置断点的行,然后在“调试”菜单上单击“切换断点”。以下脚本示例演示如何使用 Set-PSBreakpoint cmdlet 从命令窗格中设置变量断点。
# This command sets a breakpoint on the Server variable in the Sample.ps1 script. set-psbreakpoint -script sample.ps1 -variable Server
列出所有断点
显示当前会话中的所有断点。
在“调试”菜单上,单击“列出断点”。以下脚本示例演示如何使用 Get-PSBreakpoint cmdlet 从命令窗格中列出所有断点。
# This command lists all breakpoints in the current session. get-psbreakpoint
删除断点
删除特定的行断点。
删除断点的方法有两种:一是右键单击要在其中删除断点的行,然后单击“切换断点”;二是单击要在其中删除断点的行,然后在“调试”菜单上单击“切换断点”。以下脚本示例演示如何使用 Remove-PSBreakpoint cmdlet 从命令窗格中删除具有指定 ID 的断点。
# This command deletes the breakpoint with breakpoint ID 2. remove-psbreakpoint -id 2
删除所有断点
删除当前会话中的所有断点。
在“调试”菜单上,单击“删除所有断点”。以下脚本示例演示如何使用 Remove-PSBreakpoint cmdlet 从命令窗格中删除所有断点。
# This command deletes all of the breakpoints in the current session. get-breakpoint | remove-breakpoint
禁用断点
禁用特定的行断点。
禁用断点的方法有两种:一是右键单击要在其中禁用断点的行,然后单击“禁用断点”;二是单击要在其中禁用断点的行,然后在“调试”菜单上单击“禁用断点”。以下脚本示例演示如何使用 Disable-PSBreakpoint cmdlet 从命令窗格中删除具有指定 ID 的断点。
# This command disables the breakpoint with breakpoint ID 0. disable-psbreakpoint -id 0
禁用所有断点
禁用当前会话中的所有断点。
在“调试”菜单上,单击“禁用所有断点”。以下脚本示例演示如何使用 Disable-PSBreakpoint cmdlet 从命令窗格中禁用所有断点。
# This command disables all breakpoints in the current console. # You can abbreviate this command as: "gbp | dbp". get-psbreakpoint | disable-psbreakpoint
启用断点
启用特定的行断点。
启用断点的方法有两种:一是右键单击要在其中启用断点的行,然后单击“启用断点”;二是单击要在其中启用断点的行,然后按 F8 或在“调试”菜单上单击“启用断点”。以下脚本示例演示如何使用 Enable-PSBreakpoint cmdlet 从命令窗格中启用特定的断点。
# This command enables breakpoints with breakpoint IDs 0, 1, and 5. enable-psbreakpoint -id 0, 1, 5
启用所有断点
启用当前会话中的所有断点。
在“调试”菜单上,单击“启用所有断点”。以下脚本示例演示如何使用 Enable-PSBreakpoint cmdlet 从命令窗格中启用所有断点。
# This command enables all breakpoints in the current session. # You can abbreviate the command as "gbp | ebp". get-psbreakpoint | enable-psbreakpoint
另请参阅