Debugging PowerShell:$PSBoundParameters在调试上下文中不可用

Debugging PowerShell:$PSBoundParameters在调试上下文中不可用,debugging,powershell,scripting,Debugging,Powershell,Scripting,如果在PowerShell调试会话(例如PowerShell ISE或Quest PowerGUI脚本编辑器)期间尝试检查PowerShell$PSBoundParameters自动变量,则无法检索其值。但是,如果我只允许函数将$PSBoundParameters对象回显到管道中,它将按预期呈现 有人知道这是为什么吗?我希望能够在调试会话期间检查所有范围内变量,无论它们是自动的还是用户定义的。如果我将其分配给一个变量,并像下面这样查看该变量,似乎对我有效: function Test-PSBou

如果在PowerShell调试会话(例如PowerShell ISE或Quest PowerGUI脚本编辑器)期间尝试检查PowerShell
$PSBoundParameters
自动变量,则无法检索其值。但是,如果我只允许函数将
$PSBoundParameters
对象回显到管道中,它将按预期呈现


有人知道这是为什么吗?我希望能够在调试会话期间检查所有范围内变量,无论它们是自动的还是用户定义的。

如果我将其分配给一个变量,并像下面这样查看该变量,似乎对我有效:

function Test-PSBoundParameters {
    [CmdletBinding()]
    param (
        [string] $Bar
    )

    $test = $PSBoundParameters
    $test | select *
}

Test-PSBoundParameters -Bar "a"

调试时我无法检查
$PSBoundParameters
,但我可以检查
$test
。我不知道为什么会这样,但至少您可以将其作为一种解决方法。

您可以在中获得有关
$PSBoundParameters
的更多信息。此变量仅在声明参数的范围内具有值。因此,就PowerGui而言,我可以在调试期间看到这个var的值,正如您在下面看到的

您在
[DBG]
中什么也看不到,因为由于一个没有参数的函数,您处于一个相互作用的位置。

以下是原因:

显示脚本变量的值 在调试器中,还可以输入命令,显示 变量的值,使用cmdlet,并在命令行上运行脚本。 您可以显示脚本中所有变量的当前值 正在调试,以下自动变量除外: $_ $Args $Input $MyInvocation $PSBoundParameters 如果您试图显示这些变量中任何一个的值,您将得到 调试器使用的内部管道中的该变量的值,而不是 脚本中变量的值。 要为正在调试的脚本显示这些变量的值, 在脚本中,将自动变量的值指定给新变量。 然后可以显示新变量的值。
嗯,这是一个很好的解决办法,但它仍然没有回答核心问题,这就是为什么该变量在调试上下文中不可用。但是为什么$PSCmdlet变量在调试上下文中可用?这与$PSBoundParameters的情况相同。。。它是只存在于函数作用域中的自动变量。因为[DBG]是通过调用函数而不是Cmdlet获得的,所以$PSCmdlet不会被覆盖。在我的示例中,$PSCmdLet是$null。我认为在您的示例中,$PSCmdLet是$null,因为您没有在函数上使用[CmdletBinding()]属性。您的函数称为“v1”函数,而不是v2高级函数。 Displaying the Values of script Variables While you are in the debugger, you can also enter commands, display the value of variables, use cmdlets, and run scripts at the command line. You can display the current value of all variables in the script that is being debugged, except for the following automatic variables: $_ $Args $Input $MyInvocation $PSBoundParameters If you try to display the value of any of these variables, you get the value of that variable for in an internal pipeline the debugger uses, not the value of the variable in the script. To display the value these variables for the script that is being debugged, in the script, assign the value of the automatic variable to a new variable. Then you can display the value of the new variable.