所以我发现了一个奇怪的行为:
我有一个导入的 CSV 文件,其标题如下:
$connections | Get-Member
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Access Method NoteProperty System.String Access Method=SSH
Accessable NoteProperty System.String Accessable=N/A
Application NoteProperty System.String Application=Test
Authentication type NoteProperty System.String Authentication type=rsa
Node NoteProperty System.String Node=ComputerName
Port NoteProperty System.String Port=22
User NoteProperty System.String User=User
现在,如果我从一行创建一个新变量
$x = $connections[0]
然后更改 $x 中的内容
$x.Accessable = $true
更改将在 $connections[0] 中可见。
$connections[0].Accessable
True
这是正常行为吗?如果是这样,我如何使用与该行类似的对象创建一个新变量?
编辑: Powershell array assignment assigns variable, not value?是关于数组,或多维数组的,这道题是关于对象传递(复制)的。该页面上的简单答案在这种情况下不起作用。问题仍然发生。
最佳答案
是的,这是正常行为,这就是引用类型的工作方式。您需要复制对象:
$x = $connections[0].PSObject.Copy()
关于arrays - 使用 CSV 对象声明 Powershell 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27726698/