2014-12-31 34 views
2

所以,我发现了一个奇怪的现象:
我有这些头衔导入CSV文件:PowerShell的变量,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?是关于数组或多维数组,这个问题是关于对象传输(复制)的。这个页面上的简单答案在这种情况下不起作用。问题仍然存在。

+0

不完全重复IMO。部分问题“如何处理这个特定的对象”在那里没有回答。 – PetSerAl

+0

它是重复的,因为它是关于复制的参考。重复有一个解决方案。虽然这个问题也包含一个替代方案。 – JasonMArcher

回答

2

是的,这是正常的行为,它是如何参考类型的工作。您需要复制对象:

$x = $connections[0].PSObject.Copy() 
+0

工作好,还有一个问题你。我应该如何实现这一点,如果我想在$ connections对象上使用Where-Object? 例如: $ x = $ connections | Where-Object {$ _。Application -eq“Test”} – Tumpi

+0

您的意思是说,您要更改Where-Object cmdlet中的对象,还是只想在Where-Object中过滤它们并将其更改为后者? – PetSerAl

+0

过滤,比稍后更改。 – Tumpi