I am importing a CSV file, and running an application to decode some data from one of the columns, then I wish to export-csv all of that to a new file. I used this as a reference
PowerShell Output to CSV Not Placing Columns in Correct Order
$csvin = "c:\temp\hash\testinput.csv"
$csvout = "C:\temp\hash\TEST_output.csv"
$output = import-csv -path $csvin | foreach-object {
$hash = C:\temp\hash\decoder.exe -u $guid
$newhash = $hash -replace '.*(.{4})','$1'
$host = $_.host
$volumeuid = $_.volumeuid
$guid = $_.guid
[PSCustomObject]@{
Node_Name = $host
Volumeuid = $volumeuid
Hash = $newhash
Guid = $guid
}
}
$output | export-csv $csvout -notypeinformation -append
However, the FIRST line of the output file includes "system.object" and NOT the value for the $newhash and I don't know why. All the other rows are ok. It's just the first one. I'm sure it's something simple.
"Node_Name","Volumeuid","Hash","Suid"
"computer1","63980c4f","System.Object[]","1.2.3.456.789.11"
"computer2","24bcc525","0112","1.2.3.999.789.11"
ForEach-Objectwhen you do...decoder.exe -u $guid,$guidisn't defined yet so you're essentially doing...decoder.exe -u $nulland, I'm guessing when you pass$nullas argument your binary is returning more than one value. Fix of the issue is just put$guid = $_.guidas the first line of your loop body.$hostis a reserved “automatic variable” - you can’t assign values to it. See learn.microsoft.com/en-us/powershell/module/…