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

Tre*_*van 12 debugging powershell scripting

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

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

Sha*_*evy 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.


And*_*ndi 5

如果我将其分配给变量并像这样查看变量,这似乎对我有用:

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

    $test = $PSBoundParameters
    $test | select *
}

Test-PSBoundParameters -Bar "a"
Run Code Online (Sandbox Code Playgroud)

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