【发布时间】:2017-11-26 18:22:57
【问题描述】:
我正在编写的代码示例有问题。这是一个简单的问题,但这是一个需要一些时间的问题,而我没有。我已经尝试过 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 $_ }
任何允许复制 Import-csv 结果结构的帮助或代码(有或没有 PSObject 克隆)也会很棒。
注意:在找到有用的答案后,我还认为问题需要更详细的脚本,因为我的文件中有很多空字符串,将来可能会导致其他问题。
【问题讨论】:
-
Array 的 clone 方法执行 shallow 复制,它不克隆元素。寻找正确(深度)克隆的例子,有很多。
-
我确实尝试过使用
.PSObject.copy(),但得到了相同的结果。我已经开始了。 -
@MathiasR.Jessen 我确实在 ForEach-Object 中尝试过,但没有按预期工作。是的,它是一层对象的数组,如下所示:
@{name=;name2=} @{name=;name2=} etc..
标签: powershell powershell-2.0 powershell-3.0 import-csv