Utilizing SearchResultCollection to manipulate property values in AD

Nov 17 2005 1:55 PM
Hi,

I am a novice C# programmer and had a question. Is it possible to use the SearchResultCollection to modify AD property values?

Here is my code so far:

using System;

using System.DirectoryServices;

using System.IO;

using System.Text;

using System.Xml;

using Utility;



System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);

mySearcher.Filter = ("(&(objectClass=user)(samaccountname=*)(mail=*)(homeMDB=*)(homemta=*)(msexchhomeservername=*)(legacyexchangedn=*)(mdbusedefaults=*))");

mySearcher.SizeLimit = 0;

mySearcher.PageSize = 999;

mySearcher.SearchScope = SearchScope.Subtree;

mySearcher.PropertiesToLoad.Add("samaccountname");

mySearcher.PropertiesToLoad.Add("mail");

mySearcher.PropertiesToLoad.Add("mailnickname");

mySearcher.PropertiesToLoad.Add("homemdb");

mySearcher.PropertiesToLoad.Add("homemta");

mySearcher.PropertiesToLoad.Add("msexchhomeservername");

mySearcher.PropertiesToLoad.Add("mdbusedefaults");

mySearcher.PropertiesToLoad.Add("mdbstoragequota");

mySearcher.PropertiesToLoad.Add("mdboverquotalimit");

mySearcher.PropertiesToLoad.Add("mdboverhardquotalimit");

mySearcher.PropertiesToLoad.Add("legacyexchangedn");

mySearcher.PropertiesToLoad.Add("msexchmailboxguid");

// Console.WriteLine("AD Mailbox Information");

// Console.WriteLine("===============================");

w.WriteLine("sAMAccountName,mail,mailNickname,homeMDB,homeMTA,msExchHomeServerName,mDBUseDefaults,mDBStorageQuota,mDBOverQuotaLimit,mDBOverHardQuotaLimit,legacyExchangeDN,msExchMailboxGUID,ADPath");

SearchResultCollection ResEnt = mySearcher.FindAll();

foreach( SearchResult resEnt1 in ResEnt )

{

qWarn = "N/A";

qLimit = "N/A";

qDeny = "N/A";

AcctName = resEnt1.Properties["samaccountname"][0].ToString();

PrimaryMail = resEnt1.Properties["mail"][0].ToString();

mailnickname = resEnt1.Properties["mailnickname"][0].ToString();

homeMDB = resEnt1.Properties["homemdb"][0].ToString();

homeMTA = resEnt1.Properties["homemta"][0].ToString();

homeServer = resEnt1.Properties["msexchhomeservername"][0].ToString();

qDefaults = resEnt1.Properties["mdbusedefaults"][0].ToString();

if (qDefaults == "False")

{

qWarn = resEnt1.Properties["mdbstoragequota"][0].ToString();

qLimit = resEnt1.Properties["mdboverquotalimit"][0].ToString();

qDeny = resEnt1.Properties["mdboverhardquotalimit"][0].ToString();

}

legacyDN = resEnt1.Properties["legacyexchangedn"][0].ToString();

MailGUID = (byte[])resEnt1.Properties["msexchmailboxguid"][0];

ObjLoc = resEnt1.Path.ToString();

sMailGUID = HexEncoding.ToString(MailGUID);

w.Write("{0},{1},{2},'{3}','{4}',{5},{6},{7},{8},{9},{10},",AcctName,PrimaryMail,mailnickname,homeMDB,homeMTA,homeServer,qDefaults,qWarn,qLimit,qDeny,legacyDN);

foreach(byte b in MailGUID)

{

w.Write("{0:x2}",b);

}

w.WriteLine(",'{0}'",ObjLoc);

}


So given that I am searching correctly, if I wanted to modify attributes like homeMDB, could I within the foreach do something like resEnt1.Properties["homeMDB"][0] = "whatever I want it to be"

The idea here is that since I have already expended the resources to do the search whose handle is pointed to by the SearchResultCollection, cant I use the collection in someway to manipulate AD property values?

Thanks.