Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading