Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADO AD Description field

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hi,

I am having some trouble with querying the description field against groups from Active Directory. I get a Type mismatch. I guess that means it means this particular description field has been set up as an array and I am asking it to return it as a string. I can never find any MSDN pages on ADO "Active Directory Provider".

Below is my code. Changing to different fields, I have discovered the error is in
Code:
 objRS.Fields("Description").Value

Code:
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase    =  "<LDAP://"& strDNSDomain &">;" 
strFilter  = "(&(objectCategory=group)(cn=GG_*)(cn=*PRINT_USERS));"           
strAttrs   = "cn,description;"         
strScope   = "Subtree"             

set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
objRS.MoveFirst
While Not objRS.EOF
    Wscript.Echo objRS.Fields("cn").Value & "  " & objRS.Fields("Description").Value
    objRS.MoveNext
Wend

The odd thing is, I have another script that is similar - querying a different bunch of security groups, yet that doesn't error all though I ask for the description field.

I wonder if anyone would be able to confirm my suspicions or / and provide a solution to my issue.

Kind regards,

Woter
 
The description field is returned through ADO as a collection. You must do a For Each...Next loop to get the data out of it. Blue text shows my proposed changes.

Code:
[blue]Option Explicit

Dim objRootDSE, strDNSDomain, strBase, strFilter, strAttrs, strScope, objConn, objRS, item, sDesc[/blue]

Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase    =  "<LDAP://"& strDNSDomain &">;"
strFilter  = "(&(objectCategory=group)(cn=GG_*)(cn=*PRINT_USERS));"
strAttrs   = "cn,description;"
strScope   = "Subtree"

set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"

set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)

objRS.MoveFirst

While Not objRS.EOF
[blue]	' Check if field is null to avoid error
	If IsNull(objRS.Fields("Description").Value) Then
		sDesc = ""
	Else
		For Each item In objRS.Fields("Description").Value
			sDesc = sDesc & item
		Next
	End If[/blue]
	
	Wscript.Echo objRS.Fields("cn").Value & "	" & [blue]sDesc[/blue]

[blue]	' Clear the variable after each use
	sDesc = Empty[/blue]

	objRS.MoveNext
Wend

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
As I said before, I had a script that quiered a similar thing in a slightly different way. I have modified it so the LDAP query to pull the exact information I need.

If anyone else is interested here is the code:
Code:
Const ADS_SCOPE_BASE = 0
	Set dict = CreateObject("Scripting.Dictionary")
	Set objConnection = CreateObject("ADODB.Connection")
	Set objCommand =   CreateObject("ADODB.Command")
	objConnection.Provider = "ADsDSOObject"
	objConnection.Open "Active Directory Provider"
	Set objCommand.ActiveConnection = objConnection
	Set objRootDSE = GetObject("LDAP://RootDSE")
	strDNSDomain = objRootDSE.Get("defaultNamingContext")
	objCommand.Properties("Page Size") = 1000
	objCommand.Properties("Sort on") = "sAMAccountName"
	strQuery = "SELECT cn, description FROM 'LDAP://" & strDNSDomain & "' WHERE objectCategory='group' AND cn='GG_*' AND (cn='*PRINT_USERS' or cn='*PRINTERS')"
	objCommand.CommandText = strQuery
	Set objRecordSet = objCommand.Execute
	objRecordSet.MoveFirst
	Do Until objRecordSet.EOF
	    strGroupName = objRecordSet.Fields("cn").Value
	    arrDesc = objRecordSet.Fields("description").value
		If IsNull(arrDesc) Then
			strDesc = ""
		Else
			For Each strLine In arrDesc  
				'strDesc = strGroupName & " | " & strLine
				strDesc = strline
			Next  
		End If
		dict.Add strGroupName,strDesc
		objRecordSet.MoveNext
	Loop
 
Sorry PCS. I forgot to submit my last reply yesterday. Got side tracked.

Thank you for your reply. I see how that it is a multi value field and see how you iterate through.

Many thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top