Modify upn for users3 (VBScript)

This code can be found in Chapter 6 of Active Directory Cookbook, 2nd edition

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' This VBScript code sets the userPrincipalName attribute for all
' users in every OU in a domain regardless of depth.
' This won't modify users in the default Users container which you 
' probably don't want anyway.  Note that this isn't the most efficient
' way to accomplish this task.  An ADO query would be much faster, but
' you'd have to make sure to exclude users in the default Users container.

' ---------------------------------------------------------------
' Provided as a web-only addition for the book:
'     "Active Directory Cookbook" by Robbie Allen
' ISBN: 0-596-00466-4
' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
strDomain = "<DomainDNSName>" ' e.g. rallencorp.com
' ------ END CONFIGURATION ---------

set objRootDSE = GetObject("LDAP://" & strDomain & "/RootDSE")
set objDomain = GetObject("LDAP://" & objRootDSE.Get("defaultNamingContext"))
objDomain.Filter = Array("organizationalunit")
for each objOU in objDomain
   ModUsers objOU.ADsPath, strSpaces & " " 
next

Function ModUsers ( strADsPath, strSpaces )
   set objParent = GetObject(strADsPath)
   objParent.Filter = Array("user")
   WScript.Echo strSpaces & objParent.Name
   for each objUser in objParent
      if objUser.Class = "user" then
         Wscript.Echo strSpaces & " User = " & objUser.Get("sAMAccountName")
         objUser.Put "userPrincipalName", _
                      objUser.Get("sAMAccountName") & "@" & strDomain
         objUser.SetInfo
      end if
   next
   objParent.Filter = Array("organizationalunit")
   for each objCont in objParent
      ModUsers objCont.ADsPath, strSpaces & " " 
   next
End Function

This code has been viewed 2491 times.

New from the creators of TechTasks.com: StatSheet.com