Add custom e-mail addresses with VBS script

A collegue of mine, Alexander Carlucci, also working as an Microsoft Consultant for Ictivity, has written a VBS script to add custom e-mailaddress to Active Directory users. Let’s have a look on this script.

There are two files, addmailadres.txt and addmailadres.vbs. In the file addmailadres.txt you fill in the users with the e-mail addresses. The first collum is the SAMaccountname, the second collum is the Mailaddress. Watch the example that we are going to run….

   

addmailadres.txt:
markswinkels, admin@e2k3.nl
markswinkels, info@e2k3.nl
markswinkels, helpdesk@exchange2003.nl

addmailadres.vbs:
**Don’t forget to change the NETBIOS name in the script from e2k3 to your own domain name.
———————————————————-
‘De input van de tekstfile is comma separated
ON ERROR RESUME NEXT

Const ForReading = 1
Const ADS_PROPERTY_APPEND = 3
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objTextFile = objFSO.OpenTextFile (“.\addmailadres.txt”, ForReading)
Do Until objTextFile.AtEndOfStream
 ‘lees een regel uit de tekstfile
 strNextLine = objTextFile.Readline
   ‘zet de variabale weer op nul
 sADSPath=””
     ‘de separator is een comma
 arr = Split(strNextLine , “,”)
 arr(0)=TRIM(arr(0))
 arr(1)=TRIM(arr(1))
 ‘msgbox arr(0) & ” en ” & arr(1)
        call Writelogfile (“.\log.txt”, “TOEVOEGEN : gebruiker: ” & arr(0) & ” email: ” & arr(1) )
 ‘Zet de samaccountname om naar de Distinguishedname, dit is nodig om het mailadres te zetten
 Set WshNetwork = WScript.CreateObject(“WScript.Network”)
 sName = arr(0)
 Set oTrans = CreateObject(“NameTranslate”)
 oTrans.Init 1, “e2k3”
 oTrans.Set 3, “e2k3” & “\” & sName

 sAdsPath = oTrans.Get(1)
        ‘Voeg het mail adres toe in de AD
 msgbox “LDAP://” & sAdsPath
 Set objUser = GetObject (“LDAP://” & sAdsPath)
 objUser.PutEx ADS_PROPERTY_APPEND, “proxyAddresses”, Array(“smtp:”&Arr(1))
 ‘commit de wijziging in de AD
 objUser.SetInfo
 set objuser=nothing
        call Writelogfile (“.\log.txt”, “TOEVOEGEN : gebruiker: ” & arr(0) & ” gereed met errorcode ” & err.number & ” ” & err.description)
Loop
msgbox “klaar”

Function WriteLogFile (strLogName, strText)
‘ Deze functie probeert een entry in een logfile te schrijven. Deze logfile is door
‘ meerdere scripts te schrijven. Daartoe wordt de logfile steeds geopend en
‘ gesloten. De functie controleert of de file te openen is, zo nee dan wacht het
‘ script 500 miliseconden, waarna opnieuw geprobeert wordt het logbestand te openen.
‘ Enable error handling
On Error Resume Next
‘ Locale variabelen
 CONST ForAppending=8
  Dim objFSLog, objLogFile, strLogText, intDelayed
‘ Initialiseer het filesystemobject en de variabelen
 Set objFSLog = CreateObject(“Scripting.FileSystemObject”)
        intDelayed = -1
‘ Open de log file
    Do
        Err.Clear
        intDelayed = intDelayed + 1
            Set objLogFile = objFSLog.OpenTextFile(strLogName, ForAppending, True)
            If Err.Number <> 0 Then ‘ fout bij openen log file
                ‘ MsgBox Err.Number & ” ” & Err.Description, , “Test log B”
                WScript.Sleep (500) ‘ wacht 500 miliseconden en probeer opnieuw
            Set objLogFile = Nothing
            End If
        Loop Until Err.number = 0
‘ Heeft het script moeten wachten op de log file?
    If intDelayed <> 0 Then
        strText = strText & “, (Write to log delayed for ” & intDelayed * 500 _
                & ” milliseconds)”
    End If

‘ Maak en schrijf het log record.
        strLogText = FormatDateTime(Now(), vbShortDate) & “, ” _
                        & FormatDateTime(Now(), vbLongTime) & “, ” _
                        & WScript.ScriptName  & “, ” & strText
        objLogFile.WriteLine strLogText
‘ Sluit het logbestand
        objLogFile.Close
        Set objLogFile = Nothing
End Function
———————————————————-

Leave a Reply