3

Is there a way to detect which version of Exchange Server is running (2003 or 2007 or 2010) via c#?

I have went through the here . But running the WMI query using "\ROOT\MicrosoftExchangeV2" is not supported in power shell 1.0.

Since i have scenerio to get all the users and groups mailbox list that found in the particular server and convert into csv files.

The following are way that we planned implement in C#

if(ExchangeVersion == "2003")
   GetExchange2003UserList();
else if(ExchangeVersion == "2007")
   GetExchange2007UserList();
else if(ExchangeVersion == "2010")
   GetExchange2010serList();

Please provide suggestions and also refer the links too

Community
  • 1
  • 1
Thangamani Palanisamy
  • 5,152
  • 4
  • 32
  • 39
  • does this topic help you? http://stackoverflow.com/questions/9705951/get-users-exchange-server-and-email-address-in-net – Marco Mar 07 '13 at 13:28

2 Answers2

3

I can be done using Active directory LDAP Query in the CN=Configuration,DC=domain,DC=local naming context. I am share code here:

 public static string getExchangeServerVersion()
        {
            try
            {
                string domain =Domain.GetCurrentDomain().ToString();
                DirectoryEntry rootDSE = new DirectoryEntry(string.Format("LDAP://{0}/rootDSE",  domain));
                DirectoryEntry objDirectoryEntry = new DirectoryEntry(string.Format("LDAP://{0}/{1}",domain,rootDSE.Properties["configurationNamingContext"].Value.ToString()));
                DirectorySearcher searcher = new DirectorySearcher(objDirectoryEntry, "(&(objectClass=msExchExchangeServer))");
                SearchResultCollection col = searcher.FindAll();
                string version = string.Empty;
                foreach (SearchResult result in col)
                {
                    DirectoryEntry user = result.GetDirectoryEntry();
                    if (String.Equals(user.Properties["name"].Value.ToString(),Dns.GetHostName(),StringComparison.InvariantCultureIgnoreCase))
                    {
                        version = user.Properties["serialNumber"].Value.ToString();
                        break;
                    }
                }
                return version;

            }
            catch (Exception ex)
            {
                Console.WriteLine("\nError : " + ex.Message);
                return "";
            }
        }

Main function :

static void main()
{

     string exchangeServerVersion = string.Empty;
     exchangeServerVersion =getExchangeServerVersion();
     if (exchangeServerVersion.Contains("Version 6"))
            {
                users.GetExchange2003UserList();
                GetADGroupList();
            }
            else if (exchangeServerVersion.Contains("Version 8"))
            {
                users.GetExchange2007UserList();
                GetADGroupList();
            }
            else if (exchangeServerVersion.Contains("Version 14"))
            {
                users.GetExchange2010UserList();
                GetADGroupList();
            }
}

Since ldap is supported by all the versions . Hope it will works for everyone.

And also please share if there any other way to getting the exchange version from C#.

Thangamani Palanisamy
  • 5,152
  • 4
  • 32
  • 39
0

Other way is to get it with powershell command lets

Get-ExchangeServer | Format-List Name,Edition,AdminDisplayVersion

razi
  • 1