2012-04-26 75 views
4

我想輸出傳遞給我的腳本以包含在電子郵件中的任何參數的值。輸出PSBound參數

我已經試過這樣:

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

但沒有奏效。有人能幫我一把嗎?

+0

感謝大衛,吉文。這是我需要的get-enumerator的傳遞。此外,本頁: http://halr9000.com/article/912 給了我我真的很想做的形式。 – 2012-04-26 13:33:17

回答

0
function test 
{ 
    param($a, $b) 

    $psboundparameters.Values 
    $psboundparameters.Keys 
} 


test "Hello" "World" 
5

$ 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