This perl script will compare your active directory and your imail directory. Any accounts that do not exist in AD will be moved to your orphan directory and their registry settings will be exported and deleted.
use Win32::OLE;
use Array::Diff;
my $ImailUsers = “G:\\ImailUsersDirectory\\users\\”;
my $imailorphan = “G:\\DirectorytoPutOrphans\\”;
my $imailReg = “HKLM\\SOFTWARE\\Ipswitch\\Imail\\Domains\\ImailRegistryKey\\Users\\”;
@arrAD = ();
@arrFolder = ();
$baseusers = “dc=yourdomain,dc=local”;
#Get Users from AD
my $connObj = Win32::OLE->new(‘ADODB.Connection’);
$connObj->{Provider} = “ADsDSOObject”;
$connObj->Open;
my $commObj = Win32::OLE->new(‘ADODB.Command’);
$commObj->{ActiveConnection} = $connObj;
$commObj->Properties->{‘Page Size’} = 1000;
#Generate Ldap Search String
my $query = “;”;
$query .= “(&(objectClass=user)(objectCategory=person));”;
$query .= “sAMAccountName;”;
$query .= “subtree”;
$commObj->{CommandText} = $query;
opendir MYDIR, $ImailUsers;
@contents = readdir MYDIR;
closedir MYDIR;
foreach $folder (@contents)
{
if (($folder ne ‘..’) && ($folder ne ‘.’) && (-d $ImailUsers . $folder))
{
push (@arrFolder, lc($folder));
}
}#end for
my $resObj = $commObj->Execute($query); die “Could not query $base: “,$Win32::OLE::LastError,”\n” unless ref $resObj;
while (!($resObj->EOF))
{
$strUserDN = $resObj->Fields(“sAMAccountName”)->value;
push (@arrAD, lc($strUserDN));
$resObj->MoveNext;
}#end if
@arrAD = sort(@arrAD);
@arrFolder = sort(@arrFolder);
my $diff = Array::Diff->diff(\@arrAD,\@arrFolder);
if ($diff->count !=0)
{
foreach (@{$diff->added})
{
system(“move ” . $ImailUsers . $_ . ” ” . $imailorphan);
system(“reg export ” . $imailReg . $_ . ” ” . $imailorphan . $_ . “\\” . $_ . “.reg”);
system(“reg delete ” . $imailReg . $_ . ” /f”);
}
}