在PowerShell中,函数如何通过引用传递参数?答案还是比较简单的,只要在函数定义和调用时,在参数变量前加入[REF]标记即可,如:
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function Find-NewMessages($valvar1, [REF]$refvar1, [REF]$refvar2) {
//some stuff
$refvar1.Value = “hi” #注意,此处一定要是$refvar1.Value,而不能直接用$refvar1
$refvar2.Value = “bye”
}
然后在调用时
$refvar1 = “1”
$refvar2 “2”
Find-NewMessages $valvar1 ([REF]$refvar1) ([REF]$refvar2)
//now $refvar1 = “hi” and $refvar2 = “bye”
英文原文出处:Pass by Reference Parameters in PowerShell