Skip navigation

How can I use a script to add email addresses to a distribution group in Active Directory (AD)?

A. The following script, which you can download here , will add email addresses to a distribution group. In the script, the first LDAP path is the location where the contacts will be created; you need to change it to a valid path for your environment. The second LDAP path is the distinguished name (DN) of the distribution group to which the contact will be added.


' Check whether all required arguments have been passed.
If Wscript.Arguments.Count  required. For example:" & vbCrLf _
& "cscript createcontact.vbs [email protected]"
Wscript.Quit(0)
End If

strContactToAdd = Wscript.Arguments(0)

Set containerObj = GetObject("LDAP://ou=Contacts,dc=dom,dc=com")
If Err  0 Then 
WScript.Echo "Error " & Hex(Err)
End If 
Err.Clear

Set grpObj = GetObject("LDAP://cn=HRMailing,dc=dom,dc=com") 
If Err  0 Then 
WScript.Echo "Error " & Hex(Err)
End If 
Err.Clear


Set contactObj = containerObj.Create("contact","cn=" & strContactToAdd) 
If Err  0 Then 
WScript.Echo "Error " & Hex(Err)
End If 
Err.Clear

contactObj.mail = strContactToAdd
contactObj.mailNickName = strContactToAdd
contactObj.SetInfo 

grpObj.Add(contactObj.AdsPath)
grpObj.SetInfo

Run the script by using the following command:

D:\Temp>cscript createcontact.vbs [email protected]
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish