Home > Active Directory, ADSI, LDAP, Microsoft, VBScript, WMI > VBScript to Bulk Update UPNs in Active Directory

VBScript to Bulk Update UPNs in Active Directory

I had a requirement to harvest all the users in a domain into an Excel spreadsheet then update the ones with the wrong UPN after some faceless entity added the wrong UPN to a number of user accounts:

sDomain="testlab"

Set oADconn = CreateObject("NameTranslate")
oADconn.Init 3, ""

Set oExcel= WScript.CreateObject("excel.application")
With oExcel
 .Visible = True
 .Workbooks.Add
 .Range("A1:C1").Select
 .Selection.Font.Bold = True
 .Cells(1,1).Value = "Display Name"
 .Cells(1,2).Value = "sAMAccountName"
 .Cells(1,3).Value = "UPN Suffix"
 .ActiveSheet.range("A2").Activate
End With

Set objDomainUsers = GetObject("WinNT://" & sDomain & ",domain")
objDomainUsers.Filter = Array("User")
On Error Resume Next
For Each oUserAcct In objDomainUsers
 oADconn.Set 3, sDomain & "\" & oUserAcct.Name
 sUserDN = oADconn.Get(1)
 Set oUser = GetObject("LDAP://" & sUserDN)
 sname=oUser.sAMAccountname
 supn=oUser.userprincipalname
 x=instr(supn,"@")
 supnsuffix=mid(supn,x+1)
 oExcel.activecell.Value=oUser.displayname
 oExcel.activecell.offset(0,1).Activate
 oExcel.activecell.Value=oUser.sAMAccountName
 oExcel.activecell.offset(0,1).Activate
 oExcel.activecell.Value=supn
 oExcel.activecell.offset(0,1).Activate
 oExcel.activecell.offset(1,-3).Activate
 if supnsuffix="testlab.priv" then
 oUser.userprincipalname=sname & "@corporate.test.int"
 oUser.SetInfo
 end if
Next
oExcel.Application.Quit
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: