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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with ADSI (I think) syntax for groups with no members 1

Status
Not open for further replies.

Jerz

MIS
Sep 10, 2004
102
US
I need to dump the names of all groups in my AD that have no members into a TXT file. I have looked for a few days now, certain that somebody had already done this, but no joy on the plagurism attempt. Close, but not what I'm looking for. Here's something close:
Code:
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 ADsPath, Name FROM 'LDAP://dc=thadmin,dc=com' WHERE objectCategory='group'" 

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    Set objGroup = GetObject(objRecordSet.Fields("ADsPath").Value)

    i = 0

    For Each strUser in objGroup.Member
        i = i + 1
        If i > 1 Then
            Exit For
        End If
    Next
    If i <= 1 Then
        Wscript.Echo objRecordSet.Fields("Name").Value & " -- " & i
    End If
    objRecordSet.MoveNext
Loop
This code is SUPPOSED to list groups with 1 or 0 members. But it lists all groups, claiming they all have one member. That's not what I'm looking for anyhow.

I thought I saw once where sombody used some NOT (!Members=*) qualifier on the query command, but the following does not work for me.
Code:
'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 ADsPath, Name FROM 'LDAP://dc=thadmin,dc=com' WHERE (objectCategory='group')(!members=*)" 

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields("Name").Value
    objRecordSet.MoveNext
Loop
this returns 80040e14 error from provder at the 'Set objRecordSet = objCommand.Execute' line.

Any syntax hints, or completely different ides to get the same place will be welcomed.

Thanks,
David J.



 
Would something like this help?

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Excellent. Plagiarism RULES!

All I had to add was the 'if membercount=0' loop, and replace the left & right quotes with regular quotes.

Code:
strNamingContext = "defaultNamingContext"

strConnectionString = "Provider=ADsDSOObject"
strFilter= "ObjectCategory='group'"

'[Functions] ****************************************

Function removeCN(inputString)
  Dim textValue 
     textValue = inputString
 Dim textLength
     textLength = Len(textValue)
   textValue = Right(textValue, textLength-3)

      removeCN = textValue   
End Function
Set objAdRootDSE = GetObject("LDAP://RootDSE")
Set objRS = CreateObject("ADODB.RecordSet")
objNamingContext = objAdRootDSE.Get(strNamingContext)
strSQLQuery = "SELECT * FROM 'LDAP://" &_
         objNamingContext &_
         "' WHERE " & strFilter &_
         "ORDER BY NAME"

objRS.Open strSQLQuery, strConnectionString

On Error Resume Next

Do until objRS.eof
 Set objGroup = GetObject(objRS.Fields.Item(0))
 strGroup = removeCN(objGroup.Name)

        membercounter = 0

 For each oMember in objGroup.Members

  memberCounter = membercounter + 1
 Next
 If membercounter = 0 then 
  WScript.Echo(strGroup)
 End If
 Set memberCounter = nothing
objRS.MoveNext

Loop

Star for you!

Thanks,
David J.
 
Glad I could help, thanks for the star [smile]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top