Query ad user information from workgroup

Source: Internet
Author: User
Tags ldap

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.