2012-01-26 16 views
10

Se si tenta di esaminare la variabile automatica PowerShell $PSBoundParameters durante una sessione di debug di PowerShell (ad esempio PowerShell ISE o Quest PowerGUI Script Editor), non riesco a recuperarne il valore. Tuttavia, se permetto semplicemente alla funzione di richiamare l'oggetto $PSBoundParameters nella pipeline, viene eseguito il rendering come previsto.

Qualcuno sa perché questo è? Mi aspetto di essere in grado di esaminare tutte le variabili in-scope durante una sessione di debug, che siano automatiche o definite dall'utente.

risposta

3

sembra funzionare per me se assegnarlo a una variabile e guardo la variabile in questo modo:

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

    $test = $PSBoundParameters 
    $test | select * 
} 

Test-PSBoundParameters -Bar "a" 

non ho potuto ispezionare $PSBoundParameters durante il debug, ma ho potuto ispezionare $test. Non sono sicuro del perché questo sia, ma almeno puoi usarlo come un lavoro.

+0

Hmmmm che è un buon work-around, ma ancora non risponde alla questione centrale, che è il motivo per cui la variabile non è disponibile nel contesto di debug. –

2

È possibile avere ulteriori informazioni su $PSBoundParameters in about_Automatic_Variables. Questa variabile ha un valore solo in un ambito in cui vengono dichiarati i parametri. Quindi per quanto riguarda PowerGui posso vedere i valori di questa var durante il debug come puoi vedere qui sotto.

enter image description here

Basta vedere nulla all'interno [DBG] perché non ci si trova in un luogo intereactive a causa di una funzione senza argomenti.

+0

Ma allora perché la variabile $ PSCmdlet è disponibile nel contesto di debug? È la stessa situazione di $ PSBoundParameters ... è una variabile automatica che esiste solo nell'ambito della funzione. –

+0

Solo perché [DBG] è ottenuto dalla chiamata di una funzione e non da un cmdlet in modo che $ PSCmdlet non venga sovrascritto. Guarda nel mio esempio $ PSCmdLet è $ null. – JPBlanc

+0

Penso che nel tuo esempio, $ PSCmdlet sia $ null perché non stai usando l'attributo [CmdletBinding()] sulla tua funzione. La tua funzione è quella che è nota come funzione "v1", non una funzione avanzata v2. –

15

Ecco perché, da 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. 
Problemi correlati