Skip to content

Instantly share code, notes, and snippets.

@jacqinthebox
Last active September 12, 2017 09:38
Show Gist options
  • Save jacqinthebox/904919151f3d0df73e28 to your computer and use it in GitHub Desktop.
Save jacqinthebox/904919151f3d0df73e28 to your computer and use it in GitHub Desktop.
Download all proxy addresses from AD #exchange
public void getAllProxyAddresses()
{
var db = new UMTDataContext();
PrincipalContext AD = new PrincipalContext(ContextType.Domain, this.MailDomain);
UserPrincipal u = new UserPrincipal(AD);
PrincipalSearcher search = new PrincipalSearcher(u);
List<string> emailAddresses = new List<string>();
Console.WriteLine("Huidige collectie aan het verwijderen.");
db.ExecuteCommand("truncate table ProxyAddress");
db.SubmitChanges();
Console.WriteLine("Alle proxy addressen aan het verzamelen.. dit duurt wel even.");
foreach (UserPrincipal result in search.FindAll())
{
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "headquarters");
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, result.SamAccountName);
// Add the "mail" entry
emailAddresses.Add(user.EmailAddress);
// Add the "proxyaddresses" entries.
System.DirectoryServices.PropertyCollection properties = ((DirectoryEntry)user.GetUnderlyingObject()).Properties;
foreach (object property in properties["proxyaddresses"])
{
emailAddresses.Add(property.ToString());
}
}
Console.WriteLine("Alle proxy addressen zijn verzameld. Nu in de database inserten.");
search.Dispose();
foreach (var e in emailAddresses)
{
var mail = new ProxyAddress();
mail.Mail = e;
db.ProxyAddresses.InsertOnSubmit(mail);
}
db.SubmitChanges();
Console.WriteLine("{0} proxyadressen toegevoegd", db.ProxyAddresses.Count());
Console.WriteLine("Klaar met deze bevalling.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment