1

I am fairly new to powershell, but when I try to run this portion of the code, it works sometimes, but I usually get an error message:

"Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'."

It seems to fail when I add $in + $o, and it has worked in the past. I haven't changed anything for it to stop working though. I am trying to append to a csv, and I have tried Export-CSV -append, however that does not work for me. Any ideas on what might be wrong with this code?

    $in = Import-Csv $csv

    $obj = New-Object PSObject
    $obj | Add-Member NoteProperty Column1 "$c1"
    $obj | Add-Member NoteProperty Column2 "$c2"
    $obj | Add-Member NoteProperty Column3 "$c3"

    $in + $obj | Export-Csv -Encoding ASCII -NoTypeInformation $csv
4

1 に答える 1

3

$in が配列の場合、おそらく以前は機能していました。ただし、要素が 1 つだけの CSV を読み込むと失敗します。代わりに PowerShell 3.0 でこれを行うことができます ($in を読み込む必要さえありません)。

$obj | Export-Csv $csv -Encoding ASCII -NoTypeInformation -Append

-AppendExport-Csvのパラメーターは、PowerShell 3.0 で新しく追加されました。3.0 がない場合は、次の行を変更します。

$in = @(Import-Csv $csv)

その後、動作するはずです。それ$inは常に配列であることを確認します。

于 2013-02-06T18:55:05.890 に答える