Query Domain Controller

jaypappas
jaypappas used Ask the Experts™
on
On Error Resume Next
 
Set objGroup = GetObject _ 
  ("LDAP://CN=sharepoint users ,cn = users,DC=vancesecurity,DC=com")


objGroup.GetInfo
 
arrMemberOf = objGroup.GetEx("member")
 
WScript.Echo "Members:"



For Each strMember in arrMemberOf

  WScript.echo strMember

Next

Open in new window


Here is the Canonical Name
"vancesecurity.com/Centreville-Office/Information Technology/Sharepoint Users"

Attached is a sample screen shot of a member in this group

How do I change the
 ("LDAP://CN=sharepoint users ,cn = users,DC=vancesecurity,DC=com")

to query for members
sample-user.docx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The Object tab of the Sharepoint Users group should reveal the DSN for the group.
Top Expert 2011
Commented:
You can try using adsiedit to get the path.

Following script does not require the path to be mentioned, you can try
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on user objects.
strFilter = "(&(objectCategory=group)(cn=sharepoint users))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedname"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
    ' Retrieve values and display.
    strDN = adoRecordset.Fields("distinguishedname").Value
    Set objGroup = GetObject  ("LDAP://"&strDN)
    
    objGroup.GetInfo
    
    arrMemberOf = objGroup.GetEx("member")
    
    WScript.Echo "Members:"
    
    For Each strMember In arrMemberOf
        
        WScript.echo strMember
        
    Next
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close

Open in new window

Top Expert 2011

Commented:
Missed something...try the following
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on user objects.
strFilter = "(&(objectCategory=group)(cn=sharepoint users))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedname"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
    ' Retrieve values and display.
    strDN = adoRecordset.Fields("distinguishedname").Value
    Set objGroup = GetObject  ("LDAP://"&strDN)
    
    objGroup.GetInfo
    
    arrMemberOf = objGroup.GetEx("member")
    
    WScript.Echo "Members:"
    
    For Each strMember In arrMemberOf
        
        WScript.echo strMember
        
    Next
    adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close

Open in new window

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Commented:
You start at the right and go left to get the FQDN.

"vancesecurity.com/Centreville-Office/Information Technology/Sharepoint Users"

Would turn into:

"LDAP://CN=Sharepoint Users,OU=Information Technology,OU=Centreville-Office,DC=vancesecurity,DC=com"

Author

Commented:

Open in new window


Open in new window


Open in new window

Now how to add a memeber named "test" to the group by changing this

objGroup.PutEx ADS_PROPERTY_APPEND, "member", _
    Array("cn=test,cn=sharepoint,DC=andrewsinternational,DC=com")



On Error Resume Next
 
Set objGroup = GetObject _ 
  ("LDAP://CN=sharepoint_Users,ou = sharepoint,DC=andrewsinternational,DC=com")


objGroup.GetInfo
 
arrMemberOf = objGroup.GetEx("member")
 
WScript.Echo "Members:"
x=0


For Each strMember in arrMemberOf
  WScript.echo strMember
   
   
   Next

 
objGroup.PutEx ADS_PROPERTY_APPEND, "member", _
    Array("cn=test,cn=sharepoint,DC=andrewsinternational,DC=com")
 
objGroup.SetInfo

Open in new window


-----------------------------------------------------------
Now how to add a memeber named "test" to the group by changing this

objGroup.PutEx ADS_PROPERTY_APPEND, "member", _
    Array("CN=Sharepoint Users,OU=Information Technology,OU=Centreville-Office,DC=vancesecurity,DC=com

")


Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN


Set fso = CreateObject("Scripting.FileSystemObject") 
'pointing to default folder for DTS files
Set folderObject = fso.GetFolder("n:\WORKFLOW\")

Set eFile1 = folderObject.CreateTextFile("LOG.txt",true,false) 



' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on user objects.
strFilter = "(&(objectCategory=group)(cn=sharepoint users))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedname"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

msgbox(strDN)


' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
    ' Retrieve values and display.
    strDN = adoRecordset.Fields("distinguishedname").Value
	
	
    Set objGroup = GetObject  ("LDAP://"&strDN)
    
    objGroup.GetInfo
    
    arrMemberOf = objGroup.GetEx("member")
    
    WScript.Echo "Members:"
    
    For Each strMember In arrMemberOf
        
       ' WScript.echo strMember
        
    Next
    adoRecordset.MoveNext
Loop

objGroup.PutEx ADS_PROPERTY_APPEND, "member", _
    Array("CN=Sharepoint Users,OU=Information Technology,OU=Centreville-Office,DC=vancesecurity,DC=com")
objGroup.SetInfo	
	



' Clean up.
adoRecordset.Close
adoConnection.Close 

Open in new window

Commented:
I'm sorry, can you clarify what you would like please?  I don't quite understand.
Top Expert 2011

Commented:
Try the following
dim groupPath
dim userPath

groupPath = "LDAP://CN=sharepoint_Users,ou = sharepoint,DC=andrewsinternational,DC=com"
userPath = "cn=test,cn=sharepoint,DC=andrewsinternational,DC=com"

addToGroup userPath,groupPath

sub addToGroup(userPath, groupPath)
	dim objGroup
	set objGroup = getobject(groupPath)
	
	for each member in objGroup.members
		if lcase(member.adspath) = lcase(userPath) then
			exit sub
		end if
	next
	objGroup.Add(userPath)

end sub

Open in new window

Top Expert 2011

Commented:
Missed LDAP for user
dim groupPath
dim userPath

groupPath = "LDAP://CN=sharepoint_Users,ou = sharepoint,DC=andrewsinternational,DC=com"
userPath = "LDAP://cn=test,cn=sharepoint,DC=andrewsinternational,DC=com"

addToGroup userPath,groupPath

sub addToGroup(userPath, groupPath)
	dim objGroup
	set objGroup = getobject(groupPath)
	
	for each member in objGroup.members
		if lcase(member.adspath) = lcase(userPath) then
			exit sub
		end if
	next
	objGroup.Add(userPath)

end sub

Open in new window

Top Expert 2011

Commented:
It would be good if you specify all requirements..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial