Solved

Create a query based group of all users except certain members

Posted on 2008-10-07
1
411 Views
Last Modified: 2012-05-05
I have a need to create a group that contains all users in AD except 7 users.  Does anyone know a way to acheive this end?
0
Comment
Question by:jcistaro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 22661269
Paste the script below into a text file with a .vbs extension.  Customize the values of the variables and dictionary entries above the horizontal line as indicated in the comments.  Running the script will create a new group containing all users except the excluded ones.


On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
Const ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP = 4
Const ADS_GROUP_TYPE_GLOBAL_GROUP = 2
Const ADS_GROUP_TYPE_UNIVERSAL_GROUP = 8
Const ADS_GROUP_TYPE_SECURITY_ENABLED = -2147483648
 
'Distinguished name of your domain
strContainer = "dc=yourdomain,dc=local"
 
'Name of the group to create
strGroup = "new group"
 
'Distinuished name of the OU to create the new group in
strGroupOU = "ou=security groups,dc=yourdomain,dc=local"
 
'Type of group to create
strGroupType = ADS_GROUP_TYPE_GLOBAL_GROUP Or ADS_GROUP_TYPE_SECURITY_ENABLED
 
'Description of the new group
strGroupDesc = "My new group"
 
Set objExclude = CreateObject("Scripting.Dictionary")
objExclude.CompareMode = VBTextCompare
'Customize each entry with the SAM ID of a user to exclude from the query
objExclude.Add "user1", ""
objExclude.Add "user2", ""
objExclude.Add "user3", ""
objExclude.Add "user4", ""
objExclude.Add "user5", ""
objExclude.Add "user6", ""
objExclude.Add "user7", ""
 
'--------------------------------------------------------------------------------------
 
set objGroupOU = GetObject("LDAP://" & strGroupOU)
set objGroup = objGroupOU.Create("group","cn=" & strGroup)
objGroup.Put "groupType", strGroupType
objGroup.Put "samAccountName", strGroup
objGroup.Put "description", strGroupDesc
objGroup.SetInfo
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
objCommand.CommandText = _
    "SELECT AdsPath FROM 'LDAP://" & strContainer & "' WHERE objectCategory='user'"  
Set objRecordSet = objCommand.Execute
 
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    Set objUser = GetObject(objRecordSet.Fields("AdsPath").Value)
    
    If Not objExclude.Exists(objUser.sAMAccountName) Then
        objGroup.Add(objUser.ADsPath)
    End If
    objRecordSet.MoveNext
Loop

Open in new window

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trasfering FSMO roles 8 113
server DNS address could not be found 22 345
SBS 2003 RWW Login 3 58
Access denied running PowerPivot -SQL Server 2014 on Windows Server 2012 10 64
The HP utility "HP Lights-Out Online Configuration Utility for Windows Server 2003/2008" could be of great use when it comes to remotely configure a HP servers ILO WITHOUT rebooting the server. We would only need to create and run scripts using thi…
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question