Optimization help: Take two Object Arrays, return values from first that do not appear in second. Current setup takes 30 minutes.

Short Write Up:

Comparing $oldUsers and $currentUsers in order to get only the objects from $oldUsers that are not in $currentUsers. Each object array has over 2000 objects in it and it is taking around 30 minutes just for the comparison and output piece of my script.

foreach($currentObj in $currentUsers){ $oldUsers = $oldUsers | Where-Object { $_.uniqueID -ne $currentObj.uniqueID } } 

Long Write Up:

I have a script I’m using to confirm that access was actually disabled for specific users. Before anything else, I need to get a list of the users who have left. I have a previous log of users and a current log of users, and I want create a list of users that were on the previous log but not on the current log.

I cannot just combine both logs and take the unique entries, as that would include both users who left between the log times, but also new users who have joined between the log times. I need to only get the users who have left between the logs.

I’ve already read in both the prior users log and the current users log, and converted the data I need from each into two object arrays, $oldUsers and $currentUsers. Each of these have over 2000 objects.

Each object has a unique property, uniqueID, which I am using to identify “uniqueness”.

The section of my code that compares and removes uniqueID’s that exist in the current list is taking around 30 minutes to run, and I’m sure I’m missing a lot of potential optimization. I’ve used Write-Host (Get-Date -DisplayHint Time) through my code and stepped through it with breakpoints, the piece of code I’ve included above is the culprit.

I found this article about using hash tables, but I feel like implementing a hash table for this would be overkill. I’m open to being told otherwise though.


This is making it very annoying to test the later sections of my script that actually do things with this list of users that have left, and I swear it was only taking 3 minutes for this piece earlier, because I was on a roll working with the later parts of the script and testing them. Haven’t changed my input either.

submitted by /u/it_intern_throw
[link] [comments]

Leave a Reply