Search Windows and Linux Networking

Thursday, June 23, 2011

Search for the user in Active Directory to modify using VBScript

Search for the user in Active Directory to modify user using VBScript.
        Most frequently task of Active Directory System Admin is modify user's property. such as if user Designation has been  changed,  or manager or change in address and many more resonance to modify one particular user or group of users.  This task can be accomplish by in few min with the help of vbscript.I will show you how to do that. we use our last script that search for the users with First and Last name. and then modify that user description and save that user in active directory. or you can do the same task for all users or by reading Excel file then search in active directory for the users and modify user as per given in Excel file. in next post I will also show that .for now we use our last script and make some modification to done our job.


Option Explicit
Dim adocommand, adoconnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN , strGivenNAme ,strSN, strmail, strADsPath
Dim objUser

'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")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on user objects.
strFilter = "(&(objectCategory=person)(objectClass=user)(givenName=sandeep)(sn=kapadane))"

'Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,cn,givenName,sn,mail,ADsPath"

'Constuct the LDAP syntax query
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adocommand.commandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("TimeOut") = 30
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
   strCN = adoRecordset.Fields("cn").Value
   strGivenName = adoRecordset.Fields("givenName")
   strSN = adoRecordset.Fields("sn").Value
   strmail = adoRecordset.Fields("mail").Value
   strADsPath = adoRecordset.Fields("AdsPath").Value
   Wscript.Echo "Display Name: " & strCN & ", ADsPath:" & strADsPath
   
   ' Bind object to modify
   set objUser = GetObject(strADsPath)
   objUser.Put "description","System & Network Administrator"
   objUser.SetInfo
   WScript.Echo "User " & strCN & "is modified"
   'Move to the Next Record in the recordset.
   adoRecordSet.MoveNext
Loop

'Clean up.
adoRecordSet.Close
adoConnection.Close

' Display Massage All Done

MsgBox "Done"

No comments:

Post a Comment