Somebody asked this morning about using WMI and Exchange in c#. There's a lot of good information on the Web about using the System.Management namespace to access WMI but not many Exchange specific C# samples at the moment. I decided to convert a few of the examples I've used in the scripts in my blog.
The first is a simple mailbox size example
System.Management.ConnectionOptions objconn = new System.Management.ConnectionOptions();
objconn.Impersonation = System.Management.ImpersonationLevel.Impersonate;
objconn.EnablePrivileges = true;
string cServername = "servername";
System.Management.ManagementScope exmangescope = new System.Management.ManagementScope(@"\\" + cServername + @"\root\MicrosoftExchangeV2",objconn);
System.Management.ObjectQuery objquery = new System.Management.ObjectQuery("SELECT * FROM Exchange_Mailbox");
System.Management.ManagementObjectSearcher objsearch = new System.Management.ManagementObjectSearcher(exmangescope,objquery);
System.Management.ManagementObjectCollection queryCollection1 = objsearch.Get();
string strDisplay;
foreach( System.Management.ManagementObject instmailbox in queryCollection1 )
{
strDisplay = instmailbox["MailboxDisplayName"].ToString() + " " + instmailbox["size"].ToString();
System.Console.WriteLine(strDisplay);
}
The second is the Mail-Enabling a Public Folder via WMI (Exchange 2003) from this post
System.Management.ConnectionOptions objconn = new System.Management.ConnectionOptions();
objconn.Impersonation = System.Management.ImpersonationLevel.Impersonate;
objconn.EnablePrivileges = true;
string cServername = "servername";
string cPublicFolderPath = "/foldertoenable/";
System.Management.ManagementScope exmangescope = new System.Management.ManagementScope(@"\\" + cServername + @"\root\MicrosoftExchangeV2",objconn);
System.Management.ObjectQuery objquery = new System.Management.ObjectQuery("Select * From Exchange_PublicFolder Where Path='" + cPublicFolderPath + "'");
System.Management.ManagementObjectSearcher objsearch = new System.Management.ManagementObjectSearcher(exmangescope,objquery);
System.Management.ManagementObjectCollection queryCollection1 = objsearch.Get();
foreach( System.Management.ManagementObject instmailbox in queryCollection1 )
{
System.Console.WriteLine(instmailbox["path"].ToString());
System.Console.WriteLine(instmailbox["IsMailEnabled"].ToString());
instmailbox["IsMailEnabled"] = true;
instmailbox.Put();
}
I've posted a txt file version of this up here
The first is a simple mailbox size example
System.Management.ConnectionOptions objconn = new System.Management.ConnectionOptions();
objconn.Impersonation = System.Management.ImpersonationLevel.Impersonate;
objconn.EnablePrivileges = true;
string cServername = "servername";
System.Management.ManagementScope exmangescope = new System.Management.ManagementScope(@"\\" + cServername + @"\root\MicrosoftExchangeV2",objconn);
System.Management.ObjectQuery objquery = new System.Management.ObjectQuery("SELECT * FROM Exchange_Mailbox");
System.Management.ManagementObjectSearcher objsearch = new System.Management.ManagementObjectSearcher(exmangescope,objquery);
System.Management.ManagementObjectCollection queryCollection1 = objsearch.Get();
string strDisplay;
foreach( System.Management.ManagementObject instmailbox in queryCollection1 )
{
strDisplay = instmailbox["MailboxDisplayName"].ToString() + " " + instmailbox["size"].ToString();
System.Console.WriteLine(strDisplay);
}
The second is the Mail-Enabling a Public Folder via WMI (Exchange 2003) from this post
System.Management.ConnectionOptions objconn = new System.Management.ConnectionOptions();
objconn.Impersonation = System.Management.ImpersonationLevel.Impersonate;
objconn.EnablePrivileges = true;
string cServername = "servername";
string cPublicFolderPath = "/foldertoenable/";
System.Management.ManagementScope exmangescope = new System.Management.ManagementScope(@"\\" + cServername + @"\root\MicrosoftExchangeV2",objconn);
System.Management.ObjectQuery objquery = new System.Management.ObjectQuery("Select * From Exchange_PublicFolder Where Path='" + cPublicFolderPath + "'");
System.Management.ManagementObjectSearcher objsearch = new System.Management.ManagementObjectSearcher(exmangescope,objquery);
System.Management.ManagementObjectCollection queryCollection1 = objsearch.Get();
foreach( System.Management.ManagementObject instmailbox in queryCollection1 )
{
System.Console.WriteLine(instmailbox["path"].ToString());
System.Console.WriteLine(instmailbox["IsMailEnabled"].ToString());
instmailbox["IsMailEnabled"] = true;
instmailbox.Put();
}
I've posted a txt file version of this up here