Can the HashTable ContainsValue method find a value more than once?

Hi,

The goal is given a set of user accounts with their mail attribute, determine if that mail value exists in the mail attribute of a different user (ie. SamaccountName) account.  Below, after the both hashtables are loaded, I use ContainsValue to search in the the hashtable of AllDomainUsers.  How does ContainsValue work if multiple accounts contain the same mail attribute value?  So my search is really for any account in AllDomainUsers which contains the mail attribute for the SamAccountName in $AllUsers.  I've successfully accomplished this using IFs and arrays, but the performance is slow when the arrays are large.

$AllUsers = Get-Aduser -Filter .... -Pr mail
           
$AllUsersHash = $null
$AllUsersHash = @{}
Foreach ($AllU in $AllUsers) {
 $AllUsersHash.add($AllU.SamAccountName,$AllU.Mail)
 }

$AllDomainUsers = Get-Aduser .... * -Pr mail

$hash = $null
$hash = @{}
Foreach ($AU in $AllDomainUsers) {
 $hash.add($AU.SamAccountName,$AU.Mail)
 }

Foreach ($item in $AllUsersHash.GetEnumerator()) {
   $hash.ContainsValue("$item.Value")
   }
   &

August 27th, 2015 12:18am

"ContainsValue" only returns true or false.   What is it you are asking.  A hash is not how we would do this.

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 12:38am

Try this:

$results=$hash | group value

$results|?{$_.Count -gt 1}

August 27th, 2015 12:40am

Even easier:

Get-Aduser -Filter .... -Pr mail | group mail |?{$_.Count -gt 1}

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 12:41am

Thanks jrv....I think this works just great after my first test.

The goal is to simply identify User accounts which have the same mail attribute, so by issuing the Get-Aduser -Filter .... -Pr mail | group mail |?{$_.Count -gt 1} what I got was 3 columns of output with the Count col. being the count of the mail attribute occurrence, the Name col. which is the mail attribute, then the Group col. which gives me the DNs of the occurence of the mail attribute.  I just have to figure out how to expand the Group col. cause in some cases there a 5 occurences.

Any suggestions on how I would determine quickly if the mail attribute of an User account exists in any UPN attribute of any other User account?

August 27th, 2015 1:27am

Actually, after further testing and based on my original post, what I need to determine for a group of users (ie. $AllUsers ) is if their mail attribute exists on any other user account's mail attribute from another group (ie. $AllDomainUsers) of users.  I can do this by looping through the arrays and doing compares with IF statements, but that takes very long for a sizable array.  Thats why Im trying Hash Tables and the ContainsValue method to see if I can speed this 'search and identification' process up.
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 3:08am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics