所以我发现了一个奇怪的行为:
我有一个带有以下标题的导入的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数组分配分配变量,而不是值?与数组或多维数组有关,此问题与对象传输(复制)有关。在这种情况下,该页面上的简单答案不起作用。问题仍然发生。
是的,这是正常行为,这是引用类型的工作方式。您需要复制对象:
$x = $connections[0].PSObject.Copy()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句