Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need all users with emails addresses to be put in global group. 1

Status
Not open for further replies.
Feb 11, 2005
153
0
0
US
Okay I have a dilema. We have a global distribution group we created for exchange emailing. The problem is from time to time people create an ID without remembering to put them into the group in question.

I was thinking to modify a script I collaborated on creating (someone from here helped out on that one as well) for a GAL dump to make it so that when the VBS is ran all of the users this script finds will be added to the correct global group. Can anyone help me out?

Code:
Dim rootDSE, domain

Set rootDSE = GetObject("LDAP://RootDSE")

Set domain = GetObject("LDAP://" & rootDSE.Get("defaultNamingContext"))

Set fso = CreateObject("Scripting.FileSystemObject")
Set outfile = fso.CreateTextFile("Gal Export.csv")

Call EnumUsers(domain.ADsPath)
Call EnumContacts(domain.ADsPath)
Call EnumOUs(domain.ADsPath)

Sub EnumOUs(adspath)
    Dim container, ou

    Set container = GetObject(adspath)

    container.Filter = Array("OrganizationalUnit")

    Call EnumUsers(container.ADsPath)
    Call EnumContacts(container.ADsPath)

    For Each ou In container

        Call EnumOUs(ou.ADsPath)

    Next


End Sub


Sub EnumUsers(adspath)
    Dim container, user

    Set container = GetObject(adspath)

    container.Filter = Array("User")

    For Each user In container

        If user.Mail <> "" and user.sn <> "" and user.givenName <> "" Then

            outfile.WriteLine user.sn & ", " & user.givenName & ", " & user.Mail

        End If

    Next


End Sub
Sub EnumContacts(adspath)
    Dim container, contact

    Set container = GetObject(adspath)

    container.Filter = Array("Contact")

    For Each contact In container

        If contact.Mail <> "" and contact.sn <> "" and contact.givenName <> "" Then

            outfile.WriteLine contact.sn & ", " & contact.givenName & ", " & contact.Mail

        End If

    Next

End Sub

I was thinking instead of doing an outfile in each section I then add the users it parses out to to the distribution group. Would that be a simple as changing the outfile section to something like objGroup.Add ? -

Maybe put this at the beginning of the script - Set objGroup = GetObject("LDAP://yadda yadda")
then in each section do an objGroup.Add(objUser.AdsPath)

Is this too hard with the current script and possibly I should start over?

What we want is every user/contact in our AD system that has an email account listed in the e-mail field to be in this distribution group.
 
You already know what you need to do. First bind to the Global group as objGroup then instead of adding a user to the report, use objGroup.Add objUser.ADSPath

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Thanks for letting me know. I thought this was the way but I don't want to "test" the script on a live environment.
 
One big question....

The seond one is a contact parse would I instead change

objGroup.Add objUser.ADSPath to objGroup.Add contact.ADSPath?
 
As I thought the script stops at objGroup.Add objUser.ADSPath when I replace the first outfile.WriteLine user.sn & ", " & user.givenName & ", " & user.Mail line. I think this has to do with the fact I am arrying the information and have not set an objUser?

Is there any command to work with objGroup.Add that iwll work without a hard set command? Or is there a way to make each item that meets the search criteria be a set and then loop or something?
 
In your script where you query the container, you use user instead of objUser

objGroup.Add user.ADSPath

Also have you used the Set command to specify the objGroup to add them to? Post your modofied code for additional help.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Yes I did set the container and the OU within AD via an LDAP connection. I think I need contact in there as well so I made this change as well but the script isn't getting to the contacts portion yet. It is erroring on line 45 saying this object already exists is this because if a user is already in this group its making en error? Do I need to put in some kind of check to see if the users are in the group first? I ask this only because like I mentioned at the begining some will be in and some won't so I need to add some but others will be there.

Below is the new code -

Code:
Dim rootDSE, domain

Set rootDSE = GetObject("LDAP://RootDSE")

Set domain = GetObject("LDAP://" & rootDSE.Get("defaultNamingContext"))
Set fso = CreateObject("Scripting.FileSystemObject")
Set objGroup = GetObject("LDAP://cn=All Staff,ou=Metro,dc=domain,dc=com") 


Call EnumUsers(domain.ADsPath)
Call EnumContacts(domain.ADsPath)
Call EnumOUs(domain.ADsPath)

Sub EnumOUs(adspath)
    Dim container, ou

    Set container = GetObject(adspath)

    container.Filter = Array("OrganizationalUnit")

    Call EnumUsers(container.ADsPath)
    Call EnumContacts(container.ADsPath)

    For Each ou In container

        Call EnumOUs(ou.ADsPath)

    Next


