?
Solved

LDAP query syntax in VBScript

Posted on 2010-08-27
5
Medium Priority
?
1,450 Views
Last Modified: 2013-12-24
Does anyone know the correct syntex of LDAP query in VBScript, i am using the following line in my vbscript but it is not recognising. Just to give you idea i am trying to export the memberOf group via LDAP query in vbscript and the output will go into the variable in memeory.

I am using following command:

Set oContainer=GetObject("LDAP://memberOf=cn=testgroup,OU=UK,DC=tech,DC=com")

If i remove "memberOf=cn=testgroup,OU=UK" from the above line then it works ok but i want to use perticular group in the LDAP command. Can anyone correct the above command. thanks
0
Comment
Question by:tech2010
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:ploftin
ID: 33541115
Try this code.  I found it here: http://www.tek-tips.com/viewthread.cfm?qid=1348699, but it didn't work due to a bug, which I fixed and added your group name to it.  This will simply echo each group memeber in a msgbox.
'==========================================================================
'
' NAME: RecursiveGroupMemberSearch.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: http://www.thespidersparlor.com
' DATE : 3/22/2007
' COPYRIGHT (c) 2007 All Rights Reserved
'
' COMMENT: 
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
' ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
' THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'
' IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
' BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
' DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
' WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
' ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
' OF THIS CODE OR INFORMATION.
'
'==========================================================================

gDN = SearchGroup("testgroup")

GetMembers(gDN)
Function GetMembers(gDN)
Set objGroup = GetObject("LDAP://" & gDN)
objGroup.GetInfo
arrMemberOf = objGroup.GetEx("member")

For Each strMember in arrMemberOf
Set objMember = GetObject("LDAP://" & strMember)
ObjDisp = objMember.Name
oDL = Len(ObjDisp) - 3
ObjDisp = Right(ObjDisp,oDL)
ObjCatArray = Split(objMember.objectCategory,",")
oType = ObjCatArray(0)
oTL = Len(oType) - 3
oType = Right(oType,oTL)
WScript.Echo "Member: " & vbTab & ObjDisp & vbCrLf & "Type: " & vbTab & oType
If oType = "Group" Then
GetMembers(strMember)
End If
Set objMember = Nothing
Next
End Function

Public Function SearchGroup(ByVal vSAN)
Dim oRootDSE, oConnection, oCommand, oRecordSet
Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
">;(&(objectCategory=Group)(samAccountName=" & vSAN & "));distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
SearchGroup = oRecordSet.Fields("distinguishedName")
On Error GoTo 0
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
Set oRootDSE = Nothing
End Function

Open in new window

0
 

Author Comment

by:tech2010
ID: 33541397
thanks ploftin, but i would rather use my already built script which is doing some other customized tasks.

All i wanted to check if the statement i mentioned in the post is correct, if i dont use group then it work ok.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33547769
Hi, you can't use the property name in the binding string.  The GetObject will only bind to the group itself.  You then need to use a For loop fro the groups property.

Regards,

Rob.
Set oContainer=GetObject("LDAP://cn=testgroup,OU=UK,DC=tech,DC=com")
For Each objMember In oContainter.Members
   WScript.Echo objMember.cn
Next

Open in new window

0
 

Author Comment

by:tech2010
ID: 33550757
thanks RobSmapson. I will give it a go. Is this now a complete command with For loop upto line 4 in your code. Or do i need to add any thing else?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 33551027
I think that would work as is.  I can't test it at the moment.

If you only want to output users, and not other types of objects that might be in the group, use

For Each objMember In oContainter.Members
   If objMember.Class = "user" Then WScript.Echo objMember.cn
Next


Regards,

Rob.
0

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

569 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