Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LDAP query syntax in VBScript

Posted on 2010-08-27
5
Medium Priority
?
1,434 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

963 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