locked
how to get HomeMDBURL dynamically for creating mailbox for user? RRS feed

  • Question

  • Hi All,

    I want to get the HomeMDBURL dyanamically to create user & mailbox.

     

    DirectoryEntry container, user;

    CDOEXM.IMailboxStore mailbox;

    container = new DirectoryEntry("LDAP://cn=users," + DefaultNC);

    user = container.Children.Add("cn=" + CommonName, "user");

    user.Properties["sAMAccountName"].Add(SAMAccountName);

    user.CommitChanges();

    mailbox = (IMailboxStore)user.NativeObject;

    mailbox.CreateMailbox(HomeMDB);

     

    here is example of HomeMDB url. I highilighted the Variable values. If I have the values of these variables I can make a Url. Any Idea?

     

    "CN=Mailbox Store (MachineName),CN=Main Storage,"+ "CN=InformationStore,CN=MachineName,CN=Servers,"+ "CN=Development,CN=Administrative Groups,"+ "CN=OrganizationName,CN=Microsoft Exchange,CN=Services,"+ "CN=Configuration,DC=domain,DC=COM"

     

     Any Idea??

    thanks.............

    Sunday, May 9, 2010 4:53 AM

Answers

  • Hi,

    This is the powershell script which I ran on exchange 2007 successfully to get all mailbox database.

    I know ur target is ex2003 so u have to convert it into  C# code, which is not a big deal I know :)

     

    $dnc=([ADSI]"LDAP://rootDSE").defaultNamingContext
    
    Write-Host "AD Domain: $dnc" 
    
    $objDomain = New-Object System.DirectoryServices.DirectoryEntry
    
    $objDomain.Path = "LDAP://cn=configuration,$dnc"
    
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    
    $objSearcher.SearchRoot = $objDomain
    
    $objSearcher.Filter = ("ObjectClass=msExchPrivateMDB")
    
    $colProplist = "name"
    
    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
    
    $colResults = $objSearcher.FindAll()
    
    Write-Host "Databases found: "$colResults.Count
    
    foreach ($objResult in $colResults)
    
    {
    
    	$objDatabase = $objResult.Properties; 
    
     	Write-Host "Database: $($objDatabase.name)"
    
    }

    Note: U can also convert it into a vbscript easily if u have expertise in vbscript, so that u can verify the lines of code against your exchange 2003. I think you can even run this code on a domain joined machine where u have powershell installed, the AD Domain where ur exch 2003 exists.

    Hope this help u.

    Regards,


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
    • Edited by Laeeq Qazi Friday, May 14, 2010 10:33 PM format changes
    • Marked as answer by pratikmehta9 Wednesday, May 19, 2010 3:40 AM
    Friday, May 14, 2010 10:28 PM

