Update User Information (LDAP query)

Find all the users with a specific Office in Active Directory (physicalDeliveryOfficeName) and update it to something new. This script could also be modified to target different user attributes.

'OfficeRename.vbs
'
' Find all the users with a specific Office in AD and
' update their Office to a new Name.
'
Option Explicit

Dim objRootDSE, strDNSDomain, adoCommand, adoConnection
Dim strBase, strFilter, strAttributes, strQuery, adoRecordset
Dim strDN, strFirstName, strLastName, objUser, strSite,strUserName, strOldOffice, StrNewOffice, intOfficeLen


strOldOffice = "College House"
strNewOffice = "New Grand Central"

intOfficeLen = len(strOldOffice)

' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

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

' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on all user objects.
strFilter = "(&(objectCategory=person)(objectClass=user))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedName,givenName,sn,physicalDeliveryOfficeName"

' 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

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
    ' Retrieve values.
    strDN = adoRecordset.Fields("distinguishedName").Value
    strDN = Replace(strDN, "/", "\/")
    strSite = adoRecordset.Fields("physicalDeliveryOfficeName").Value
    strFirstName = adoRecordset.Fields("givenName").Value & ""
    strLastName = adoRecordset.Fields("sn").Value & ""
    strUserName = adoRecordset.Fields("distinguishedName").Value

    If (left(strSite,intOfficeLen) = strOldOffice) Then
        ' Echo User
         wscript.echo strFirstName & "." & strLastName & " " & strUserName

        ' Bind to user object.
        Set objUser = GetObject("LDAP://" & strDN)
        ' Assign value to physicalDeliveryOfficeName attribute.

        objUser.physicalDeliveryOfficeName = strNewOffice
        ' Save change.
        objUser.SetInfo
    End If
    adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close

' Clean up.
Set objRootDSE = Nothing
Set adoCommand = Nothing
Set adoConnection = Nothing
Set adoRecordset = Nothing

“Nearly all men can stand adversity, but if you want to test a man's character, give him power” ~ Abraham Lincoln

Related:

ListUsers - List all Users
UserInfo - List properties of a User (as shown in ADUC)
UserName - List user's simple name when called with a Distinguished Name
SearchAD - Search AD for either Users, Computers or Workgroups


© Copyright SS64.com 1999-2013
Some rights reserved