Results 1 to 4 of 4

Thread: Active Directory VBScript to get user's OU information

  1. #1
    Join Date
    Jan 2004
    Posts
    42

    Active Directory VBScript to get user's OU information

    We are running a Windows 2003 Server with AD. There are several user clients connected to this domain. Due to some reasons I want check the list of users and their primary SMTP email addresses with their department value in accounts. In order to do the same I created a VBScript but it is not working. Also another issues that is coming up is LDAP ADSI provider wants the exact OU container of each account before it can list email address and department. But this is difficult because the users in this list are scattered across different OUs.

    So can anybody please tell me how can I pull exact OU location of the accounts on my AD? For example: “cn=user,OU=Users,OU=xxx,DC=yyy,DC=local"

    Thanks for your helps.

  2. #2
    Join Date
    Jan 2006
    Posts
    605

    Re: Active Directory VBScript to get user's OU information

    It can be quite difficult if your users have Common Names in them. Most probably your list is of "pre-Windows 2000 logon" names, which is also called NT names. Anyways, just check this example, it might work for you:

    ========
    Const ForReading = 1
    ' Constants for the NameTranslate object.
    Const ADS_NAME_INITTYPE_GC = 3
    Const ADS_NAME_TYPE_NT4 = 3
    Const ADS_NAME_TYPE_1779 = 1

    ' Determine DNS name of domain from RootDSE.
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootDSE.Get("defaultNamingContext")

    ' Use the NameTranslate object to find the NetBIOS domain name from the
    ' DNS domain name.
    Set objTrans = CreateObject("NameTranslate")
    objTrans.Init ADS_NAME_INITTYPE_GC, ""
    objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
    strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
    ' Remove trailing backslash.
    strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)

    ' Specify file of user names.
    strFile = "c:\Scripts\Users.txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFile, ForReading)

    Do Until objFile.AtEndOfStream
    strNTName = Trim(objFile.ReadLine)
    ' Skip blank lines.
    If (strNTName <> "") Then
    ' Use the Set method to specify the NT format of the object name.
    ' Trap error if user does not exist.
    On Error Resume Next
    objTrans.Set ADS_NAME_TYPE_NT4, strNTName
    If (Err.Number = 0) Then
    On Error GoTo 0
    ' Use the Get method to retrieve the RPC 1779 Distinguished
    Name.
    strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

    ' Bind to the user object (if desired).
    Set objUser = GetObject("LDAP://" & strUserDN)
    ' Do whatever you want...
    Else
    On Error GoTo 0
    ' Alert user about bad user name.
    Wscript.Echo "User " & strNTName & " does not exist"
    End If
    End If
    Loop

    objFile.Close
    ========

  3. #3
    Join Date
    Sep 2004
    Posts
    140

    Re: Active Directory VBScript to get user's OU information

    I have used the following script long ago and it has worked for me perfectly. I have made some changes in the same according to your situation. You should give it a try. This script will enumerate all users in the domain and
    produce a report for you. The report is tab separated, so you can open
    it in Excel.

    Code:
    Dim qQuery, objConnection, objCommand, objRecordSet, obj
    Dim oRootDSE, strDomain
    
    Set oRootDSE = GetObject("LDAP://rootDSE")
    strDomain = oRootDSE.get("defaultNamingContext")
    
    ' other categories = computer, user, printqueue, group
    qQuery = "<LDAP://" & strDomain &">;" & _
    "(objectCategory=person)" & _
    ";name,proxyAddresses,department;subtree"
    
    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand = CreateObject("ADODB.Command")
    objConnection.Open "Provider=ADsDSOObject;"
    objCommand.ActiveConnection = objConnection
    objCommand.CommandText = qQuery
    Set objRecordSet = objCommand.Execute
    
    While Not objRecordSet.EOF
    Report = Report & objRecordSet.Fields("name")
    For Each address In proxyAddresses
    If Left(address,4) = "SMTP" Then
    Report = Report & vbTab & address & vbTab
    End If
    Next
    Report = Report & objRecordSet.Fields("department") & vbCrLf
    objrecordset.MoveNext
    Wend
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set ts = objFSO.CreateTextFile("SMTPDepartmentReport.txt",True)
    ts.Write Report
    ts.Close
    objConnection.Close

  4. #4
    Join Date
    Jan 2004
    Posts
    42

    Re: Active Directory VBScript to get user's OU information

    Thank you very much for the helps guys. Your script really worked.. Using this i was able to get the information I wanted.

    Help appreciated.

Similar Threads

  1. Need information on Active Directory
    By Raj@LOL in forum Active Directory
    Replies: 5
    Last Post: 07-04-2012, 08:28 PM
  2. Export user information Active Directory to openldap
    By MobilePhoneGuru in forum Networking & Security
    Replies: 3
    Last Post: 24-10-2009, 10:14 AM
  3. active directory user properties new tab
    By hatred in forum Active Directory
    Replies: 1
    Last Post: 26-03-2008, 11:15 PM
  4. Rename active directory user
    By Rachel G in forum Active Directory
    Replies: 1
    Last Post: 19-09-2007, 02:21 AM
  5. Replies: 5
    Last Post: 13-06-2007, 07:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,699,941.39917 seconds with 17 queries