All replies

  • The HomeMDB property is the distinguished name of the mail store where you want to create the mailbox. You can query Active Directory for any
    objectclass='msExchPrivateMDB'

    The problem is just picking one a random is generally not a great idea eg you should be trying to make a decision based on the location of the users the size of the mailstores available, recovery time for the particular user etc,etc

    Cheers
    Glen

     

     

    Tuesday, May 11, 2010 12:08 AM
  • Hi Glen,

           thanks for your suggestion, but I unable to understand your opinion, can you please go some deeper as I dont have much idea about how to query for the HomeMDB url...

          Now what I am following is I am asking following attributes from the user(as I am making an installer) and replace it into the HomeMDB url. I think that's the only solution.

    1) Exchange Server name

    2) Administrative Group

    3) Storage Group

    4)Mailbox store

    Above all are replaced in following HomeMDB url.(see bold letters).

    "CN=Mailbox Store,CN=Storage Group,"+ "CN=InformationStore,CN=ExchangeServerName,CN=Servers,"+ "CN=Administrative Group,CN=Administrative Groups,"+ distinguishedName

     

    And I am doing following for getting the domain name and distinguishedName for the homemdb url.

     

    DirectoryEntry

     

    iAdRootDSE;
    ADODB.
    Connection Conn = new ADODB.Connection(); ;
    ADODB.
    Command Com = new ADODB.Command();
    ADODB.
    Recordset Rs = new ADODB.Recordset();
    String strQuery;
    iAdRootDSE =
    new DirectoryEntry(LDAP://RootDSE);
    var varConfigNC = iAdRootDSE.Properties["configurationNamingContext"].Value;
    var varNamingC = iAdRootDSE.Properties["defaultNamingContext"].Value;
    domainName = varNamingC.ToString().Replace(
    "DC=", "").Replace(",", ".");
    Conn.Provider = "ADsDSOObject";
    Conn.Open(iAdRootDSE.Path,
    "", "", -1);

    strQuery =

    "<LDAP://" + varConfigNC + ">;(objectCategory=msExchOrganizationContainer);name,cn,distinguishedName;subtree";

    Com.ActiveConnection = Conn;
    Com.CommandText = strQuery;

    Rs.Open(strQuery, Conn, ADODB.CursorTypeEnum.adOpenUnspecified,
    ADODB.
    LockTypeEnum.adLockOptimistic, 1);
    Rs.MoveFirst();
    distinguishedName =
    Convert.ToString(Rs.Fields["distinguishedName"].Value);

    please comment on this, will it work on any exchange sever configuration??

    thanks again

    Friday, May 14, 2010 1:19 PM
  • Hi,

    This is the powershell script which I ran on exchange 2007 successfully to get all mailbox database.

    I know ur target is ex2003 so u have to convert it into  C# code, which is not a big deal I know :)

     

    $dnc=([ADSI]"LDAP://rootDSE").defaultNamingContext
    
    Write-Host "AD Domain: $dnc" 
    
    $objDomain = New-Object System.DirectoryServices.DirectoryEntry
    
    $objDomain.Path = "LDAP://cn=configuration,$dnc"
    
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    
    $objSearcher.SearchRoot = $objDomain
    
    $objSearcher.Filter = ("ObjectClass=msExchPrivateMDB")
    
    $colProplist = "name"
    
    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
    
    $colResults = $objSearcher.FindAll()
    
    Write-Host "Databases found: "$colResults.Count
    
    foreach ($objResult in $colResults)
    
    {
    
    	$objDatabase = $objResult.Properties; 
    
     	Write-Host "Database: $($objDatabase.name)"
    
    }

    Note: U can also convert it into a vbscript easily if u have expertise in vbscript, so that u can verify the lines of code against your exchange 2003. I think you can even run this code on a domain joined machine where u have powershell installed, the AD Domain where ur exch 2003 exists.

    Hope this help u.

    Regards,


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
    • Edited by Laeeq Qazi Friday, May 14, 2010 10:33 PM format changes
    • Marked as answer by pratikmehta9 Wednesday, May 19, 2010 3:40 AM
    Friday, May 14, 2010 10:28 PM
  • Hi Laeeq,

           thanks for the valuable code, but the problem is I have to create a mailbox in one mailbox database only, and the above code returns more than one(if the server has more than one), so for that purpose I am planning to ask some values from the user so that I can predict where user wantsto add the mailbox??

           So If possible can you advise what I am doing will not be failed at any situation?? or can I go for more optimize solution(means can I reduce some fields which I am asking from the user)??

    Thanks again...

     

    Saturday, May 15, 2010 12:08 PM
  • Hi Laeeq,

           thanks for the valuable code, but the problem is I have to create a mailbox in one mailbox database only, and the above code returns more than one(if the server has more than one), so for that purpose I am planning to ask some values from the user so that I can predict where user wantsto add the mailbox??

           So If possible can you advise what I am doing will not be failed at any situation?? or can I go for more optimize solution(means can I reduce some fields which I am asking from the user)??

    Thanks again...

     

    First of all i would like to suggest that as your original post was "How to get mailbox database from AD dynamically" so close this thread and mark one of above post as asnwser bcoz ur question has been answered.

    Your last post is just a User usability and GUI related question which is not related to Exchange. 

    As you have asked what to do in case of multiple mailbox DBs, so answer is obvious that show all databases to user for selection, and let the user select the mailbox DB at the time of mailbox creation. You can show MBX DBs in following format:

    "Server\MailboxDatabaseName"  OR

    "Server\StorageGroupName\MailboxDatabaseName"

    You should not ask user for

    1) Exchange Server name

    2) Administrative Group

    3) Storage Group

     bcoz once you will show the mailbox DB in one of my above mentioned format then those will be enough for the end user to understand which mailbox server and mailbox database is going to be targeted for mailbox.

    Hope this help u.

    Regards,


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
    Tuesday, May 18, 2010 6:07 PM