向 CSV 文件添加条目会引发错误:不包含名为 ‘op_Addition’ 的方法。

huangapple go评论129阅读模式
英文:

Adding a entry to a csv file throws error:does not contain a method named 'op_Addition'

问题

有一个.csv文件如下:

"LastWriteTime","BackupFile","MountedDrive"
"07:07 Fri Mar 03 23","c:\temp\xxx01-01.mrimg","A"
"08:07 Fri Mar 03 23","c:\temp\xxx01-02.mrimg","B"
"09:07 Fri Mar 03 23","c:\temp\xxx01-03.mrimg","C"

这是一个日志文件,我有一个函数,每次执行操作时都会向其中添加一个条目,以下是函数的一部分代码:

$Log = Import-Csv -Path "C:\Temp\Logging.csv"

$NewLogEntry = [PSCustomObject]@{
  LastWriteTime = $Path.LastWriteTime.ToString('hh:mm ddd MMM dd yy')
  BackupFile    = $path
  MountedDrive  = $Drive.ToUpper()
}

$Log += $NewLogEntry
$Log | ConvertTo-Csv | Set-Content -Path "C:\Temp\Logging.csv" -Force

这一行$Log += $NewLogEntry似乎会随机引发错误,有时会工作,但大多数时候会引发错误:

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At line:24 char:12
+     $Var = $Log += $NewLogEntry
+            ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

查找后,我发现有答案表明这与 $log 没有被覆写有关,所以我尝试了:

$Log2 = Log+= $NewLogEntry
$Log2 | ConvertTo-Csv | Set-Content -Path "C:\Temp\Logging.csv" -Force

仍然失败。完全陷入困境。感谢任何帮助。

英文:

I have a .csv file like this:

"LastWriteTime","BackupFile","MountedDrive"
"07:07 Fri Mar 03 23","c:\temp\xxx01-01.mrimg","A"
"08:07 Fri Mar 03 23","c:\temp\xxx01-02.mrimg","B"
"09:07 Fri Mar 03 23","c:\temp\xxx01-03.mrimg","C"

Its a logging file, I have a function that adds a entry to it every time it does something, here is a snippet from the function:

$Log = Import-Csv -Path "C:\Temp\Logging.csv"

$NewLogEntry = [PSCustomObject]@{
  LastWriteTime = $Path.LastWriteTime.ToString('hh:mm ddd MMM dd yy')
  BackupFile    = $path
  MountedDrive  = $Drive.ToUpper()
}

$Log += $NewLogEntry
$Log | ConvertTo-Csv | Set-Content -Path "C:\Temp\Logging.csv" -Force

This line $Log += $NewLogEntry seems to throw an error randomly, it will sometimes work and but mostly throw an error:

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At line:24 char:12
+     $Var = $Log += $NewLogEntry
+            ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

Looking it up, I found answers suggesting its to do with $log not being overwritten, so I tried:

$Log2 = Log+= $NewLogEntry
$Log2 | ConvertTo-Csv | Set-Content -Path "C:\Temp\Logging.csv" -Force

Still fails.

Completely stuck. Thanks for any help.

答案1

得分: 2

发生错误的情况是,当您的 CSV 仅有一行时,Import-Csv 的输出在分配给变量时被枚举,如果 CSV

英文:

Your error happens when your CSV only has one row, the output from Import-Csv gets enumerated when assigned to a variable, if the CSV happens to have only one row, then the variable would be of the type PSObject instead of object[] and += would fail in that case because PSObject doesn't have an op_Addition imlementation defined.

Easy way to reproduce the error:

([pscustomobject]@{ foo = 123 }) + ([pscustomobject]@{ foo = 123 })

To overcome this you could use the Array subexpression operator @( ) so $Log would always be an object[]:

$Log = @(Import-Csv -Path "C:\Temp\Logging.csv")

However, a much easier approach would be to simply use -Append, that way there is no need to import the CSV:

# No `Import-Csv` needed

[PSCustomObject]@{
  LastWriteTime = $Path.LastWriteTime.ToString('hh:mm ddd MMM dd yy')
  BackupFile    = $path
  MountedDrive  = $Drive.ToUpper()
} | Export-Csv "C:\Temp\Logging.csv" -NoTypeInformation -Append

huangapple
  • 本文由 发表于 2023年3月7日 04:25:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定