0

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"

2
  • 2
    In the first iteration of the ForEach-Object when you do ...decoder.exe -u $guid, $guid isn't defined yet so you're essentially doing ...decoder.exe -u $null and, I'm guessing when you pass $null as argument your binary is returning more than one value. Fix of the issue is just put $guid = $_.guid as the first line of your loop body. Commented Apr 30, 2024 at 17:59
  • 1
    Note that $host is a reserved “automatic variable” - you can’t assign values to it. See learn.microsoft.com/en-us/powershell/module/… Commented May 1, 2024 at 6:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.