Set-PSDebug
打开和关闭脚本调试功能,设置跟踪级别并切换 strict 模式。
语法
Set-PSDebug [-Off] [<CommonParameters>]
Set-PSDebug [-Step] [-Strict] [-Trace <int>] [<CommonParameters>]
说明
Set-PSDebug cmdlet 打开和关闭脚本调试功能,设置跟踪级别,并切换 strict 模式。
当 Trace 参数设置为 1 时,将在执行脚本的每一行时对其进行跟踪。当该参数设置为 2 时,还将跟踪变量赋值、函数调用和脚本调用。如果指定了 Step 参数,则在执行脚本的每一行之前将会提示您。
参数
-Off
关闭所有脚本调试功能。
注意:“set-strictmode –off”命令禁用“set-psdebug –strict”命令设置的验证。有关详细信息,请参阅 Set-StrictMode。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-Step
打开脚本步进。在运行每一行之前,系统会提示用户停止、继续或输入新的解释器级别以检查脚本的状态。
注意:指定 Step 参数会自动将跟踪级别设置为 1。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-Strict
指定如果在为某个变量赋值之前引用了该变量,解释器应引发异常。
注意:“set-strictmode –off”命令禁用“set-psdebug –strict”命令设置的验证。有关详细信息,请参阅 Set-StrictMode。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
-Trace <int>
指定跟踪级别:
0 - 关闭脚本跟踪
1 - 执行脚本行时跟踪它们
2 - 跟踪脚本行、变量赋值、函数调用和脚本。
是否为必需? |
false |
位置? |
named |
默认值 |
|
是否接受管道输入? |
false |
是否接受通配符? |
false |
<CommonParameters>
此 cmdlet 支持通用参数:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。有关详细信息,请参阅 about_Commonparameters.
输入和输出
输入类型是指可通过管道传递给 cmdlet 的对象的类型。返回类型是指 Cmdlet 所返回对象的类型。
输入 |
无 不能通过管道将输入传递给此 cmdlet。 |
输出 |
无 此 cmdlet 不返回任何输出。 |
示例 1
C:\PS>set-psdebug -trace 2; foreach ($i in 1..3) {$i}
DEBUG: 1+ Set-PsDebug -trace 2; foreach ($i in 1..3) {$i}
DEBUG: 1+ Set-PsDebug -trace 2; foreach ($i in 1..3) {$i}
1
DEBUG: 1+ Set-PsDebug -trace 2; foreach ($i in 1..3) {$i}
2
DEBUG: 1+ Set-PsDebug -trace 2; foreach ($i in 1..3) {$i}
3
说明
-----------
此命令将跟踪级别设置为 2,然后运行显示数字 1、2 和 3 的脚本。
示例 2
C:\PS>set-psdebug -step; foreach ($i in 1..3) {$i}
DEBUG: 1+ Set-PsDebug -step; foreach ($i in 1..3) {$i}
Continue with this operation?
1+ Set-PsDebug -step; foreach ($i in 1..3) {$i}
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):a
DEBUG: 1+ Set-PsDebug -step; foreach ($i in 1..3) {$i}
1
2
3
说明
-----------
此命令打开步进,然后
运行显示数字 1、2 和 3 的脚本。
示例 3
C:\PS>set-psdebug -off; foreach ($i in 1..3) {$i}
1
2
3
说明
-----------
此命令关闭所有调试功能,然后运行显示数字 1、2 和 3 的脚本。
示例 4
C:\PS>set-psdebug -strict; $NewVar
The variable $NewVar cannot be retrieved because it has not been set yet.
At line:1 char:28
+ Set-PsDebug -strict;$NewVar <<<<
说明
-----------
此命令将解释器置于 strict 模式,并尝试访问尚未设置的变量。
另请参阅
概念
about_Debuggers
Debug-Process
Set-PSBreakpoint
Set-StrictMode
Write-Debug