Link to home
Create AccountLog in
Avatar of amnhtech
amnhtechFlag for United States of America

asked on

Querying AD for Department membership for 1500 user script

I have a file with names / First , Last / and I need to get everybodys department from AD. What the easiest way to automate this?
Avatar of bsharath
bsharath
Flag of India image

Hi,

Save this file as vbs and run
You will get all users from the ad with department.

I got this from one of the posts in EE.

Option Explicit

Const ADS_SCOPE_SUBTREE = 2

Dim objConnection, objCommand, objRecordSet, objRootDSE, objFileSystem, objFile, objUser
Dim strDisplayName, strDepartment, strUserName

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
objCommand.CommandText = "SELECT aDSPath, displayName, sAMAccountName, department FROM " &_
      "'LDAP://" & objRootDSE.Get("defaultNamingContext") & "' WHERE objectClass='user'"

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 600
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False

Set objRecordSet = objCommand.Execute
Set objRootDSE = Nothing

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.OpenTextFile("UsersAndDepartments.txt", 2, True, 0)

While Not objRecordSet.EOF
      Set objUser = GetObject(objRecordSet.Fields("aDSPath"))
      If objUser.Class = "user" Then
            strDisplayName = ""
            If Not IsNull(objRecordSet.Fields("displayName")) Then
                  strDisplayName = objRecordSet.Fields("displayName")
            End If
            strDepartment = ""
            If Not IsNull(objRecordSet.Fields("department")) Then
                  strDepartment = objRecordSet.Fields("department")
            End If
            strUserName = objRecordSet.Fields("sAMAccountName")
           
            objFile.WriteLine strDisplayName & VbTab & strUserName & VbTab & strDepartment
      End If
      objRecordSet.MoveNext
Wend
objConnection.Close

Set objFile = Nothing
Set objFileSystem = Nothing

Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Avatar of amnhtech

ASKER

actually my goal here is to input names from a file and find out their department membership
Sorry i dont know of a way as such.You can still get all data put in excel sort the data and match with your list...

May be some other expert may help with this post...
ASKER CERTIFIED SOLUTION
Avatar of Farhan Kazi
Farhan Kazi
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thank you very much for the script it looks like there is a small problem taht i can't figure out. Here is the errors i am getting

Querying:  John Smith

dsquery failed:No superior reference has been configured for the directory servi
ce. The directory service is therefore unable to issue referrals to objects outs
ide this forest.

Please advise
On what OS you are running this script?

Can please run following commands (one by one) and post the results?

Click Start -> Run -> Cmd.exe

DSQuery * -Filter "(&(samAccountType=805306368)(givenName=John)(sn=Smith))"

DSQuery * DomainRoot -Filter "(&(samAccountType=805306368)(givenName=John)(sn=Smith))"

DSQuery * ForestRoot -Filter "(&(samAccountType=805306368)(givenName=John)(sn=Smith))"

DSQuery user -name John*

DSQuery user DomainRoot -name *

DSQuery user ForestRoot -name *
acctually i figured out i had a problem with cvs file there were a space infront the names. Thank you very much.
Great! Thanks for the points.