打印每个参数名称及其值

时间:2014-04-10 16:19:09

标签: powershell

我想打印我的脚本收到的所有参数(名称/值),用于诊断目的。但我不想一个一个地命名 - 这太无聊了,维护也很复杂。有没有办法迭代参数列表并打印它们的值?

1 个答案:

答案 0 :(得分:4)

自动变量$ PSBoundParamters应该会给你。

来自Get-Help about_automatic_variables

$PSBoundParameters
   Contains a dictionary of the parameters that are passed to a script
   or function and their current values. This variable has a value only
   in a scope where parameters are declared, such as a script or function. 
   You can use it to display or change the current values of parameters 
   or to pass parameter values to another script or function.

   For example:

     function Test {
        param($a, $b)

        # Display the parameters in dictionary format.
        $PSBoundParameters

        # Call the Test1 function with $a and $b.
        test1 @PSBoundParameters
     }