• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

Script to remove multiple users from a secutiry group

Hi Experts,

I have a script at the moment that adds multiple users in to a particular security group, I was wondering if there was a way to do the same but in reverse, and thus removing multiple active directory users from a security group?

I have included a snippet of the code we use to add multiple users to a security group for your reference, to see what we do and how we do it.

Kindest Regards
Option Explicit
Dim objRootLDAP, objGroup, objUser, objOU
Dim strOU, strOU2, strGroup, strDNSDomain
Dim intCounter

strOU = "OU=Users,OU=01-Bournemouth,OU=Stores,OU=Beales Users,"
strOU2 = "OU=Security Groups,OU=Beales Groups,"
strGroup = "CN=Remote Users,"

Set objRootLDAP = GetObject("LDAP://RootDSE")
strDNSDomain = objRootLDAP.Get("DefaultNamingContext")

Set objGroup = GetObject("LDAP://"& strGroup _
& strOU2 & 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

Open in new window

0
Flight5497
Asked:
Flight5497
  • 2
2 Solutions
 
RobSampsonCommented:
You can just change
objGroup.add(objUser.ADsPath)

to
objGroup.remove(objUser.ADsPath)

Regards,

Rob.
0
 
Krzysztof PytkoActive Directory EngineerCommented:
I would rather use for that Microsoft DS Tools or Quest PowerShell module for AD. It's much easier in use if you're not a VBScript expert ;)
If you're interested, please let me knwo. I will prepare a syntax for you :)

Regards,
Krzysztof
0
 
Flight5497Author Commented:
Ah that is brilliant mate, thank you very much it was that simple :P now I feel suitably silly :)

I would also like to ask while I am here another problem I had with the above script just a second ago is that when I tell it to add all users in the OU to a group and it finds a user that is already in the group it falls over and does not carry on with the users that are not in the group.

Is there someway of amending it to allow for this?
0
 
RobSampsonCommented:
Sure.  Just change this:
      objGroup.add(objUser.ADsPath)

Open in new window


to this:
      On Error Resume Next
      objGroup.add(objUser.ADsPath)
      If Err.Number = 0 Then
            WScript.Echo "User added successfully."
      Else
            WScript.Echo "User was not added. Error " & Err.Number & ": " & Err.Description
      End If
      Err.Clear
      On Error GoTo 0

Open in new window


You can comment out the WScript.Echo statements if you don't them to display.

Regards,

Rob.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now