Finding out if a user has been merged

Our Active Directory environment contains a list of email aliases for a user that I can query against, so I’d like to come up with a way of going through that database and merge users in RT to the primary account name. I’ve already installed RT-Extension-MergeUsers and can do it by hand, but doing this in an automated fashion would be better given the thousands of users we have and skip over accounts that have already been merged.

Here’s the snippet I have and comments I threw in along the way. My problem appears to be that $merged_users has information about the system user rather than about $account. What am I missing?

my $UnPrivilegedUsers = RT::Users->new ( $RT::SystemUser );

We only want to search through unprivileged users.

$UnPrivilegedUsers->LimitToUnprivileged;

Start going through the list of users

while ( my $UnPrivilegedUser = $UnPrivilegedUsers->Next ) {

Pull out their e-mail address

my $email_address = $UnPrivilegedUser->EmailAddress;
my $name = $UnPrivilegedUser->Name;

AD does case-sensitive matching, and they all appear to be lowercase

my $account = checkLDAP(lc($email_address));

Account could be found in AD

if ( $account ) {
# Get information about this user
my $merge_user = RT::User->new ( $RT::SystemUser );
$merge_user->Load ( $account );
# Find out what is merged into this account
my $merged_users = $merge_user->GetMergedUsers;
if (grep { $_ == $email_address } @{$merged_users->Content} ) {
print “Accounts already merged: $email_address in $account\n”;
}
}
}

Our Active Directory environment contains a list of email aliases for
a user that I can query against, so I’d like to come up with a way of
going through that database and merge users in RT to the primary
account name. I’ve already installed RT-Extension-MergeUsers and can
do it by hand, but doing this in an automated fashion would be better
given the thousands of users we have and skip over accounts that have
already been merged.

I’m not sure if you actually want to be checking GetMergedUsers or
looking at the EffectiveId attribute instead, but I see at least 2
problems in the code:

my $merged_users = $merge_user->GetMergedUsers;
if (grep { $_ == $email_address } @{$merged_users->Content} ) {
  print "Accounts already merged: $email_address in $account\n";
}

Content is a list of ids, not Email Addresses and you can’t use the
numeric compare (==) on a mixed number and string without really weird
results.

-kevin