Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 3克隆对象出现意外结果-影响其他对象_Powershell_Powershell 2.0_Powershell 3.0_Import Csv - Fatal编程技术网

Powershell 3克隆对象出现意外结果-影响其他对象

Powershell 3克隆对象出现意外结果-影响其他对象,powershell,powershell-2.0,powershell-3.0,import-csv,Powershell,Powershell 2.0,Powershell 3.0,Import Csv,我写的代码样本有问题。这是一个有点简单的问题,但这是一个花了一些时间的问题,我没有。我已经尝试了stackoverflow相关问题和搜索,但没有找到任何有用的东西 我有以下代码: #Importing some file from a csv to a variable $output = import-csv -LiteralPath ...some file imports OK ## #Copy the layout of the csv imported for further pro

我写的代码样本有问题。这是一个有点简单的问题,但这是一个花了一些时间的问题,我没有。我已经尝试了stackoverflow相关问题和搜索,但没有找到任何有用的东西

我有以下代码:

#Importing some file from a csv to a variable
$output = import-csv -LiteralPath ...some file imports OK

##
#Copy the layout of the csv imported for further processing..
##
$extraOut = $output.clone() 
$extraOut | ForEach-Object {
$_.innerlinks1 = ""
$_.innerlinksurl1 = ""
}
当我试图打印$output的值时,通过使用$\我得到了我以前分配的空字符串(我不想要)。为什么会发生这种情况

#Trying to print out $output should have data and not empty strings.
$output | ForEach-Object { Write-Host $_ }
任何允许复制导入csv结果结构(带或不带PSObject克隆)的帮助或代码都将非常有用

注意:在找到一个有用的答案后,我还认为这个问题需要更详细的脚本编写,因为我的文件中有很多空字符串,将来可能会导致其他问题。

每当我需要深度复制数组时,我都会在中使用答案。引用原来的答案:

# Get original data
$data = Import-Csv ...

# Serialize and Deserialize data using BinaryFormatter
$ms = New-Object System.IO.MemoryStream
$bf = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
$bf.Serialize($ms, $data)
$ms.Position = 0
$data2 = $bf.Deserialize($ms)
$ms.Close()

# Use deep copied data
$data2

数组的克隆方法执行浅层复制,但不克隆元素。寻找正确(深度)克隆的例子,有很多。我尝试过使用
.PSObject.copy()
,但得到了相同的结果。我已经知道了。@MathiasR.Jessen我确实在ForEach对象中尝试过,但没有按预期工作。是的,这是一个由一级对象组成的数组,如:
@{name=;name2=}{name=;name2=}等。
我相信这是与我已经找到的临时答案最接近的答案。在进一步搜索之后,我确实找到了与您的答案相关的两个答案,在这和在中