Friday, June 18, 2021

Script to Search for a User in Active Directory

Author:Yi Helen Wang [email protected]
Platform:Windows NT

Windows 2000


This script is for an administrator who wants to search for a particular user through Active Directory of local or remote Windows 2000 servers. It also prevents duplication when new users are added into Active Directory.

Scroll down to view the script.


'Author: Yi Helen Wang
'Purpose:Searching users through an Active Directory (AD) of
'Windows 2000 to insure against duplicating when 
'a new user is added into AD
'Keywords:ADO and ADSI 2.5 or up
'Comment:You have to assign a permission to get Active Directory 
'  information.  This program is run locally or remotely
on error resume next

dim oContainer,searchpath
dim oRoot
dim oConnect, oCommand, rs
dim strConnect, strDomainCtrl, strOU
dim cntuser, newuser, strCN, strSearchUser

newuser = false
cntuser = 0

'to get LDAP namespace
set oRoot = GetObject("LDAP: //RootDSE")
call ShowError("Permission issues !")
strDomainCtrl = oRoot.Get("defaultNamingContext")

strOU = InputBox("Please entry an Organization Unit Name to search: ", "Searching","Legal")
if Len(Trim(strOU)) = 0 then
  msgbox "No information typed in!" 
  strConnect = "LDAP://OU=" & strOU & ", " & strDomainCtrl
  set oContainer = GetObject(strConnect)
  call ShowError("OUNameNotExist")
  searchpath = oContainer.ADsPath

  strSearchUser = InputBox("Please entry the user searched initial: ", "Searching", "sls")
  if Len(Trim(strSearchUser))=0  then
msgbox "No information typed in!"
'Using ADO to query the Active Directory for a particule user
set oConnect = CreateObject("ADODB.Connection")
set oCommand = CreateObject("ADODB.Command")
call ShowError("ADOProblems")

'opening the connection
oConnect.Provider = "ADsDSOObject"
oConnect.Open "Active Directory Provider"
call ShowError("ADOProvider")
 'creating a command object for this connection
 Set  oCommand.ActiveConnection = oConnect

 strCN = strSearchUser
 DO WHILE newuser  True
  oCommand.CommandText = "SELECT samAccountName FROM '" & searchpath & "' WHERE objectClass='user' AND samAccountName = '" & strSearchUser & "'"

  'creating a recordset based on the ADO command
  set rs = oCommand.Execute
  'Navigating the record set
  if rs.EOF and rs.BOF then
msgbox  "New user"
newuser = True
cntuser = cntuser + 1
'rename and check again till no duplication
strSearchUser = left(strCN,1) & right(strCN,1) & cntuser
msgbox  "Rename CN as " & strSearchUser
set rs = nothing
 end if

  end if
end if

sub ShowError(strErr)
  if err  0 then

if err = 432 or err=70 then
wscript.echo "Permission issues to run the program"
select case strErr
 case "OUNameNotExist"
  wscript.echo "OU name is not correct or not exist !"
 case "ADOProblems"
  wscript.echo "ADO problems!"
 case "ADOProvider"
  wscript.echo "ADO Provider Problems!"
end select
end if

  end if
end sub

Disclaimer: We hope that the information in these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by me. I shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

Similar articles

Latest Articles

Top Data Visualization Tools...

The amount of data generated and consumed by organizations is growing at an astounding rate. The total volume of data and information worldwide has...

The Data Capture Market

Data capture is the process of collecting, ingesting, or otherwise acquiring structured and unstructured data and either converting it into a data format usable...

NVIDIA and the Move...

NVIDIA recently held a Q&A with its visionary CEO Jensen Huang.   While the Q&A this week focused on NVIDIA’s announcements at Computex, his opening and...

Acquia Updates Open Digital...

BOSTON – Acquia’s Drupal-based customer experience (CX) platform is looking different to enterprise users. Acquia made updates last quarter across its three-part Open Digital Experience...