End Sub


Sub EnumUsers(adspath)
    Dim container, user

    Set container = GetObject(adspath)

    container.Filter = Array("User")

    For Each user In container

        If user.Mail <> "" and user.sn <> "" and user.givenName <> "" Then

            objGroup.Add(user.ADSPath)

        End If

    Next


End Sub
Sub EnumContacts(adspath)
    Dim container, contact

    Set container = GetObject(adspath)

    container.Filter = Array("Contact")

    For Each contact In container

        If contact.Mail <> "" and contact.sn <> "" and contact.givenName <> "" Then

            objGroup.Add(contact.ADSPath)

        End If

    Next

End Sub
 
You will get an error if the user is already a member of the group. Use On Error Resume Next to allow it to continue to process names.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Adding that to the beginning of the script gets rid of the error but nothing imports into the Distribution group.
 
If I change objGroup.Add user.Adspath to wscript.echo user.Adspath and objGroup.Add contact.Adspath to wscript.echo contact.Adspath it shows each user/contact in LDAP form.

LDAP://cn=User,ou=whichever OU the user is in,dc=domain,dc=com

I just am not sure why its not going into the objGroup.Add. Could it be because this is a distribution group and not a security group?
 
That shows you are binding to the user OK.

Try this one simeple change. I notice you are using the user adspath in parenthesis. Remove the parens and put a space after objGroup.Add.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Yep already tried that change but the results of it as objGroup.Add user.Adspath or objGroup.Add(user.Adspath) are the same as well as wscript.echo subsituted in both cases.

I get tyhe echo out I just don't get the group addition.

This is being ran from a domain admin account so I know this isn't permissions related.
 
Can it be that LDAP doesn't like the users that we have that have spaces? We use first<space>last and sometimes these spaces can cause problems but the question becomes why would it output to screen correctly but not put them in?
 
Mannnnn I thought we had something - thank you for teaching me the Chr(34) trick though I had another script where it didn't like the "'s next to each other and htis may have resolved it for me I'll have to play around with the other script again.

Needless to say the echo command shows the "'s but the objGroup.Add didn't add them.

Now I did do another thing yesterday I did a csvde and verified the LDAP of the Distribution group but that too is spot on.

 
Go back to basics.

Write a 3 line script to add one user to the group to verify it should work in your environment.

Set objUser = GetObject("LDAP://cn=username,ou=ouname,dc=company,dc=local")
Set objGroup = GetObject("LDAP://cn=groupname,ou=ouname,dc=company,dc=local")
objGroup.add(objUser.ADsPath)


I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
I think I may have it... I just ran this script -

Code:
On Error Resume Next
Dim rootDSE, domain

Set rootDSE = GetObject("LDAP://RootDSE")

Set domain = GetObject("LDAP://" & rootDSE.Get("defaultNamingContext"))
Set objGroup = GetObject("LDAP://cn=All Staff,ou=Metro,dc=domain,dc=com")


Call EnumUsers(domain.ADsPath)
Call EnumContacts(domain.ADsPath)
Call EnumOUs(domain.ADsPath)

Sub EnumOUs(adspath)
    Dim container, ou

    Set container = GetObject(adspath)

    container.Filter = Array("OrganizationalUnit")

    Call EnumUsers(container.ADsPath)
    Call EnumContacts(container.ADsPath)

    For Each ou In container

        Call EnumOUs(ou.ADsPath)

    Next


End Sub


Sub EnumUsers(adspath)
    Dim container, user

    Set container = GetObject(adspath)

    container.Filter = Array("User")

    For Each user In container

        If user.Mail <> "" Then

            objGroup.Add user.Adspath
            
        End If

    Next


End Sub
Sub EnumContacts(adspath)
    Dim container, contact

    Set container = GetObject(adspath)

    container.Filter = Array("Contact")

    For Each contact In container

        If contact.Mail <> "" Then

            objGroup.Add contact.Adspath
            
        End If

    Next

End Sub

And it ran fine to a brand new global group. (I renamed the global group that has some users in it.)

Can it be that this on error resume next has to go into the sub sections? Since it calls them do they not know about the on error resume next of the main part of the script?
 
Ahha that was it!

I didn't realize you start fresh with a call and need to reiterate the on error resume next in the subs as well.

Now its working like a charm.
 
HI All,,

I have a similar problem like these, do we have a script that automatically adds new users in add to a security group? Please Share. Thanks

Solec
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top