Page 1 of 1 1
Topic Options
#198575 - 2010-05-10 04:39 PM IDispatch pointers not allowed in expressions!
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
Hey people got a small problem with my script. I'm trying to query an account name, first of all it querires an oracle database then it should query the active directory. the problem is i get an error message "IDispatch pointers not allowed in expressions!"

I think the problem is that i'm useing periods in a varible. any advice on how to resolve this issue

 Code:
Function fn_testAD()

Dim $cn, $cmd, $rs, $cnstring, $cmdtext, $selectedProperties, $propertynames, $oCn, $oCmd, $oRS, $i
$cnstring = 'Provider=MSDAORA;Password=password;User ID=user;Data Source=source;Persist Security Info=True;'
							
$cmdtext = "SELECT DISTINCT PEOPLE.PERSON_CODE, PEOPLE.FORENAME, PEOPLE.SURNAME, PEOPLE_UNITS.UNIT_INSTANCE_CODE, PEOPLE_UNITS.CALOCC_CODE FROM PEOPLE INNER Join PEOPLE_UNITS ON PEOPLE.PERSON_CODE = PEOPLE_UNITS.PERSON_CODE and PEOPLE_UNITS.UNIT_TYPE = 'R' and PEOPLE_UNITS.CALOCC_CODE = '09' and length(PEOPLE_UNITS.UNIT_INSTANCE_CODE) = '6' "
$cn = CreateObject("adodb.connection") 
$cmd = CreateObject("adodb.command") 
$cn.connectionstring = $cnstring 
$cn.Open 
$cmd.activeconnection = $cn 
$cmd.commandtext = $cmdtext 
$selectedProperties = "ADsPath"
$propertynames = Split($selectedProperties, ",")

$oCn = CreateObject("ADODB.Connection")
$oCmd = CreateObject("ADODB.Command")
$oCn.Provider = "ADsDSOObject"
$ = $oCn.Open("Active Directory Provider", "", "", -1)
$oCmd.ActiveConnection = $oCn
$oCmd.Properties("Page Size").Value = 1000
$oCmd.Properties("Searchscope").Value = 2 ;ADS_SCOPE_SUBTREE							
$rs = CreateObject("adodb.recordset") 
$rs.cursortype = 3 
$rs.locktype = 3 
$rs.Open($cmd)
While Not $rs.EOF And Not $rs.BOF
														
															
	? "PERSON_CODE: " + $rs.Fields.Item("PERSON_CODE").Value
	? "FORENAME: " + $rs.Fields.Item("FORENAME").Value
	? "SURNAME: " + $rs.Fields.Item("SURNAME").Value
	? "YEAR: " + $rs.Fields.Item("CALOCC_CODE").Value
	? "GROUP: " + $rs.Fields.Item("UNIT_INSTANCE_CODE").Value
	? " "			
	$Tempname = $rs.Fields.Item("PERSON_CODE").Value
	? $Tempname

 $oCmd.CommandText = "SELECT " + $selectedProperties + " FROM 'LDAP://student.domain.co.uk/DC=student,DC=domain,DC=co,DC=uk' WHERE objectCategory='user' and objectClass = 'user' AND sAMAccountName = '$Tempname' ORDER BY Name"
$oRS = $oCmd.Execute
; While Not $oRS.EOF
	$obj = GetObject($oRS.Fields($propertynames[0]))
	? "CN Name: " +  + $obj.cn 
	? "First Name: " + $obj.givenname
	? "Surname: " + $obj.sn
	? "Login Name: " + $obj.sAMAccountName	
	? "Email: " + $obj.mail 
	? "Home Directory: " + $obj.homeDirectory	
	? " "								
	$Tempname = $rs.Fields.Item("PERSON_CODE").Value
	? $Tempname
	Sleep 2							
				
	 $rs.MoveNext
 ; Loop				
										
$rs.Close
$cn.Close
			
EndFunction


it fails on this line.

$obj = GetObject($oRS.Fields($propertynames[0]))

However If I remove the varible $Tempname in the active directory and change it to a static name it works

Any help much appreciated.

Thanks

Glyn


Edited by 5861king (2010-05-10 04:41 PM)

Top
#198576 - 2010-05-10 04:58 PM Re: IDispatch pointers not allowed in expressions! [Re: 5861king]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Looks like $Tempname is an array and not a simple one element string value.
You could try displaying $Tempname[0] or if element [0] does not hold the correct data, use whatever element you need.

Try:
 Code:
For $i = 0 to UBound($Tempname)
	? "Element: " + $i + " contains: " + $Tempname[$i]
Next
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198577 - 2010-05-10 04:58 PM Re: IDispatch pointers not allowed in expressions! [Re: 5861king]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Use VarTypeName() to check the data type of variables on the failing line. The "IDispatch..." error normally means that you are using a complex data type (such as an object or array) where a simple data type (such as a string) is expected.

