eFeng.Leung

[爱我所爱][记我所想][学我所需]
posts - 25, comments - 13, trackbacks - 0, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

公告

枚举Exchange Server、SotreGroups和MailStore

Posted on 2007-03-26 16:25 eFeng.Leung 阅读(...) 评论(...) 编辑 收藏

转自 http://blog.joycode.com/liuhuimiao/archive/2007/03/04/94132.aspx

通过以下代码枚举列出所有的Exchange Server、StoreGroups和MailStore,并获取每个MailStore中Mailbox的数量。本段C#代码为http://www.ureader.com/message/513012.aspx一文中的VBNET代码改写而成,在Exchange 2003环境中测试通过。

通过这段代码,结合创建 Mailbox 的代码,可以实现获取Exchange环境的Server、StoreGroup、MailStore和Mailbox信息,或在指定Store(比如在所有Store中最少Mailbox的那个,或者人为指定目标Store)中创建Mailbox。

  

protected void Page_Load(object sender, EventArgs e)

        
{

            DirectoryEntry RootDSE 
= new DirectoryEntry("LDAP://RootDSE");

            
string rootPath = "LDAP://" + RootDSE.Properties["configurationNamingContext"].Value.ToString();

            DirectoryEntry configContainer 
= new DirectoryEntry(rootPath);

 

            DirectorySearcher configSearcher 
= new DirectorySearcher(configContainer);

            configSearcher.SearchRoot 
= configContainer;

            configSearcher.Filter 
= "(objectCategory=msExchExchangeServer)";

 

            
// Enumerate all Exchange Servers

            SearchResultCollection serverResults 
= configSearcher.FindAll();

            
foreach (SearchResult serverResult in serverResults)

            
{

                Response.Write(
"<br/><br/><font color=\"red\">=== Exchange Server: " + serverResult.GetDirectoryEntry().Properties["cn"].Value.ToString() + " ===</font><br/><br/>");

 

                SearchResultCollection storeGroups;

                SearchResultCollection stores;

                
int mailboxCount;

 

                
// Enumerate all Store Groups

                storeGroups 
= SearchContainer(serverResult.Properties["distinguishedName"][0].ToString(),

                    
"(objectCategory=msExchStorageGroup)");

                
foreach (SearchResult storeGroup in storeGroups)

                
{

                    
string storeGroupName = storeGroup.GetDirectoryEntry().Properties["cn"].Value.ToString();

                    Response.Write(storeGroupName 
+ "<br/>");

 

                    mailboxCount 
= 0;

 

                    
// Enumerate All Stores

                    stores 
= SearchContainer(storeGroup.Properties["distinguishedName"][0].ToString(),

                        
"(objectCategory=msExchPrivateMDB)");

                    
foreach (SearchResult store in stores)

                    
{

                        Response.Write(
"&nbsp;&nbsp;" + store.GetDirectoryEntry().Properties["cn"].Value.ToString() + "<br/>");

                        Response.Write(
"&nbsp;&nbsp;&nbsp;&nbsp;Number of Mailboxes: " +

                            store.GetDirectoryEntry().Properties[
"homeMDBBL"].Count.ToString() + "<br/>");

 

                        mailboxCount 
+= store.GetDirectoryEntry().Properties["homeMDBBL"].Count;

                    }


 

                    
string reportMsg = String.Format("Total Number of Mailboxes in Storage Group({0}): {1}<br/><br/>", storeGroupName, mailboxCount);

                    Response.Write(reportMsg);

                }


            }


        }


 

        
private SearchResultCollection SearchContainer(string srvPath, string strFilter)

        
{

            
string ldapPath = "LDAP://" + srvPath;

            DirectoryEntry serverContainer 
= new DirectoryEntry(ldapPath);

 

            DirectorySearcher serverSearcher 
= new DirectorySearcher(serverContainer);

            serverSearcher.Filter 
= strFilter;

            serverSearcher.SearchScope 
= SearchScope.Subtree;

 

            serverSearcher.PropertiesToLoad.Add(
"cn");

            serverSearcher.PropertiesToLoad.Add(
"distinguishedName");

            serverSearcher.PropertiesToLoad.Add(
"homeMDBBL");

 

            SearchResultCollection results 
= serverSearcher.FindAll();

 

            
return results;

        }



     
关于Exchange的收藏夹几个必备站点: