【发布时间】:2021-06-02 16:11:22
【问题描述】:
我有一个调用本地函数的数据循环。
在这个函数中,我提供了一个全局范围的数组。
但是,最后,数组是空的。为什么 ?以及如何解决?
要重现该行为,请尝试以下操作:
$myArray = @() # Global array
function Invoke-SomeAction{
param(
[Parameter()]
[int]$Val
)
$myArray += $Val
}
1..10 | % {
Invoke-SomeAction -Val $_
}
$myArray.Count # Expect 10, got 0
如果我像这样删除函数调用:
$myArray = @() # Global array
1..10 | % {
$myArray += $_
}
$myArray.Count # Expect 10, got 10
它的行为符合预期。
PS:如果重要的话,这是我的$PSVersionTable:
Name Value
---- -----
PSVersion 5.1.14393.3053
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.3053
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
【问题讨论】:
标签: powershell