Solved

Getting/Filtering on departmentNumber in AD query in vbscript

Posted on 2012-04-12
8
1,795 Views
Last Modified: 2012-04-13
The following code appears to work for theAD attributes I am requesting, except for departmentNumber:

Response.Write objRS("name")

My query is:

Set objDomain = GetObject ("GC://RootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.provider ="ADsDSOObject"
objConn.Properties("User ID") = "xxxxxx" 'domain account with read access to LDAP
objConn.Properties("Password") = "xxxxxx" 'domain account password
objConn.Properties("Encrypt Password") = True
objConn.open "Active Directory Provider"
Set objCom = CreateObject("ADODB.Command")
Set objCom.ActiveConnection = objConn
objCom.CommandText ="select name,telephonenumber,mobile,mail,company,title,department,sAMAccountName,sn,userAccountControl,msexchhidefromaddresslists,departmentNumber FROM 'GC://"+objADsPath+"' where sAMAccountName='xxxxxx' and objectCategory='Person' AND objectClass = 'USER' ORDER by sAMAccountname"

I get the error "Item cannot be found in the collection corresponding to the requested name or ordinal." when I add departmentName to the output. Any help is greatly appreciated.
0
Comment
Question by:MegGrace
  • 4
  • 3
8 Comments
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
Try using just "department" (without the quotes)
0
 

Author Comment

by:MegGrace
Comment Utility
Gah, just realized in the last paragraph I put departmentName - that should be departmentNumber.

pony10us: I just tried that but I get the same message.
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Hi, where is the department number attribute coming from?  I can't see it on the Organization tab of a user account.

You already have department being returned, so if you just use:
objCom.CommandText ="select name,telephonenumber,mobile,mail,company,title,department,sAMAccountName,sn,userAccountControl,msexchhidefromaddresslists FROM 'GC://"+objADsPath+"' where sAMAccountName='xxxxxx' and objectCategory='Person' AND objectClass = 'USER' ORDER by sAMAccountname"

then you should be able to retrieve
objRecordSet.Fields("department")

Rob.
0
 

Author Comment

by:MegGrace
Comment Utility
I am pretty sure it is a default attribute. Here is MS's list and it is there:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms675090(v=vs.85).aspx

I can get department, but I need to filter by the departmentNumber, as the department name isn't always consistent. Thanks.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:MegGrace
Comment Utility
I should also note that this is currently working in ColdFusion (cfldap), but need to migrate to asp.  Thanks.
0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
Have you tried to move them around and see if that helps?  Put the departmentNumber right after department?
0
 
LVL 26

Accepted Solution

by:
pony10us earned 500 total points
Comment Utility
You may also want to take a look at this:   http://blogs.msdn.com/b/tehnoonr/archive/2010/11/22/mapping-user-profile-properties-in-sharepoint-2010-to-ldap-attributes.aspx

It seems that departmentNumber is a "missing attribute" at least for Sharepoint.
0
 

Author Closing Comment

by:MegGrace
Comment Utility
I think you are correct. I have found I can get that information from our employee database, rather than AD, so I will go that much easier route. Thank you for time and help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now