Modify Mailbox Alias field

We recently finished a project updating the naming standard for all our Windows AD accounts. Once completed, I found that approximately 3% of the accounts had the alias field set to this old naming standard. Googled high-and-low, I could not find a simple script to set the alias field. So...

This script reads a text file of account distinguished names, then modifies the mailbox alias (aka mailnickname) field to match the SAMAccountName field. Added protection for accounts without mailboxes associated with them. Found that by setting a mailnickname, mail-enabled the account.


' Script to modify the mailbox alias of all accounts in text file to match SamAcct.
' Written by Eric Woodford
' Date 11/27/2007
'
Dim mbxAlias
dim samAcct
Dim objfso, tf, ef
Dim strUser, arUser
Dim objUser, strDN
Dim testrun

set objfso = CreateObject("Scripting.FileSystemObject")
'reads from this file
set tf = objfso.OpenTextFile("c:\useraccounts.txt",1)
' creates this log file
set ef = objfso.CreateTextFile("c:\User-Updates.log",vbtrue)

'set this boolean value to TRUE to it will not modify accounts. Good for testing results.
testrun = true

if testrun Then
ef.writeline "Starting TEST RUN at: " & Now()
Else
ef.writeline "Starting Account Conversion at: " & Now()
End If

' Loop through file
While tf.AtEndOfStream <> True
strDN= tf.readline

' cleanup values from export.
strDN = trim(replace(replace(Replace(strDN,"\\","\"),"""",""),"''","'"))
If Left(strDN,1) = "'" then strDN=Mid(strdn,2,Len(strDN))
If Right(strDN,1) = "'" then strDN = Mid(strdn,1, len(strdn)-1)

'don't bother processing if not a distinguished name.
If instr(strDN,"CN=")>0 Then
ef.writeline now() & ":" & strDN
'ef.writeline "Wanting to set to:" & arUser(1)
Set objUser = GetObject("LDAP://"&strDN)
samacct = objUser.sAMAccountName
mbxAlias = objUser.mailnickname

' skip if already set, or no mailbox associated with it
If (LCase(trim(mbxAlias))<> LCase(samacct)) And (mbxAlias<>"") Then
' If these don't match, then the account needs updating.
' a little formatting for the report.
sp = vbtab
If Len(mbalias) < 6 Then sp = sp & vbtab
If Len(mbalias) < 12 Then sp = sp & vbtab

ef.WriteLine "Alias: "&mbxAlias&sp&" should be: "& LCase(samacct)
' This means that the Alias is still set to the old account information.
If Not testrun Then
objUser.mailnickname = samacct
objUser.SetInfo
Else
ef.WriteLine "testrun - values not changed."
End If
Else
' don't bother updating as it is already correct.
ef.WriteLine "Alias correct: " & mbxAlias &sp&" is "& LCase(samacct)
End if
Set objuser=Nothing
End If
Wend

tf.Close
ef.Close

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options