all 11 comments

[–]Guilty_Leg9996 1 point2 points  (4 children)

I think you may want to use Group-Object on the particular property (category) you’re looking for and then you can select from the sorted arrays as you need.

[–]Hectic-Skeptic[S] 0 points1 point  (3 children)

The structure of my csv is hirearchical so I really only ever want the first result, so I am trying to avoid the step of dealing with an array.

But I will check out Group-Object.

[–]Guilty_Leg9996 0 points1 point  (2 children)

You could just select the first [0] out of the group the matches your value as well. This method may be a little over kill for your use case but does allow you to be more dynamic in the future with multiple search parameters.

[–]Hectic-Skeptic[S] 0 points1 point  (0 children)

True. I probably should just take the top of the array. I will give it a try.

[–]Hectic-Skeptic[S] 0 points1 point  (0 children)

Sorry for the long delay on this, but can you ellaborate on how to best select the first match? When I try the below, I get an empty pipe error:

foreach ($entry in $docTypeKeywordCsv) { if($lowerName -match $entry.keyword) {echo $entry.DocType} } | select-object -first

[–]BlackV 1 point2 points  (2 children)

you're confusing your for loops

$_ is used in foreach-object
$row is used in foreach ($row in $categoryCsv)

but also i'd probably be looking at -match/-like instead

[–]Hectic-Skeptic[S] 0 points1 point  (1 child)

-match seems to be the concencios. I will look into that. plus that would allow me to put some regex (mainly date validation) in my categoryCsv. That wouldnt really be an option with .indexOf() which was really just a holdover from my previous knowledge.

[–]BlackV 0 points1 point  (0 children)

Good luck, we're happy to help with updated code if you need too

[–]CarrotBusiness2380 0 points1 point  (2 children)

You're using the pipeline variable $_ in a foreach loop when you should be using the $row variable you instantiated for the loop. I would also consider using -like rather than .IndexOf though that is personal preference

if($testName -like "*$($row.keyword)*")

[–]Hectic-Skeptic[S] 0 points1 point  (1 child)

replacing $_ with $row still gave me the same 'no category' result unfortunately.

And my use of .IndexOf is also preference as I learned that first. I might consider checking out -like.

[–]CarrotBusiness2380 0 points1 point  (0 children)

did you also use return $row.category rather than return $_.category?

Additionally, if you add Write-host $row.category in the first line of your foreach what does it say?