Powershell 追加 CSV 错误
Powershell append CSV Error
我有一个只有一列的 csv 文件,如下所示
Column1
Apple
Mango
我正在尝试通过使用以下脚本附加当前 csv 文件来附加 csv 以添加更多水果
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$csvPathforFruits = $path + "\Fruits.csv"
$importFruit = Import-Csv $csvPathforFruits
$new = "Orange"
$new | Export-CSV $csvPathforFruits -Append
但是抛出了下面的错误。
Export-CSV : Cannot append CSV content to the following file: C:\Users\sysadmin\Desktop\Test\Fruits.csv. The appended object does not have a property that corresponds to the following column:
Colume1. To continue with mismatched properties, add the -Force parameter, and then retry the command.
At C:\Users\sysadmin\Desktop\Test\tt.ps1:5 char:9
+ $new | Export-CSV $csvPathforFruits -Append
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Colume1:String) [Export-Csv], InvalidOperationException
+ FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvCommand
谁能指出我的错误。
单列 CSV 基本上是一个文本文件,每行都有一个条目。这将满足您的需求。
无需导入 CSV。我已将编码设置为 utf8
;如果你 运行 遇到问题尝试 unicode
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$csvPathforFruits = $path + "\Fruits.csv"
# In English: add a new line and then the word "Orange" to the bottom of the file.
"`nOrange" | Out-File $csvPathforFruits -Append -Encoding utf8
当您导入 CSV 时,它被转换成一个对象,您需要附加一个类似的对象,但您正试图附加一个字符串。这样的事情可以解决它:
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$csvPathforFruits = $path + "\Fruits.csv"
$importFruit = Import-Csv $csvPathforFruits
$new = New-Object psobject -Property @{Column1 = "Orange"}
$new | Export-CSV $csvPathforFruits -Append
甚至:
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$csvPathforFruits = $path + "\Fruits.csv"
$importFruit = Import-Csv $csvPathforFruits
$importFruit += New-Object psobject -Property @{Column1 = "Orange"}
$importFruit | Export-CSV $csvPathforFruits -NoTypeInformation
但是,如果您的文件只是一列文本,那么将其视为 CSV 似乎有点过分了。
我有一个只有一列的 csv 文件,如下所示
Column1
Apple
Mango
我正在尝试通过使用以下脚本附加当前 csv 文件来附加 csv 以添加更多水果
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$csvPathforFruits = $path + "\Fruits.csv"
$importFruit = Import-Csv $csvPathforFruits
$new = "Orange"
$new | Export-CSV $csvPathforFruits -Append
但是抛出了下面的错误。
Export-CSV : Cannot append CSV content to the following file: C:\Users\sysadmin\Desktop\Test\Fruits.csv. The appended object does not have a property that corresponds to the following column:
Colume1. To continue with mismatched properties, add the -Force parameter, and then retry the command.
At C:\Users\sysadmin\Desktop\Test\tt.ps1:5 char:9
+ $new | Export-CSV $csvPathforFruits -Append
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Colume1:String) [Export-Csv], InvalidOperationException
+ FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvCommand
谁能指出我的错误。
单列 CSV 基本上是一个文本文件,每行都有一个条目。这将满足您的需求。
无需导入 CSV。我已将编码设置为 utf8
;如果你 运行 遇到问题尝试 unicode
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$csvPathforFruits = $path + "\Fruits.csv"
# In English: add a new line and then the word "Orange" to the bottom of the file.
"`nOrange" | Out-File $csvPathforFruits -Append -Encoding utf8
当您导入 CSV 时,它被转换成一个对象,您需要附加一个类似的对象,但您正试图附加一个字符串。这样的事情可以解决它:
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$csvPathforFruits = $path + "\Fruits.csv"
$importFruit = Import-Csv $csvPathforFruits
$new = New-Object psobject -Property @{Column1 = "Orange"}
$new | Export-CSV $csvPathforFruits -Append
甚至:
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$csvPathforFruits = $path + "\Fruits.csv"
$importFruit = Import-Csv $csvPathforFruits
$importFruit += New-Object psobject -Property @{Column1 = "Orange"}
$importFruit | Export-CSV $csvPathforFruits -NoTypeInformation
但是,如果您的文件只是一列文本,那么将其视为 CSV 似乎有点过分了。