Delete Contacts NOT found in CSV

We have been working to synchronize our GAL through non-technical means with another remote company. We don't actively have a connection to this other environment so sneaker-net is the solution.

I currently get a CSV from this company with the following columns.

  • Mode (A for Add, D for Delete, C for Change)
  • FirstName
  • LastName
  • DisplayName
  • EmailAddress

I then take this CSV file and run it through this script to remove entries that are not found in this CSV. It only takes in account (currently) entries that are marked with a mode of 'A'.

To speed up my processing, it uses the Quest Active Roles Cmdlets to do an LDAP filter on the email addresses, but uses the Exchange 2007 Powershell cmdlets to actually get the Contact objects.

$SearchDomain= "@example.com"
$SearchOU = ""

$csv = import-csv -path "c:\ExampleDotComEmployees.CSV" | sort displayname | ?{$_.mode -match "a"}

if ($objContacts -eq $null) {
        $ldapFilter = "(mail=*"+$searchDomain+")"
        $objContacts= Get-QADObject -SizeLimit 0 -ldapFilter $ldapFilter| %{get-mailcontact -Identity $_.dn -erroraction silentlycontinue}
}

$DeleteThese = @()
foreach ($c in $objContacts ) {
        $email = $c.primarysmtpaddress.tostring()
        $found = $csv | ?{$_.emailaddress -match $email}
        if ($found -eq $null) {
                $DeleteThese += $c             
        }      
}
write-host "Found "$objContacts.count "contacts. " $deleteThese.count "contacts NOT in CSV"
$deleteThese | Remove-MailContact

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal6>, <html>, <java>, <javascript>, <php>, <posh>.

More information about formatting options