在下面的简化 PS 代码中,我不明白为什么在调用函数$MyPeople
后数组会发生变化。changeData
这个数组变量应该只是一个副本,我希望该函数将另一个数组变量返回到$UpdatedPeople
而不是 touch $MyPeople
:
function changeData {
Param ([PSCustomObject[]]$people)
$changed_people = $people
$changed_people[0].Name = "NEW NAME"
return $changed_people
}
# Original data:
$Person1 = [PSCustomObject]@{
Name = "First Person"
ID = 1
}
$Person2 = [PSCustomObject]@{
Name = "Second Person"
ID = 2
}
$MyPeople = $Person1,$Person2
"`$MyPeople[0] =`t`t" + $MyPeople[0]
"`n# Updating data..."
$UpdatedPeople = changeData($MyPeople)
"`$UpdatedPeople[0] =`t" + $UpdatedPeople[0]
"`$MyPeople[0] =`t`t" + $MyPeople[0]
控制台输出:
$MyPeople[0] = @{Name=First Person; ID=1}
# Updating data...
$UpdatedPeople[0] = @{Name=NEW NAME; ID=1}
$MyPeople[0] = @{Name=NEW NAME; ID=1}
谢谢!