【问题标题】:powershell Export-Csv overwrite and -appendpowershell Export-Csv 覆盖和 -append
【发布时间】:2014-10-13 09:03:42
【问题描述】:
#Create an empty System.Array object
$employees = @()


#Add newly created object to the array
#$employees += $employee
for ($i=1;$i -le 2;$i++)
{
    $employee = New-Object System.Object
    $employee | Add-Member -MemberType NoteProperty -Name "Creation Time" -Value $(Get-date)
    $employee | Add-Member -MemberType NoteProperty -Name "Employee ID" -Value $($i.ToString("D2"))
    $employees += $employee
    Start-Sleep -Seconds 1
}


#Finally, use Export-Csv to export the data to a csv file
$employees | Export-Csv -NoTypeInformation -Path "c:\temp\test\EmployeeList.csv"

这是代码写入数据覆盖旧数据,我想同样更改代码,添加“-Append”

$employees | Export-Csv  -NoTypeInformation -Path "c:\temp\test\EmployeeList.csv" -Append 

错误信息:

Export-Csv:找不到与参数名称“附加”匹配的参数。 在 D:\powershell\MU_TAG_ET.ps1:292 char:70 + $员工 | Export-Csv -Path "c:\temp\test\EmployeeList.csv" -Append + CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand

你能帮帮我吗??

【问题讨论】:

  • -append 从 3.0 版本开始。你在 powershell 2.0 中吗?

标签: powershell csv


【解决方案1】:

如果您没有提到的 Powershell V3 或更高版本,您可以测试以查看您的 CSV 是否存在,如果是这种情况,请使用 Import-Csv 到 $employees 数组,添加新的 $employee PSCustomObjects,并在完成后导出.

【讨论】:

    【解决方案2】:

    在 PS 5.1 中“- Append”可用。

    $员工 |导出-Csv -NoTypeInformation -Append -Path "c:\temp\test\EmployeeList.csv"

    (P.s. 请管理员更正原始问题中的拼写吗?)

    顺便说一句,我看到一个“-强制”选项。我不确定为什么需要这样做,但您可能需要强制执行附加。试试看,让我们知道.... -Patrick Burwell,www.burwell.tech

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2019-11-01
      • 2018-12-12
      • 2018-02-08
      • 2020-12-03
      • 2016-04-30
      相关资源
      最近更新 更多