How do I fix my PowerShell script to export CSV rows correctly?

0
0
Asked By CuriousCoder99 On

I'm working on a PowerShell script to import a CSV file containing ticket data, modify it by appending new columns, and then export it into a new CSV file. However, I'm facing an issue where all of the output data is appearing in a single row instead of multiple rows, which is what I need. Here's a snippet of my code:

```powershell
$path = 'c:tempcurrentweekclosed.csv'
$data = Import-CSV $path
$outputfile = 'C:temptrackitdatatest.csv'

$trackitdata = foreach($t in $data){
$ticketdata = [PSCustomObject]@{
'$ticketno' = ($data.'Ticket ID' | Out-String).Trim()
'$summary' = ($data.'Ticket Summary' | Out-String).Trim()
'$category' = ($data.category | Out-String).Trim()
'$closer' = ($data.'Assigned To Full Name' | Out-String).Trim()
'$org' = ($data.type | Out-String).Trim()
'$Group' = $somegroup1
'Type' = $somegroup2
'SubType' = $somegroup3
} | Select-Object '$ticketno','$summary','$category','$closer','$org','$Group','$Type','$SubType'
}
$ticketdata | Export-CSV -Path $outputfile
```

In its current state, the output is all consolidated into a single row, which isn't what I intended. If I try to export `$trackitdata`, I end up with a blank CSV file. I could really use some help debugging this issue and getting it to output correctly!

5 Answers

Answered By CleanCoder On

It seems like you're unnecessarily using `Select-Object` on your PSCustomObject creation. You can simplify this by just returning the object directly without needing to pipe it to `Select-Object`. Remember to replace your variable assignments to use `$t` instead of `$data` inside the loop for each property.

CuriousCoder99 -

I eliminated the `Select-Object` after adjusting my `$ticketdata`, and now I'm getting the desired CSV output. I'm working on my formatting and trying to balance readability with maintainability.

Answered By CodeMasterGeek On

Make sure that instead of using `$data`, you should be referencing `$t` while creating the PSCustomObject for each ticket iteration. So change lines like `'$ticketno' = ($data.'Ticket ID' | Out-String).Trim()` to use `$t` instead. That change should help you get multiple rows in your output. Keep an eye out for how you're structuring your objects.

CuriousCoder99 -

I adjusted that and I'm now getting some output in the CSV. However, it's still only one row. EDIT: Spoke too soon, when I try to `Export-CSV $trackitdata`, I'm still getting a blank CSV.

Answered By ScriptSavvy On

Also, consider using the `-Append` switch while exporting if you're trying to add rows to an existing CSV file. Otherwise, for a new file, you might not need it. Just make sure you're targeting the correct variable for the export.

CuriousCoder99 -

Thanks for the tip! Although I wasn't really looking to append, I'll keep that in mind for future scripts.

Answered By HelpfulHarry42 On

It looks like there are a couple of issues with your script. First, you're trying to assign your `foreach` loop to a variable, but nothing is being emitted during the loop, which means your `$trackitdata` is never actually holding any data. Try removing the `$ticketdata` assignment within the loop. You should export `$trackitdata` instead of `$ticketdata`, which will allow all of your data to be exported properly after your looping updates.

CuriousCoder99 -

THANK YOU SO MUCH!! I knew it was something simple tripping me up. Removing the assignment of `$ticketdata = [PSCustomObject]` worked like a charm. I assumed that assigning it a variable would let me use that further down the script if I wanted but otherwise keep the output normal. I'm an idiot.

Answered By PowerShellPro On

Just a question: what platform are you using to write this PowerShell script? ISE? VS Code? Knowing this can help us assist you better. If you're not using debugging tools properly, learning how to debug can definitely be a game changer, especially for troubleshooting issues like blank outputs.

CuriousCoder99 -

I've been using ISE but switched to VSCode recently since ISE is going EOL. I don't have much experience with debugging, just googling errors and fixing them. But in this case, I'm not getting any errors, just a blank output.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.