Hey, Scripting Guy! Question

Hey, Scripting Guy! How do I get a list of the Exchange servers (msExchHomeServerName) assigned to the users in an OU?

-- BG

SpacerHey, Scripting Guy! AnswerScript Center

Hey, BG. As you probably already figured out, you have two issues: you need to get a list of all the users in an OU, and then you need to figure out which Exchange server they’ve been assigned. Believe it or not, this is probably one of the easiest scripts you’ll ever have to write.

Well, OK, that we’ll ever have to write for you ….

Let’s start by getting a list of all the users in an OU. There really isn’t much to say about that; all you have to do is bind to the OU and you automatically get back a collection of all the objects in that OU. Thus if we want to get a collection of all the objects found in the Finance OU (and echo back the object’s common name, or CN), we can use this code:

Set objOU = GetObject("LDAP://OU=Finance,DC=fabrikam,DC=com")
objOU.Filter = Array("user")

For Each objItem in objOU
    Wscript.Echo objItem.CN
Next

Notice the one little “trick” we did here. We set a filter on the collection using this line of code:

objOU.Filter = Array("user")

This tells our script, hey, filter out everything in the collection except user accounts. Why do we do that? Well, suppose you have computer accounts or print queues or other objects in the OU. We don’t want to try to determine the Exchange server for a bunch of print queues, so we use the filter to weed out everything except user accounts.

And now comes the “hard” part: retrieving each user’s Exchange server. Of course, that’s hard only if you don’t know that the name of the attribute. But seeing as how you are told us the attribute you’re looking for is msExchHomeServerName, well:

Set objOU = GetObject("LDAP://OU=Finance,DC=fabrikam,DC=com")
objOU.Filter = Array("user")

For Each objItem in objOU
    Wscript.Echo objItem.CN
    Wscript.Echo objItem.msExchHomeServerName
Next

And just for the heck of it, here are a few more Exchange-related properties you might want to grab while you’re at it:

Set objOU = GetObject("LDAP://OU=Finance,DC=fabrikam,DC=com")
objOU.Filter = Array("user")

For Each objItem in objOU
    Wscript.Echo objItem.CN
    Wscript.Echo objItem.mailNickname
    Wscript.Echo objItem.msExchHomeServerName
    Wscript.Echo objItem.homeMDB
    Wscript.Echo objItem.mdbUseDefaults
Next