Page 1 of 1 1
Topic Options
#206969 - 2013-03-26 05:17 PM WMIQuery crashes on some computers
KIXKicks Offline
Starting to like KiXtart

Registered: 2002-07-26
Posts: 177
Loc: Vancouver, WA
WMIQuery is crashing (see attached file) on some computers, yet works on others.

It crashes in this statement in our login script, if I comment it out it crashes on the next WMIQUERY statement.

Of other note, we recently rolled out new versions of KIX32.EXE & KIXFORMS.DLL:

KIX32.EXE version 4.63
KIXFORMS.DLL version 2.47.5

 Code:
$SERIALNO = WMIQUERY("SERIALNUMBER", "WIN32_BIOS")[0]

If $SERIALNO < " " 	
	$SERIALNO = WMIQUERY("SMBIOSASSETTAG", "WIN32_SYSTEMENCLOSURE")[0]
EndIf


Using Glenn's UDF

 Code:
Function WMIQuery($sWhat, $sFrom, Optional $sComputer, Optional $sWhere, Optional $x, Optional $root, Optional $pAuth)
	Dim $, $sQuery, $objEnum, $sValue, $TMP, $SystemSet, $objInstance, $objLocator 

	$sComputer = Trim(Join(Split($sComputer,'\'),''))
	If Not $sComputer Or $sComputer = @WKSTA
		$sComputer = '.'
	EndIf
	
	If Not $root
		$root = '\root\cimv2'
	EndIf
	
	$sQuery = 'Select ' + $sWhat + ' From '+ $sFrom
	
	If $sWhere And $x
 		$sQuery = $sQuery + " Where " + $sWhere + " = '" + $x + "'"
	EndIf
	
	If $pAuth
    		$SystemSet = $pAuth
  	Else
    		$SystemSet = GetObject('winmgmts:{impersonationLevel=impersonate}!\\' + $sComputer + $root)
    		If @ERROR Or Not $SystemSet
      			Exit Val('&' + Right(DecToHex(@ERROR), 4))
    		EndIf
  	EndIf

	$objEnum = $SystemSet.ExecQuery($sQuery)
	If @ERROR Or Not $objEnum
		Exit VAL("&" + Right(DecToHex(@ERROR), 4))
 	EndIf
	
	For Each $objInstance In $objEnum
		$ = Execute(Chr(36) + 'sValue = ' + Chr(36) + 'objInstance.' + $sWhat)
		If VarType($sValue) & 8192
			$Tmp = $Tmp + '|' + Join($sValue,'|')
		Else
			$Tmp = $Tmp + '|' + $sValue 
		EndIf
	Next

	$WMIQuery = split(substr($Tmp,2),'|')
	Exit Val("&" + Right(DecToHex(@ERROR), 4))
EndFunction


Attachments
303.jpg
Description: WMIQuery Error Message



Top
#206970 - 2013-03-26 05:43 PM Re: WMIQuery crashes on some computers [Re: KIXKicks]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
and what would be line 693?
_________________________
!

download KiXnet

Top
#206971 - 2013-03-26 05:45 PM Re: WMIQuery crashes on some computers [Re: Lonkero]
KIXKicks Offline
Starting to like KiXtart

Registered: 2002-07-26
Posts: 177
Loc: Vancouver, WA
Line 693 is: $SERIALNO = WMIQUERY("SERIALNUMBER", "WIN32_BIOS")[0]
Top
#206972 - 2013-03-26 07:13 PM Re: WMIQuery crashes on some computers [Re: KIXKicks]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
It's possible the array in not being created and or the Serialnumber is not there.

Maybe something like
 Code:
$QUERY = WMIQUERY("SERIALNUMBER", "WIN32_BIOS")
if unbound($QUERY)>-1
  $SERIALNO=$QUERY[0]
else
  $SERIALNO="NONE/MISSING"
endif
_________________________
(... better days ahead)

Top
#206974 - 2013-03-26 07:28 PM Re: WMIQuery crashes on some computers [Re: Allen]
KIXKicks Offline
Starting to like KiXtart

Registered: 2002-07-26
Posts: 177
Loc: Vancouver, WA
Thanks Allen, but it does not seem to like this code:

ERROR: Error in expression: this type of array not supported in expressions.!
Script: C:\NETWORK\loginscript.kix
Line: 699

Line 699: $SERIALNO=$QUERY[0]



Edited by KIXKicks (2013-03-26 07:28 PM)

Top
#206976 - 2013-03-26 07:31 PM Re: WMIQuery crashes on some computers [Re: KIXKicks]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
ooops ... lol

fix my code to use ubound... not unbound and try again.
_________________________
(... better days ahead)

Top
#206981 - 2013-03-27 01:22 PM Re: WMIQuery crashes on some computers [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
KixKicks,

Please log the error returned from the WMI call, something like
 Code:
If @ERROR
  'WMI:' + @ERROR + ' / ' + @SERROR + @CRLF
EndIf
Let me know what error is being returned. I've recently (past 6 weeks) seen issues with WMI calls - scripts that have been in place for over 10 years have suddenly started throwing weird errors when invoking WMI. Specifically, I've been getting error 9 returned. Again, it's a few systems, not all. A reboot will help, and sometimes restarting the WMI services one or more times will help as well. We're using Kix 4.62 in production.

The WMIQuery code hasn't changed since 2009 and was originally written in 2001 (and - it's Radimus' UDF, I just added the authentication logic ;\) ). Since we haven't seen issues with the code in all that time, I have a feeling that something changed in Kix itself, or with a recent O/S patch.

Can you try running with Kix 4.53 on one of the problem systems and see if the problem persists? That would help narrow things down to Kix.

Also, using this UDF in a login script might not be the best method. This UDF instantiates the connection for each individual query, which can take a fair amount of time. For example, my login script completes a dozen mappings in seven-eighths of a second, a single call to WMIQuery can take 400ms or more. I found this when I called WMIQuery just nine times in a script. Rewriting the code to instantiate the WMI object and then make the 9 queries in sequence in the single UDF completed all queries in about 550ms compared to over 3.5 seconds! Look at my WMINicInfo UDF for an example - the top half of the UDF should look familiar!

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#206982 - 2013-03-27 05:00 PM Re: WMIQuery crashes on some computers [Re: Glenn Barnas]
KIXKicks Offline
Starting to like KiXtart

Registered: 2002-07-26
Posts: 177
Loc: Vancouver, WA
I used Allen's revised code and it worked!

Glenn, while we are using 4.63 now (per Microsoft to fix another issue), we had been on 4.62 when this first started happening.

Sorry Radimus for not giving credit to your original work.

I will look at your WMINicInfo UDF.

Top
#206984 - 2013-03-27 06:20 PM Re: WMIQuery crashes on some computers [Re: KIXKicks]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
I'd still like to know what error you are receiving. The UDF will return an array if the query was successful, but if errors occur during initialization of the WMI object, the return value will be undefined. That's why checking for a UBound seems to work - the return is not an array when errors occur.

Allen's solution works around the issue but does not identify or respond to the problem. The correct method of using this type of function is
 Code:
$aResult = WMIQuery(args...)
If @ERROR
  ; error occurred - decide if default data can be used (ie - create an empty array 
  ; with the expected number of elements) or an error handler routine is needed.
Else
 ; No error - process the results
EndIf
Checking for the correct number of elements in an array returned from a function is always a good idea, but is not a substitute for proper error checking and handling.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#206986 - 2013-03-27 09:41 PM Re: WMIQuery crashes on some computers [Re: Glenn Barnas]
KIXKicks Offline
Starting to like KiXtart

Registered: 2002-07-26
Posts: 177
Loc: Vancouver, WA
Sorry Glenn, I couldn't get it to crash even with changing the code back (note the test computer had be rebuilt twice due to this issue). I couldn't get the 4.53 version of KIX32.EXE to work in our environment (probably cause of the recoding I did to get 4.63 to work).

Here is the code I used:

 Code:
        $SERIALNO = WMIQUERY("SERIALNUMBER", "WIN32_BIOS")[0]
	If @ERROR
		Shell '%COMSPEC% /C "ECHO WMI_BIOS: ' + @ERROR + ' / ' + @SERROR + ' >>' + $INSTALLDIR + '\Debug_LS.txt 2>&1"'
	Else
		If $SERIALNO = " "
			$SERIALNO = WMIQUERY("SMBIOSASSETTAG", "WIN32_SYSTEMENCLOSURE")[0]
			If @ERROR
				Shell '%COMSPEC% /C "ECHO WMI_SYSTEM_ENCLOSURE: ' + @ERROR + ' / ' + @SERROR + ' >>' + $INSTALLDIR + '\Debug_LS.txt 2>&1"'
			Else
				Shell '%COMSPEC% /C "ECHO WMI_SYSTEM_ENCLOSURE: ' + @ERROR + ' / ' + @SERROR + ' >>' + $INSTALLDIR + '\Debug_LS.txt 2>&1"'
			EndIf
		Else
			Shell '%COMSPEC% /C "ECHO WMI_BIOS: ' + @ERROR + ' / ' + @SERROR + ' >>' + $INSTALLDIR + '\Debug_LS.txt 2>&1"'
		EndIf
	EndIf


On this computer, I get:

WMI_BIOS: 0 / The operation completed successfully.

Top
#206987 - 2013-03-27 10:26 PM Re: WMIQuery crashes on some computers [Re: KIXKicks]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
????

You REALLY need to look at my fMsg UDF!! \:D You're working WAY too hard to put text in a file..

No frills log method:
 Code:
$ = RedirectOutput($INSTALLDIR + '\Debug_LS.txt')
@DATE + ' ' + @TIME + ': ' + @ERROR + ' / ' + @SERROR 
$ = RedirectOutput('')
The fMsg UDF takes that basic concept and puts it into a UDF, which allows you to display and/or log messages; timestamp logged messages; display/log only when Debug mode is active, etc..
 Code:
Global $MSG_LOG_, $DEBUG
$MSG_LOG_ = $INSTALLDIR + '\Debug_LS.txt'
$DEBUG = 1
;
; your code...
;
; write the debug message only to the log, only if DEBUG is enabled
fMsg('Result of WMIQuery: + @ERROR + ' / ' + @SERROR, ''. 0, 20)
The latest versions of all my UDFs are PostPrepped nightly and published on my web site. Many are posted here, but the freshest copies are always at http://www.innotechcg.com.

Anyway - figures that it won't fail when you need it to. If you're doing conditional queries like you show, and only a few, WMIQuery is probably OK. I'd be concerned if you were gathering a half-dozen or more values for use or display. This is where a more custom solution would be beneficial.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

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 2375 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

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

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