最好的辦法是,如果沒有
BEGIN {}
PROCESS {}
END {}
結構高級功能都非常混亂,當談到從管道和陣列接受。
這裏發生的事情是,如果沒有PROCESS {}塊,函數中的$ T就是一個INT [],並作爲數組傳遞。你的foreach過程,但返回的命令的最後結果 - 10 + 10
更改您的代碼:你期待什麼
function a{
param([parameter(ValueFromPipeline=$true)][int[]]$t)
BEGIN {}
PROCESS{
$t | %{$_+10}
}
END {}
}
回報 - $ T的每個元素傳遞給在foreach再往下管道。
下面是一個MS PowerShell傢伙的鏈接:Windows PowerShell: The Advanced Function Lifecycle顯示了一些很好的例子。
閱讀來自@克里斯 - 馬格努森下面的鏈接,我想出了偉大的代碼,準確顯示發生的事情:
Function AA {
Begin {"Begin: The input is $input , Pipeline is $_"}
End {"End: The input is $input , Pipeline is $_" }
}
PS C:\Temp> 1,2,3 | AA
Begin: The input is , Pipeline is
End: The input is 1 2 3 , Pipeline is
Function BB {
Begin {"Begin: The input is $input , Pipeline is $_" }
Process {"Process: The input is $input , Pipeline is $_" }
End {"End: The input is $input , Pipeline is $_" }
}
PS C:\Temp> 1,2,3 | BB
Begin: The input is , Pipeline is
Process: The input is 1 , Pipeline is 1
Process: The input is 2 , Pipeline is 2
Process: The input is 3 , Pipeline is 3
End: The input is , Pipeline is 3
質樸的善良,我會說。
感謝您提供快速而全面的答案 –