I have previously written a script to query ad user information from the ad domain. However, the results cannot be obtained from the workgroup. The following script can query domain account information from workgroup.
Note: before running the command, replace the DC Server, account name, and password, and then save them as vbs. If you want to query other information, you can change the corresponding filter, category, and class.
Option explicit
Dim objrootdse, strdnsdomain, adocommand, adoconnection
Dim strbase, strfilter, strattributes, strquery, adorecordset
Dim strdn, objns, strserver
Const ads_secure_authentication = & H1
Const ads_server_bind = & h200
'Specify a server (Domain Controller ).
Strserver = "Allen. Home"
'Termine DNS domain name. Use server binding and alternate
'Credentials. The value of strdnsdomain can also be hard coded.
Set objns = GetObject ("LDAP :")
Set objrootdse = objns. opendsobject ("LDAP: //" & strserver & "/rootdse ",_
Struser, strpassword ,_
Ads_server_bind or ads_secure_authentication)
Strdnsdomain = objrootdse. Get ("defaultnamingcontext ")
'Use ADO to search Active Directory.
'Use alternate credentials.
Set adocommand = Createobject ("ADODB. Command ")
Set adoconnection = Createobject ("ADODB. Connection ")
Adoconnection. provider = "adsdsoobject"
Adoconnection. properties ("User ID") = "youruser"
Adoconnection. properties ("password") = "yourpassword"
Adoconnection. properties ("encrypt password") = true
Adoconnection. properties ("ADSI flag") = ads_server_bind _
Or ads_secure_authentication
Adoconnection. Open "Active Directory provider"
Set adocommand. activeconnection = adoconnection
'Search entire domain. Use server binding.
Strbase = "<LDAP: //" & strserver & "/" & strdnsdomain & ">"
'Search for special users.
Strfilter = "(& (objectcategory = person) (objectclass = user) (samaccountname = mycode ))"
'Only get display name for user
Strattributes = "name"
'Construct the LDAP query.
Strquery = strbase & ";" & strfilter &";"_
& Strattributes & "; subtree"
'Run the query.
Adocommand. commandtext = strquery
Adocommand. properties ("page size") = 100
Adocommand. properties ("timeout") = 30
Adocommand. properties ("cache results") = false
Set adorecordset = adocommand. Execute
'Enumerating the result (for me only have one result)
Do until adorecordset. EOF
'Retrieve values.
Strdn = adorecordset. Fields ("name"). Value
Wscript. Echo strdn
Adorecordset. movenext
Loop
'Clean up.
Adorecordset. Close
Adoconnection. Close
For more information, see:
Searching with ActiveX Data Objects (ADO)
Http://msdn.microsoft.com/en-us/library/Aa746471.aspx
Search filter syntax
Http://msdn.microsoft.com/en-us/library/Aa746475.aspx