Solved

Query Active Directory

Posted on 2014-03-05
17
460 Views
Last Modified: 2014-03-06
I have this little script to extract some infos from the Avtive Directory. It comes up with an error messageon Line 9 : An Invalid dn syntax has been specified.

Please help . Thanks

'On Error Resume Next
Dim objUser

searchDN   = "DC=sptyres,DC=com, DC=AU"                            
serverName = "10.24.20.25"                              
userid     = InputBox("Enter user id","User ID")    
ldapFilter = "(sAMAccountName>=" + userid + ")"

Set objuser = GetObject("LDAP://" & serverName & "/" & searchDN & ">;" & ldapFilter)

WScript.Echo "DN: " & objUser.distinguishedName
WScript.Echo ""
WScript.Echo "GENERAL"
WScript.Echo "First name: " & objUser.givenName
WScript.Echo "Initials: " & objUser.initials
WScript.Echo "Last name: " & objUser.sn
WScript.Echo "Display name: " & objUser.displayName
WScript.Echo "Description: " & objUser.description
WScript.Echo "Office: " & objUser.physicalDeliveryOfficeName
WScript.Echo "Telephone number: " & objUser.telephoneNumber
WScript.Echo "Other Telephone numbers: " & objUser.otherTelephone
WScript.Echo "Email: " & objUser.mail
WScript.Echo "Web page: " & objUser.wWWHomePage
WScript.Echo "Other Web pages: " & objUser.url
0
Comment
Question by:Bianchi928
[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
  • 8
  • 6
  • 3
17 Comments
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39907894
not sure if this is entire problem but:

ldapFilter = "(sAMAccountName>=" + userid + ")"

Concatenate strings is with "&" not "+"

So should be :
ldapFilter = "(sAMAccountName>=" & userid & ")"
0
 

Author Comment

by:Bianchi928
ID: 39907955
Nope..Still the same problem
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39907983
Looking at this line:

Set objuser = GetObject("LDAP://" & serverName & "/" & searchDN & ">;" & ldapFilter)

Open in new window


*** Updated comment

Thinking the ">" is meant to be a comma.


Set objuser = GetObject("LDAP://" & serverName & "/" & searchDN & "," & ldapFilter)

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Bianchi928
ID: 39908005
I did the change and it comes up with the following error message.

Error 0x80005000

Regards
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39908009
Can you post updated code?
0
 

Author Comment

by:Bianchi928
ID: 39908021
Sorry..don't wory about the 0x80005000.

It's still showing : An Invalid dn syntax has been specified.

'On Error Resume Next
Dim objUser

searchDN   = "DC=sptyres,DC=com, DC=AU"                            
serverName = "10.24.20.25"                              
userid     = InputBox("Enter user id","User ID")    
ldapFilter = "(sAMAccountName>=" & userid & ")"

Set objuser = GetObject("LDAP://" & serverName & "/" & searchDN & "," & ldapFilter)

WScript.Echo "DN: " & objUser.distinguishedName
WScript.Echo ""
WScript.Echo "GENERAL"
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39908164
Hi, your code is not performing a search.  The LDAP Filter you are trying to use is intended for use with the ADsDSOObject provider.  To use what you have with a GetObject call, you need only
Set objUser = GetObject("LDAP://CN=Your User,OU=SomeOU,DC=domain,DC=com")

To perform your search using the provider, you will need code like this:
Dim objUser

searchDN   = "DC=sptyres,DC=com,DC=AU"
serverName = "10.24.20.25"
userid     = InputBox("Enter user id","User ID")
ldapFilter = "(&(sAMAccountName=" + userid + "))"

strBase = "<LDAP://" & servername & "/" & searchDN & ">"
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set ADOConnection = CreateObject("ADODB.Connection")
Set rsADUsers = CreateObject("ADODB.Recordset")
ADOConnection.Provider = "ADsDSOObject"
ADOConnection.Open "Active Directory Provider"
ADOConnection.CursorLocation = 3
adoCommand.ActiveConnection = ADOConnection

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

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

' Run the query.
Set rsADUsers = adoCommand.Execute

While Not rsADUsers.EOF
	Set objuser = GetObject("LDAP://" & rsADUsers("distinguishedName"))
	
	WScript.Echo "DN: " & objUser.distinguishedName
	WScript.Echo ""
	WScript.Echo "GENERAL"
	WScript.Echo "First name: " & objUser.givenName
	WScript.Echo "Initials: " & objUser.initials
	WScript.Echo "Last name: " & objUser.sn
	WScript.Echo "Display name: " & objUser.displayName
	WScript.Echo "Description: " & objUser.description
	WScript.Echo "Office: " & objUser.physicalDeliveryOfficeName
	WScript.Echo "Telephone number: " & objUser.telephoneNumber
	WScript.Echo "Other Telephone numbers: " & objUser.otherTelephone
	WScript.Echo "Email: " & objUser.mail
	WScript.Echo "Web page: " & objUser.wWWHomePage
	WScript.Echo "Other Web pages: " & objUser.url
	
	rsADUsers.MoveNext
Wend

Open in new window


Regards,

Rob.
0
 

Author Comment

by:Bianchi928
ID: 39908199
Hi Rob,

Thanks for helping

I've modified mine and I'm gettign an error

There is no such object on the server. I'm not too familiar with LDAP. I might have the wrong info for CN , OU

The userid is cis0794 and the domain is sptyres.com.au

I'll talk to you about using a provider later.


'On Error Resume Next
Dim objUser

'searchDN   = "DC=sptyres,DC=com, DC=AU"                            
'serverName = "10.24.20.25"                              
'userid     = InputBox("Enter user id","User ID")    
'ldapFilter = "(sAMAccountName>=" & userid & ")"

Set objUser = GetObject("LDAP://CN=cis0794,OU=users,DC=sptyres,DC=com,DC=au")

WScript.Echo "DN: " & objUser.distinguishedName
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39908233
If it's the default Users container, it will need to be CN=Users, instead of OU=Users,

Rob.
0
 

Author Comment

by:Bianchi928
ID: 39908242
I'm a bit lost here. Where do I pass in the userid parameter. I only want to get the details for  a specific user
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39908253
OK, so from your original script, all you should need is this
Dim objUser

Set objuser = GetObject("LDAP://CN=cis0794,CN=users,DC=sptyres,DC=com,DC=au")

WScript.Echo "DN: " & objUser.distinguishedName
WScript.Echo ""
WScript.Echo "GENERAL"
WScript.Echo "First name: " & objUser.givenName
WScript.Echo "Initials: " & objUser.initials
WScript.Echo "Last name: " & objUser.sn
WScript.Echo "Display name: " & objUser.displayName
WScript.Echo "Description: " & objUser.description
WScript.Echo "Office: " & objUser.physicalDeliveryOfficeName
WScript.Echo "Telephone number: " & objUser.telephoneNumber
WScript.Echo "Other Telephone numbers: " & objUser.otherTelephone
WScript.Echo "Email: " & objUser.mail
WScript.Echo "Web page: " & objUser.wWWHomePage
WScript.Echo "Other Web pages: " & objUser.url 

Open in new window


Regards,

Rob.
0
 

Author Comment

by:Bianchi928
ID: 39908263
OPkay..I tested tthis last script from you and I'm getting

There is no such object on the server on LIne 3
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39908347
OK, so we can get the distinguished name of the user by logging in as that user, then running this code:
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
WScript.Echo objUser.distinguishedName

Open in new window


Then you can plug that distringuished name into the main script, and it should bind to the object.

Regards,

Rob.
0
 

Author Comment

by:Bianchi928
ID: 39908376
Okay..we're getting there .. I have modified the script to down below and it works fine.
But If I want to prompt a user name or id , how do I go about it . Because I might have to run it for any selected user from my computer.

Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
WScript.Echo objUser.distinguishedName

Dim objUser

Set objuser = GetObject("LDAP://CN=Christian Requin,OU=user,OU=Windows 7,DC=sptyres,DC=com,DC=au")

WScript.Echo "DN: " & objUser.distinguishedName
WScript.Echo ""
WScript.Echo "GENERAL"
WScript.Echo "First name: " & objUser.givenName
WScript.Echo "Initials: " & objUser.initials
WScript.Echo "Last name: " & objUser.sn
WScript.Echo "Display name: " & objUser.displayName
WScript.Echo "Description: " & objUser.description
WScript.Echo "Office: " & objUser.physicalDeliveryOfficeName
WScript.Echo "Telephone number: " & objUser.telephoneNumber
WScript.Echo "Other Telephone numbers: " & objUser.otherTelephone
WScript.Echo "Email: " & objUser.mail
WScript.Echo "Web page: " & objUser.wWWHomePage
WScript.Echo "Other Web pages: " & objUser.url
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 200 total points
ID: 39908408
OK great, so now you can see how the distinguished name needs to be passed.  However, for searching, you need to use the provider I spoke of earlier, so this code should do that for you.

Dim objUser

searchDN   = "DC=sptyres,DC=com,DC=au"
userid     = InputBox("Enter user id","User ID")
ldapFilter = "(&(sAMAccountName=" & userid & "))"

strBase = "<LDAP://" & searchDN & ">"
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set ADOConnection = CreateObject("ADODB.Connection")
Set rsADUsers = CreateObject("ADODB.Recordset")
ADOConnection.Provider = "ADsDSOObject"
ADOConnection.Open "Active Directory Provider"
ADOConnection.CursorLocation = 3
adoCommand.ActiveConnection = ADOConnection

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

' Construct the LDAP syntax query.
strQuery = strBase & ";" & ldapFilter & ";" & strAttributes & ";subtree"
WScript.Echo strQuery
adoCommand.CommandText = strQuery
' Define the maximum records to return
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set rsADUsers = adoCommand.Execute

While Not rsADUsers.EOF
	Set objuser = GetObject("LDAP://" & rsADUsers("distinguishedName"))
	
	WScript.Echo "DN: " & objUser.distinguishedName
	WScript.Echo ""
	WScript.Echo "GENERAL"
	WScript.Echo "First name: " & objUser.givenName
	WScript.Echo "Initials: " & objUser.initials
	WScript.Echo "Last name: " & objUser.sn
	WScript.Echo "Display name: " & objUser.displayName
	WScript.Echo "Description: " & objUser.description
	WScript.Echo "Office: " & objUser.physicalDeliveryOfficeName
	WScript.Echo "Telephone number: " & objUser.telephoneNumber
	WScript.Echo "Other Telephone numbers: " & objUser.otherTelephone
	WScript.Echo "Email: " & objUser.mail
	WScript.Echo "Web page: " & objUser.wWWHomePage
	WScript.Echo "Other Web pages: " & objUser.url
	
	rsADUsers.MoveNext
Wend

Open in new window


Note that I have removed the server specification, so we're using a serverless bind.

Regards,

Rob.
0
 

Author Closing Comment

by:Bianchi928
ID: 39908886
Perfect Rob. It all makes sense now. Thanks a lot
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39909027
No problem. Thanks for the grade.
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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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