Tuesday, 22 October 2013

Find AD user location in VBScript by samAccountName

In this article I am going give the VBScript code to find Active Directory user's location (distinguishedname) by their samAccountName.

Summary:

 1. Find Active Directory User Location by samAccountName
 2. Find Active Directory User with Dynamic input of samAccountName

Find Active Directory User Location by samAccountName:

1. Copy the below example VBScript code and paste it in notepad or a VBScript editor.
2. Change the value for strUserName if you want to give your own name otherwise simply leave it.
3. Save the file with a .vbs extension, for example: FindADUser.vbs
4. Double-click the VBScript file (or Run this file from command window) to Find AD user.

 Click to get VBScript source code as a file Download FindADUser.vbs
' FindADUser.vbs
' Sample VBScript to find AD object .
' Author: http://www.morgantechspace.com/
' ------------------------------------------------------' 

Option Explicit
Dim adoCommand, adoConnection
Dim varBaseDN, varFilter, varAttributes
Dim objRootDSE, varDNSDomain, strQuery, adoRecordset, strName, strDN
Dim varSearchName

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")

varDNSDomain = objRootDSE.Get("defaultNamingContext")
varBaseDN = "<LDAP://" & varDNSDomain & ">"

varSearchName="Test"

' Filter on user objects.
varFilter = "(&(objectCategory=person)(objectClass=user)(samaccountname="& varSearchName &"))"

' Comma delimited list of attribute values to retrieve.
varAttributes = "samaccountname,distinguishedname"

' Construct the LDAP syntax query.
strQuery = varBaseDN & ";" & varFilter & ";" & varAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 1000
adoCommand.Properties("Timeout") = 20
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
    ' Retrieve values and display.
    strName = adoRecordset.Fields("samaccountname").Value
    strDN = adoRecordset.Fields("distinguishedname").value
    Wscript.Echo "Name: " & strName & "; Location: " & strDN
    ' Move to the next record in the recordset.
    adoRecordset.MoveNext
Loop

  If strName= "" then
      Msgbox "No user found with the name '"& varSearchName &"'"
   end if

' close ado connections.
adoRecordset.Close
adoConnection.Close

Find Active Directory User with Dynamic input of samAccountName:

1. Copy the below example VBScript code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: FindADUserWithDynamicName.vbs
3. Double-click the VBScript file (or Run this file from command window) to Find AD user.
4. Give the samAccountName of user which you want find and Click OK to proceed.

Click to get VBScript code as a file Download FindADUserWithDynamicName.vbs
' FindADUserWithDynamicName.vbs
' Sample VBScript to find AD object with dynamic samaccountname.
' Author: http://www.morgantechspace.com/
' ------------------------------------------------------' 
Option Explicit
Dim adoCommand, adoConnection
Dim varBaseDN, varFilter, varAttributes
Dim objRootDSE, varDNSDomain, strQuery, adoRecordset, strName, strDN
Dim varSearchName

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")

varDNSDomain = objRootDSE.Get("defaultNamingContext")
varBaseDN = "<LDAP://" & varDNSDomain & ">"

' Asks samAccountName from user.
Do
   varSearchName = InputBox ("Please enter user's samAccountName")
   If varSearchName = "" then
      Msgbox "No samAccountName entered"
   end if
Loop Until varSearchName <> ""


' Filter on user objects.
varFilter = "(&(objectCategory=person)(objectClass=user)(samaccountname="& varSearchName &"))"

' Comma delimited list of attribute values to retrieve.
varAttributes = "samaccountname,distinguishedname"

' Construct the LDAP syntax query.
strQuery = varBaseDN & ";" & varFilter & ";" & varAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 1000
adoCommand.Properties("Timeout") = 20
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
    ' Retrieve values and display.
    strName = adoRecordset.Fields("samaccountname").Value
    strDN = adoRecordset.Fields("distinguishedname").value
    Wscript.Echo "Name: " & strName & "; Location: " & strDN
    ' Move to the next record in the recordset.
    adoRecordset.MoveNext
Loop

  If strName= "" then
      Msgbox "No user found with the name '"& varSearchName &"'"
   end if

' close ado connections.
adoRecordset.Close
adoConnection.Close

Advertisements
Advertisements

1 comment:

  1. Hi , your code works really grt. It helped me in one of my urgent work. Thanks :)

    ReplyDelete