5

我想输出已传递给我的脚本以包含在电子邮件中的任何参数的值。

我试过这个:

foreach ($psbp in $PSBoundParameters)
{
    $messageBody += $psbp | out-string + "`r`n"
}

但它没有用。有人可以帮帮我吗?

4

2 回答 2

8

$PSBoundParameters 是一个哈希表,使用 GetEnumerator 展开它的项目

foreach($psbp in $PSBoundParameters.GetEnumerator())
{
    "Key={0} Value={1}" -f $psbp.Key,$psbp.Value
}




function Get-PSBoundParameters
{
    [CmdletBinding()]    
    Param($param1,$param2,$param3)

    foreach($psbp in $PSBoundParameters.GetEnumerator())
    {
            "Key={0} Value={1}" -f $psbp.Key,$psbp.Value
    }
}


PS> Get-PSBoundParameters p1 p2 p3 | ft -a

Key=param1 Value=p1
Key=param2 Value=p2
Key=param3 Value=p3
于 2012-04-26T07:59:13.053 回答
0
function test 
{
    param($a, $b)

    $psboundparameters.Values
    $psboundparameters.Keys
}


test "Hello" "World"
于 2012-04-26T07:51:10.213 回答