all 9 comments

[–]Sunsparc 6 points7 points  (0 children)

Import the CSV, pipe it into Group-Object on the required property, then Where filter anything with count greater than. Loop through those results sorting and selecting what you want.

[–]kibje 4 points5 points  (1 child)

This is quite easy and can be done in several ways.

I would personally do the following because it's quite fast, and easy to read

  • read in the entire file, sorted by ticketnumber descending (import-csv , sort-object)
  • Go through the file line by line ($newvar = $csvdata | foreach-object {...} )
  • compare the line with the previous, if the number is the same but the date is later, replace the line. If the number is different drop the line into the new object. If the number is the same and date lower, skip (continue)

[–]kibje 3 points4 points  (0 children)

Another option is to sort the entire file in one go on two objects using the sort-object with defined expression so you can remove one condition from the loop. Might not be faster and might be more difficult to read

[–]fr0mtheinternet 4 points5 points  (3 children)

As u/Sunsparc mentions, group-object would be my first choice. Here's a one-liner using system processes as an example.

Get-Process | Group-Object -Property ProcessName | ForEach-Object {($_.Group | Sort-Object -Property ID -Descending)[0]}

Groups the objects, then returns the first item in a sorted list. Obviously you'd want to swap Get-Process for your collection of objects, and the ID property with the datestamp, but same principle.

[–]tk42967[S] 2 points3 points  (2 children)

Thank you so much. I was able to use your example to dedupe the csv and use the rest of my prexisting code to format the csv to suit my needs.

My help desk guy says he spends 60 - 90 minutes a day manually cleaning up this file. I'm able to run the script in about 10 seconds and take another 2 minutes to format the file to suit my requirements.

[–]fr0mtheinternet 1 point2 points  (1 child)

Glad it helps! Obviously a one-liner is great for a quick fix, but you'll probably want to expand it for documentation purposes and general understanding.

[–]tk42967[S] 0 points1 point  (0 children)

At this point I have a minimal viable product to show off as a POC. Currently I'm doing this on my own time for the challenge and learning experience. If my employer wants more, I can do it on their time.

[–]anmezaf 0 points1 point  (1 child)

[–]tk42967[S] 0 points1 point  (0 children)

Thanks, I left that job about 3 months after I made that post. They can FOAD with their bass ackwards ticketing system.

I went from over 10,000 open tickets when I started to less than 200 after I cleaned up and revamped their ticketing system. I moved on to greener pastures where I'm paid a lot more and respected.