Vbscript OU path of mailbox

raju1706
raju1706 used Ask the Experts™
on
Hi, I need help with a vbscript, which will return the full OU path of each mailbox in exchange 2003 server.

Thank You
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hello,

Your question isn't clear : mailboxes aren't stored in OU in Exchange 2003 but in Storage/admin groups. Did you mean that you want the OU of each users that has a mailbox attached and the corresponding mailbox name ?

Author

Commented:
Thank You pilozite for your response. It is just the OU name corresponding to mailbox name..

Commented:
Hello,

you can adapt this script (found here : http://windowsitpro.com/article/articleid/84585/jsi-tip-9843-how-can-i-use-vbscript-to-return-all-the-users-in-my-domain.html)

like in the code snippet (save this script as "get-user.vbs" and type in command line

get-user.vbs *
n Error Resume Next
Dim objConnection, objCommand, objRootDSE, strDNSDomain
Dim strFilter, strQuery, objRecordSet, objArgs
Set objArgs = Wscript.Arguments
if objArgs.Count = 0 then 
 Wscript.Echo  "sAMAccountName argument required. ""*"" or ""J*"" or etc.."
 WScript.Quit (1)
End If
sam = objArgs(0)
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
'Get domain
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
'Define the filter elements
strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & sam & "))"
'List all attributes you will require
strAttributes = "distinguishedName,sAMAccountName,givenName,sn,userPrincipalName"
'compose query
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 99999
objCommand.Properties("Timeout") = 300
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    strDN = objRecordSet.Fields("distinguishedName")
    strGN = objRecordSet.Fields("mailbox")
    Wscript.Echo strGN & ";" & strDN
    objRecordSet.MoveNext
Loop
' Clean up.
objConnection.Close
Set objConnection = Nothing
Set objCommand = Nothing
Set objRootDSE = Nothing
Set objRecordSet = Nothing

Open in new window

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Commented:
here is the corrected script (tested). You get the DistinguishedName and mail adress for every user
On Error Resume Next
Dim objConnection, objCommand, objRootDSE, strDNSDomain
Dim strFilter, strQuery, objRecordSet, objArgs
Set objArgs = Wscript.Arguments
if objArgs.Count = 0 then 
 Wscript.Echo  "sAMAccountName argument required. ""*"" or ""J*"" or etc.."
 WScript.Quit (1)
End If
sam = objArgs(0)
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
'Get domain
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
'Define the filter elements
strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & sam & "))"
'List all attributes you will require
strAttributes = "distinguishedName,sAMAccountName,givenName,sn,userPrincipalName,mail"
'compose query
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 99999
objCommand.Properties("Timeout") = 300
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    strDN = objRecordSet.Fields("distinguishedName")
    strGN = objRecordSet.Fields("mail")
    Wscript.Echo strGN & ";" & strDN
    objRecordSet.MoveNext
Loop
' Clean up.
objConnection.Close
Set objConnection = Nothing
Set objCommand = Nothing
Set objRootDSE = Nothing
Set objRecordSet = Nothing

Open in new window

Author

Commented:
Hi pilozite Thank you, I need to modify the script, to get for each user in a server. I have written a script, to get mailbox details for a server. For each mailbox, in that list i need the OU name.

Commented:
you can save your mailbox name to a text file, then read this text file from a vbscript that will look at the user that got this email adress in AD. Then you get the parent container for the user.

By the way, using PowerShell will be MUCH simpler for this kind of query, is it a possibility for you ?

Author

Commented:
pilozite Thank You. Powershell is not a possibility. If the mailbox list has a name like "Turner, Walter" the above script does not execute for that name. Can you tell me where I am going wrong?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial