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!

Script Not Returning Values 1

Status
Not open for further replies.

djtech2k

MIS
Jul 24, 2003
1,097
US
A script keeps failing in an environment and I cannot figure out why. This method has worked for me hundreds of times elsewhere. It started failing at "option Explicit", so I commented it out so it would run. As of now, it DOES exho out the "recordcount", but it shows nothing else.

Any ideas why it would just not show anything?

Code:
strUserName = "LastName, FirstName" 'displayName from AD

Call SearchAD(strUserName)

Function SearchAD(strUserName)
'Option Explicit

Dim objRootDSE, strDNSDomain, objCommand, objConnection
Dim strQuery, strBase, strFilter, strAttributes
Dim objRecordSet, strAlias, strName, strSAM

' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

' Search for all user objects. Return Values.
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=person)(objectClass=user)(displayName=" & strUserName & "))"
strAttributes = "sAMAccountName,cn"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
wscript.echo objRecordSet.RecordCount

'Do Until objRecordSet.EOF
'	wscript.echo "TEST"
'  strName = objRecordSet.Fields("cn")
'  'strAlias = objRecordSet.Fields("mailNickname")
'  strSAM = objRecordSet.Fields("sAMAccountName")
'  wscript.echo strName & vbtab & strSAM
'  'Wscript.Echo strSAM & " ; " & strAlias & " ;   " & strName
'  	
'    objRecordSet.MoveNext
'	Loop

Do Until objRecordSet.EOF
wscript.echo "111111111"
wscript.echo objRecordSet.Fields("sAMAccountName")
objRecordSet.MoveNext
Loop


objConnection.Close
End Function
 
Any ideas? I have even removed the filter of the name and just let it roll with any user it finds. Same results...it shows the recordcount then nothing else comes back. I am baffled.
 
I have stripped this thing down to almost nothing...it still wont run the loop.
 
Add option explicit again and any on error remove next...what error do you get?

Code:
Function SearchAD(strUserName)
	' Determine DNS domain name from RootDSE object.
	Dim objRootDSE : Set objRootDSE = GetObject("LDAP://RootDSE")
	Dim strDNSDomain : strDNSDomain = objRootDSE.Get("defaultNamingContext")
	
	' Use ADO to search Active Directory.
	Dim objCommand : Set objCommand = CreateObject("ADODB.Command")
	Dim objConnection : Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Provider = "ADsDSOObject"
	objConnection.Open "Active Directory Provider"
	objCommand.ActiveConnection = objConnection
	
	' Search for all user objects. Return Values.
	Dim strBase : strBase = "<LDAP://" & strDNSDomain & ">"
	Dim strFilter : strFilter = "(&(objectCategory=person)(objectClass=user)(displayName=" & strUserName & "))"
	Dim strAttributes : strAttributes = "sAMAccountName,cn"
	Dim strQuery : strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"	
' 	WScript.Echo strQuery

	objCommand.CommandText = strQuery
	objCommand.Properties("Page Size") = 100
	objCommand.Properties("Timeout") = 30
	objCommand.Properties("Cache Results") = False
	
	Dim objRecordSet : Set objRecordSet = objCommand.Execute
	wscript.echo objRecordSet.RecordCount
	
	Dim strName, strSAM, strAlias
	Do Until objRecordSet.EOF
		strName = objRecordSet.Fields.Item("cn").Value
' 		strAlias = objRecordSet.Fields("mailNickname")
		strSAM = objRecordSet.Fields.Item("sAMAccountName").Value
		wscript.echo strName & vbTab & strSAM
				
		objRecordSet.MoveNext
	Loop
	
	objConnection.Close
End Function

...it's your code I just defined the variables closer to where they get initialized...can't test it just yet...not sure if this will work for you

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
If memory serves me correct, it was getting "Microsoft vbscript compilation error: expected end of statement" which makes NO sense.

 
A second look at your code...I think the issue is with the "Cache Results" currently set to false... if you comment that out...it works fine

Code:
Option Explicit 

Dim strUserName : strUserName = "Test User01" 'displayName from AD

Call SearchAD(strUserName)

Function SearchAD(strUserName)	
	Dim objRootDSE, strDNSDomain, objCommand, objConnection
	Dim strQuery, strBase, strFilter, strAttributes
	Dim objRecordSet, strAlias, strName, strSAM
	
	' Determine DNS domain name from RootDSE object.
	Set objRootDSE = GetObject("LDAP://RootDSE")
	strDNSDomain = objRootDSE.Get("defaultNamingContext")
	
	' Use ADO to search Active Directory.
	Set objCommand = CreateObject("ADODB.Command")
	Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Provider = "ADsDSOObject"
	objConnection.Open "Active Directory Provider"
	objCommand.ActiveConnection = objConnection
	
	' Search for all user objects. Return Values.
	strBase = "<LDAP://" & strDNSDomain & ">"
	strFilter = "(&(objectCategory=person)(objectClass=user)(displayName=" & strUserName & "))"
	strAttributes = "sAMAccountName,cn"
	strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
	objCommand.CommandText = strQuery
	objCommand.Properties("Page Size") = 100
	objCommand.Properties("Timeout") = 30
	'objCommand.Properties("Cache Results") = True
	Set objRecordSet = objCommand.Execute
	WScript.Echo "Record Count:  " & objRecordSet.RecordCount
	
	Do Until objRecordSet.EOF
		strName = objRecordSet.Fields("cn")
		'strAlias = objRecordSet.Fields("mailNickname")
		strSAM = objRecordSet.Fields("sAMAccountName")
		WScript.Echo strName & vbtab & strSAM
		'WScript.Echo strSAM & " ; " & strAlias & " ;   " & strName
		
		objRecordSet.MoveNext
	Loop
	
	objConnection.Close
End Function

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
You could also set the "Cache Results" to True like I had changed it in the example above.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
I am not thru testing the cache results yet, but if I uncomment the "Option Explicit", I get the "Expected Statement" Error.
 
Remove it from inside your function...you should specify that at the beginning of your entire script.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top