Solved

LDAP query syntax in VBScript

Posted on 2010-08-27
5
1,417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
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…
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…

623 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