From: Paco on
Hi,

Because of changes in my organization, I have been required to modify the UPN and prewindows2000 account name (samaccountname) for all the users within the domain (active directory Windows 2003).

The present rules for user's UPN's and samaccountname is the first name, dot the surname. I.e, if a User is called Jonh Doe, both his UPN and samaccountname is john.doe (@domain.com)

Right. The new requirements, are that the new UPN's and samaccounts have to be ddsssiii, where dd is the city where the user works, sss is the department, and iii are his initials. For example, if John Doe works at New York and belongs to the IT staff, his UPN and samaccount has to be nyitjd.

I am able to make a excel sheet with two columns, first with the present username, and second with the future username. But I need an vbscript which read from the first and second columns of that sheet, and change the present UPN and samaccountname of all user accounts in the domain to the new values.

Any help?




Submitted via EggHeadCafe - Software Developer Portal of Choice
WPF GridView Sample To Insert, Update, and Delete Records
http://www.eggheadcafe.com/tutorials/aspnet/fc9a5bf6-f5bb-4443-a92a-c9a46fd3aeb2/wpf-gridview-sample-to-in.aspx
From: Richard Mueller [MVP] on

"Paco Gaspar" wrote in message news:2010567202fjgaspar(a)fake.com...
> Hi,
>
> Because of changes in my organization, I have been required to modify the
> UPN and prewindows2000 account name (samaccountname) for all the users
> within the domain (active directory Windows 2003).
>
> The present rules for user's UPN's and samaccountname is the first name,
> dot the surname. I.e, if a User is called Jonh Doe, both his UPN and
> samaccountname is john.doe (@domain.com)
>
> Right. The new requirements, are that the new UPN's and samaccounts have
> to be ddsssiii, where dd is the city where the user works, sss is the
> department, and iii are his initials. For example, if John Doe works at
> New York and belongs to the IT staff, his UPN and samaccount has to be
> nyitjd.
>
> I am able to make a excel sheet with two columns, first with the present
> username, and second with the future username. But I need an vbscript
> which read from the first and second columns of that sheet, and change the
> present UPN and samaccountname of all user accounts in the domain to the
> new values.
>
> Any help?
>
>
>
>
> Submitted via EggHeadCafe - Software Developer Portal of Choice
> WPF GridView Sample To Insert, Update, and Delete Records
> http://www.eggheadcafe.com/tutorials/aspnet/fc9a5bf6-f5bb-4443-a92a-c9a46fd3aeb2/wpf-gridview-sample-to-in.aspx

You can use the NameTranslate object to convert sAMAccountName's into
distinguishedName's. The script could be similar to below (not tested):
=========
Option Explicit

Dim objRootDSE, strDNSDomain, objTrans, strNetBIOSDomain
Dim objExcel, strExcelPath, objSheet, intRow
Dim strOldName, strNewName, strUserDN, objUser, strUPN

' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' Specify spreadsheet.
strExcelPath = "c:\scripts\users.xls"

' Specify UPN suffix.
strUPN = "@mycompany.com"

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

' Use the NameTranslate object to find the NetBIOS domain name from the
' DNS domain name.
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
' Remove trailing backslash.
strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)

' Open spreadsheet.
Set objExcel = CreateObject("Excel.Application")
objExcel.WorkBooks.Open strExcelPath
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

' Read the spreadsheet. Start with the second row.
' Read until a blank found in column 1.
intRow = 2
Do While objSheet.Cells(intRow, 1).Value <> ""
strOldName = objSheet.Cells(intRow, 1).Value
strNewName = objSheet.Cells(intRow, 2).Value

' Use the Set method to specify the NT format of the user name.
' Trap error if user does not exist.
On Error Resume Next
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strOldName
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "User " & strOldName & " does not exist"
Else
On Error GoTo 0

' Use the Get method to retrieve the RPC 1779 Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

' Bind to the user object.
Set objUser = GetObject("LDAP://" & strUserDN)

' Change sAMAccountName and UPN.
objUser.sAMAccountName = strNewName
objUser.userPrincipalName = strNewName & strUPN

' Save changes. Trap error if sAMAccountName a duplicate.
On Error Resume Next
objUser.SetInfo
If (Err.Number <> 0) Then
Wscript.Echo "Cannot modify user " & strOldName
End If
On Error GoTo 0
End If

intRow = intRow + 1
Loop

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--