This can catch you out for example when (say) an AD object property is usually a simple string, but can sometimes be an an array when multiple values are returned.

Top
#198578 - 2010-05-10 05:22 PM Re: IDispatch pointers not allowed in expressions! [Re: Richard H.]
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
 Originally Posted By: Richard H.
Use VarTypeName() to check the data type of variables on the failing line. The "IDispatch..." error normally means that you are using a complex data type (such as an object or array) where a simple data type (such as a string) is expected.

This can catch you out for example when (say) an AD object property is usually a simple string, but can sometimes be an an array when multiple values are returned.


When running the vartype it comes back as a decimal does this help?

Glyn

@Mart

I tried the ubound but it came back with no results. e.g. empty


Edited by 5861king (2010-05-10 05:33 PM)

Top
#198580 - 2010-05-11 10:40 AM Re: IDispatch pointers not allowed in expressions! [Re: 5861king]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: Glyn
When running the vartype it comes back as a decimal does this help?


Depends. You were expecting a string, yesno? Decimal's probably not good then. Which variable did you VarTypeName()?

More importantly you have:
 Code:
$oRS = $oCmd.Execute


But you don't check the success of the command. You have assumed that it has worked and you then go on to reference the properties.

If the query failed then you will get unexpected results.

Check what is coming back in $Tempname *very* carefully and see what might cause problems, if it doesn't exactly match the AD record you are going to have problems.

Be especially careful of special characters. For example if a name contains leading / trailing spaces then it won't match, and if it contains a single quote such as "O'Leary" then it will actually break the query.

Other problem characters are "\" which is used to escape characters such as the "," in some query types (LDAP mainly).

Top
#198582 - 2010-05-11 11:22 AM Re: IDispatch pointers not allowed in expressions! [Re: Richard H.]
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
Hi Richard H.

I done the VarTypeName() on $tempname i am assuming thats correct.
I am also the $tempname is workingas i'm running this bit of code.

 Code:
$Tempname = $rs.Fields.Item("PERSON_CODE").Value
	? $Tempname


from this bit of code a number is returned which it does for example
when running ? $tempname it reports back on the console as

9847430

therefore I would assusme useing $tempname would work in the active directory as show below

 Code:
$oCmd.CommandText = "SELECT " + $selectedProperties + " FROM 'LDAP://student.domain.co.uk/DC=student,DC=domain,DC=co,DC=uk' WHERE objectCategory='user' and objectClass = 'user' AND sAMAccountName = '9847430' ORDER BY Name"



now if change the code to the folowing it works

 Code:
$oCmd.CommandText = "SELECT " + $selectedProperties + " FROM 'LDAP://student.domain.co.uk/DC=student,DC=domain,DC=co,DC=uk' WHERE objectCategory='user' and objectClass = 'user' AND sAMAccountName = '$Tempname' ORDER BY Name"


so I'm not to sure what is going wrong?


Edited by 5861king (2010-05-11 11:25 AM)

Top
#198584 - 2010-05-11 11:50 AM Re: IDispatch pointers not allowed in expressions! [Re: 5861king]
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
Little bit of an update

If i create a new varible and call it $tempname2 then i manualy insert a number for example

 Code:
$tempname2 = '48394823923'


this makes the script work so the problem is that the

so this bit code does not work.

 Code:
$Tempname = $rs.Fields.Item("PERSON_CODE").Value
? $Tempname


however why does the varible work when asking it by useing the ?


Edited by 5861king (2010-05-11 12:01 PM)

Top
#198586 - 2010-05-11 02:46 PM Re: IDispatch pointers not allowed in expressions! [Re: 5861king]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
It could be as simple as the variable type.

Try this:
 Code:
$Tempname = CStr($rs.Fields.Item("PERSON_CODE").Value)


The CStr() will convert the value to a string, the same as assigning an explicit value in single quotes.

Top
#198587 - 2010-05-11 03:08 PM Re: IDispatch pointers not allowed in expressions! [Re: Richard H.]
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
Thats brilliant,

It worked thank you very much Richard. \:\)

So i take it the varible i was useing was not in a string format is that correct?

Top
#198589 - 2010-05-11 04:02 PM Re: IDispatch pointers not allowed in expressions! [Re: 5861king]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: 5861king
So i take it the varible i was useing was not in a string format is that correct?


Correct.

VarTypeName() reported it as a decimal, which is not a valid KiXtart data type. KiXtart only supports variants with sub-types of String, Integer and Double.

When you use non-intrinsic objects you can get back a wider variety of types and KiXtart will do it's best to convert to a supported type when you reference it. Depending on the context if you are reusing the object then KiXtart may just pass it on as it is.

In this case we use CStr() to explicitly convert the value to a string to override the default action.

Top
#198590 - 2010-05-11 04:46 PM Re: IDispatch pointers not allowed in expressions! [Re: Richard H.]
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
Thank you for all the Help & Explanation

Glyn

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 756 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.062 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org