Use vbs to determine the user's login name Code

Source: Internet
Author: User

Q:
Hello, script expert! How do I determine the user login name of a user named John Smith?

-- FR

A:
Hello, FR. You know, we would like to tell you how to determine the user name of a user named John Smith, but we cannot do this: because there is a specific name list in one of our sample scripts, the username that can only be referenced. Unfortunately, John Smith is not in this list, so we cannot use his name.

Yes, we also feel uncomfortable about this, but we can't do anything about it. But what I want to tell you is: how is the script for determining the User Login Name of a user named Ken Myer? Yes, we know: they are different, aren't they? But we can only do this at most:

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject ("ADODB. Connection ")
Set objCommand = CreateObject ("ADODB. Command ")
ObjConnection. Provider = "ADsDSOObject"
ObjConnection. Open "Active Directory Provider"
Set objCommand. ActiveConnection = objConnection

Objectcommand. Properties ("Page Size") = 1000
ObjCommand. Properties ("Searchscope") = ADS_SCOPE_SUBTREE

ObjCommand. CommandText = _
"SELECT sAMAccountName FROM 'ldap: // dc = fabrikam, dc = com 'WHERE objectCategory = 'user '"&_
"AND givenName = 'ken' AND sn = 'myer '"
Set objRecordSet = objCommand. Execute

ObjRecordSet. MoveFirst
Do Until objRecordSet. EOF
Wscript. Echo objRecordSet. Fields ("sAMAccountName"). Value
ObjRecordSet. MoveNext
Loop

You may have discovered that this is a script used to search for Active Directory. We do not intend to explain each line of code used in this script one by one, which will take too much time. If you are not familiar with the Active Directory Search script, we recommend that you take a look at our two "script stories" series Dude: Where's My Printer? All the strange things you see in this script-ADsDSOObject, DS_SCOPE_SUBTREE, and ADODB. Command-are described in detail in these two columns.

However, we will point out several things related to the query used for the search. When writing a script for searching Active Directory, the most difficult part may be to know the attribute name to be searched. For example, the user login name you mentioned. We know the meaning of the user login name, and you also know the meaning of the user login name, but Active Directory does not know what the user login name is. Active Directory calls sAMAccountName instead. (Note: although the uppercase and lowercase letters are irrelevant, We will write this attribute name into sAMAccountName, just because it is the formal name of this attribute. Therefore, the SQL query retrieves the sAMAccountName of the specified user.

How to specify this user? Well, we need to find the Active Directory objects that meet the following three conditions:

• Is a user account. To limit the returned data to user accounts, we need to search for items with objectCategory equal to user.

• The name is Ken. Of course, Active Directory does not know what a "name" is ". Therefore, we need to search for users whose givenName is Ken.

• The last name is Myer. As expected, Active Directory never heard of the word "last name. Therefore, we need to search for sn (surname) Myer.

Add all the preceding conditions, and the query is as follows:

ObjCommand. CommandText = _
"SELECT sAMAccountName FROM 'ldap: // dc = fabrikam, dc = com 'WHERE objectCategory = 'user '"&_
"AND givenName = 'ken' AND sn = 'myer '"

The rest is easy. When this query is executed, Active Directory returns a record set consisting of all user groups whose giveName is Ken and whose sn is Myer. Then, set a Do Until loop to traverse the record set and display the sAMAccountName of each user. (Ideally, there is only one Ken Myer in Active Directory, but there may actually be multiple users with the same name and surname. In this case, sAMAccountName is a difference, because sAMAccountNames must be unique .)

Do you understand? Okay, let's see (don't tell anyone what we said). Take the script we just introduced to you, replace Ken with John, and replace Myer with Smith, you will get a script to search for a user named John Smith. However, this is just a secret between you and us, OK? Good

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.