Announcement

Collapse
No announcement yet.

How I can Add Lots of Users to a Group using bat file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How I can Add Lots of Users to a Group using bat file

    Hi, All!
    I need some help. My goal is add all user that belong to Test OU, for example, to Tech global security group in same OU. I found pretty good solution in scripting guys site:

    Code:
     
    Option Explicit 
    Dim objRootLDAP, objGroup, objUser, objOU
    Dim strOU, strGroup, strDNSDomain
    Dim intCounter
     
    ' Check these objects referenced by strOU, strGroup exist in strOU
    strOU = "Test,"
    strGroup = "CN=Tech,"
     
    Set objRootLDAP = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootLDAP.Get("DefaultNamingContext")
     
    Set objGroup = GetObject("LDAP://"& strGroup _
    & strOU & strDNSDomain)
    Set objOU =GetObject("LDAP://" & strOU & strDNSDomain)
     
    intCounter = 1
    For Each objUser In objOU
       If objUser.Class = lcase("User") then
          objGroup.add(objUser.ADsPath)
          intCounter = intcounter +1
       End If
    Next
    WScript.Echo strGroup & " has " & intCounter & " new members"
     
    Wscript.Quit
    It works well. This example add all user from Test OU (this is top level OU) to Tech security group in same OU. If we need to bind an OU that not top level OU, we must change strOU accordingly:
    Code:
     
    strOU = "OU=Test,OU=Departments,"
    In this case Test OU reside under Departments OU. Apropos value strGroup, we must change it if group, where we want add users to, reside in other location. For example:

    Code:
    strGroup = "CN=Tech,OU=Test,OU=Departments,"
    ̉he question is: do we can perform same staff with *.bat file? Yes, we can use command like this:

    Code:
    dsquery group "OU=Test,DC=litware,DC=com"  -name Tec*  | dsmod group -addmbr  "CN= GivenName SurName,OU=Test,DC=litware,DC=com"
    but in this case we need to create same command for each user in OU. I mean some method that bind all the objects, which match a simple criteria, namely they are User objects and not computers or contacts etc.

    Thank you in advance,
    Igor.

  • #2
    Re: How I can Add Lots of Users to a Group using bat file

    Does this batch do what you are looking for?

    Code:
    :: All the userobjects found in one OU (onelevel only), are 
    :: being grouped in an existing security Group
    ::
    :: important, I used the switch -chmbr
    :: Do CHANGE the switch if you don't 
    :: want to empty the group before adding the new members
    
    @echo off
    
    Set "strOU=OU=Test,DC=litware,DC=com"
    
    dsquery user "%strOU%" -scope onelevel | dsmod group "cn=name of existing group,%strOU%" -chmbr -c
    \Rems
    Last edited by Rems; 14th September 2008, 07:29. Reason: ADDED: -scope onelevel

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: How I can Add Lots of Users to a Group using bat file

      Thanks, Rems!
      As always,- you are the best!!!
      It should work, I will check it later...
      One question:
      In you're example group, where I need add user, reside in same OU. What I need to change if this group located in some other OU?
      Last edited by igor7; 13th September 2008, 16:07.

      Comment


      • #4
        Re: How I can Add Lots of Users to a Group using bat file

        Originally posted by igor7 View Post
        One question:
        In you're example group, where I need add user, reside in same OU. What I need to change if this group located in some other OU?

        Code:
        :: All the userobjects found in one OU (onelevel only), are 
        :: being grouped in an existing security Group
        ::
        :: important, I used the switch -chmbr
        :: Do CHANGE the switch if you don't 
        :: want to empty the group before adding the new members
        
        @echo off
        
        Set "UsersOUstr=OU=Test,DC=litware,DC=com"
        Set "GroupOUstr=OU=Test Groups,DC=litware,DC=com"
        
        dsquery user "%UsersOUstr%" -scope onelevel | dsmod group "cn=name of existing group,%GroupOUstr%" -chmbr -c
        \Rems


        Similar Solutions,
        -VBS: Alternative Query-Based_Distribution_Group
        -
        Last edited by Rems; 15th September 2008, 09:54. Reason: ADDED: -scope onelevel

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: How I can Add Lots of Users to a Group using bat file

          Thanks, Rems!!
          This is exactly what I need!! It works perfect!

          Comment


          • #6
            Re: How I can Add Lots of Users to a Group using bat file

            Glad I could help Igor.

            Note, I have changed the scope of the query.
            The default value is Subtree, it wil search in subOUs too. With a scope set to Onelevel there will only be a search for objects in the provided OU, not in the OUs that might be there lower in the tree.


            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment

            Working...
            X