This is a very cut down version of my actual script, but it essentially highlights my problem.
I want to do a series of checks against some users, but after an initial run though, some of the criteria for a user may change, so I want to loop through the users again any number of times set in a parameter (for this example it is only 2).
My issue is that the -contains parameter doesn’t work and is always $false in the if statement.
All my users are getting added in twice, I can’t use -match or -like as they are not accurate enough, as it needs to be an exact match.
If I add ($Batch -contains $objUser) after ($Batch += $objUser), it shows $true, but on the next loop, the if statement is still $false when it comes up to a user with the same name in the batch.
$Batch -contains does not work because $Batch contain object and not string. PS tries to find object reference, but not object with same properties.
Olaf’s solution not good because of using $Batch.UserName - on each loop powershell will extract UserName value from all $Batch objects, collect it to another internal array and conpare it with $objUser.UserName. it’s sooo sloooooow…
the simpliest way to solve your problem: use hashtables
Maybe I was being lazy, the full script has multiple headings in the CSV, the values, some are integers, some boolean, some string.
I created the new object to ensure that all the headings were stored in the correct format, rather than all as string from an Import-CSV / Get-Content.