PowerShell:$ PSBoundParameters在Debug上下文中不可用

时间:2012-01-26 21:53:13

标签: debugging powershell scripting

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

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

3 个答案:

答案 0 :(得分:18)

这就是为什么,来自about_debuggers

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.

答案 1 :(得分:5)

如果我将它分配给变量并查看变量,它似乎对我有用:

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

    $test = $PSBoundParameters
    $test | select *
}

Test-PSBoundParameters -Bar "a"

调试时我无法检查$PSBoundParameters,但我可以检查$test。我不确定为什么会这样,但至少你可以把它用作解决方法。

答案 2 :(得分:2)

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

enter image description here

你只是在[DBG]内看不到任何内容,因为你因为没有参数的函数而处于一个相互作用的地方。