Solved

LDAP query syntax in VBScript

Posted on 2010-08-27
5
1,409 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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

840 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