VBScript: How Can I Get a List of All the Groups Whose Name Starts with G-S-Group?
- Details
- Category: Programming Guides & Tutorials
- Published on Saturday, 16 March 2013 12:32
- Written by Vinayaga Moorthy
Here’s a script that returns all the groups whose Name starts with G-S-Group:
On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
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 Name FROM 'LDAP://DC=fabrikam,DC=com' WHERE objectCategory='group' " & _
"AND Name='G-S-Group*'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
Wscript.Echo objRecordSet.Fields("Name").Value
objRecordSet.MoveNext
Loop
The part we’re interested in is the SQL query that returns just those groups that meet our criteria (names starting with G-S-Group):
"SELECT Name FROM 'LDAP://DC=fabrikam,DC=com' WHERE objectCategory='group' " & _
"AND Name='G-S-Group*'"
As you can see, we need to specify two things in the Where clause: 1) the objectCategory must be equal to group (that way, we make sure we get back only groups and not users, OUs, printers, or anything else found in Active Directory); and 2) the Name attribute must begin with the letters G-S-Group. To specify the latter, we set the search value of Name to the desired string – G-S-Group – followed by an asterisk (*), the asterisk being short for “anything.” What we’re saying here is pretty straightforward: “Show me all the groups whose name starts with G-S-Group; I don’t care what – if anything – comes after that.”
If we wanted to search for groups whose Name ended in G-S-Group we’d put the asterisk before the string:
Name='*G-S-Group'
That’s read as, “Show me all the groups whose name ends with G-S-Group; I don’t care what – if anything – comes before that.” Or we could search for groups that have the string G-S-Group somewhere (anywhere) in their name. To do that, we put asterisks before and after the string:
Name='*G-S-Group*'
Basics of VBScript can be found here
VBScript: How Can I Get a List of All the Groups Whose Name Starts with G-S-Group , vbscript active directory group ,blog comments powered by Disqus
More 
