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
———————————————————-

7 Responses to “Add custom e-mail addresses with VBS script”

  1. 学校要求每周写总结,烦啊、累啊,碰到这种事不要急,【计划汇总】功能来帮您,OA可以提供汇总功能将每月的计划汇总成年计划,使您轻松自得。

  2. Hiya, I was reading another thing about this on another blog. Interesting. Your perspective on it is diametrically contradicted from what I read earlier. I’m still pondering with the opposite points of view, but I’m leaning to a great extent toward yours. And irrespective, that’s what is so perfect about modern-day democracy and the marketplace of ideas online.

  3. Hi there, I discovered this blog once, then lost it. Took me forever to come back and find it. I wanted to view what comments you got. Very good blog by the way.

  4. Coty says:

    You ever think of switching hosts? Maybe go with hostgator or something? Maybe it’s just me but it seems like your site speed is going down the pooper the past few days ;\

  5. You have some very thought provoking articles and view points, although I dont totally agree with everything in the article its still a very interesting view, look forward to coming back again soon. Have bookmarked you for future reference.

  6. Awesome post man. I was wondering from where do you get all this stuff!!

  7. hey there, this might be little offtopic, but i am hosting my site on hostgator and they will suspend my hosting in 4days, so i would like to ask you which hosting do you use or recommend?

Leave a Reply