英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论