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

x
?
Solved

Getting/Filtering on departmentNumber in AD query in vbscript

Posted on 2012-04-12
8
Medium Priority
?
2,071 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
ID: 37840211
Try using just "department" (without the quotes)
0
 

Author Comment

by:MegGrace
ID: 37840232
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
ID: 37840733
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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

Author Comment

by:MegGrace
ID: 37843017
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
 

Author Comment

by:MegGrace
ID: 37843183
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
ID: 37843199
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 2000 total points
ID: 37843382
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
ID: 37844180
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

971 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