Niag Ntawv
Niag Ntawv

Reputation: 337

best way to compare different objects in powershell

Not sure if this is even possible, but I'm trying to compare properties of an ADgroup object and a PSCustomObject object. We're in the middle of a user audit which requires validating a list of active employees against our active AD user accounts along with their AD group memberships. Here's a basic breakdown of what I have so far:

(we're defining two separate search paths because we have groups in different OUs)

List of usernames from HR system

List of usernames from AD

Empty array to store custom object/properties

Compare HR to AD

From this point on, I want to compare every group from each matching user to the group name from the $mainGroups to see if there's a match. If there isn't then compare it to the $subGroups group names. If there's a match do nothing, if there's a mismatch, output the username along with any mismatched group names. Just not sure how best to compare these objects. Any hints will be appreciated.

Upvotes: 0

Views: 959

Answers (1)

thepip3r
thepip3r

Reputation: 2935

If your groups are arrays, then use the -contains operator -- if they're not, make them arrays:

foreach ($u in $users) {
   foreach ($groupdn in $u.memberof) {
      if ($mainGroups -contains $ug -or $subGroups -contains $ug) {
         ## do something when the users' group exists in the checked sub-groups
      }
   }
}

...this assumes the $maingroups array is an array of group DNs...

Upvotes: 1

Related Questions