arrays - 使用 CSV 对象声明 Powershell 变量

标签 arrays variables powershell csv declare

所以我发现了一个奇怪的行为:
我有一个导入的 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/

相关文章:

php mysql 查询后过滤

c - 如何计算C中相同字符的个数?

python - 在 Python IDLE 中, 'print' 变量和只写变量有什么区别?

powershell - Powershell ConvertTo-json输出为小写

powershell - 从 powershell 模块重新签名、重新加载和运行测试功能?

java - [Java]数组和集合测试,意外的结果?

java - 检查多个整数是否大于一个整数

Javascript:将字符串转换为数字?

c# - 如何在 C# 中使用 PowerShell 的 Invoke-WebRequest

javascript - 获取数组中最长字符串的长度