Page 1 of 1 1
Topic Options
#185742 - 2008-02-26 09:19 PM I am not sure how to use the @Error Macro
Indigoseth Offline
Getting the hang of it

Registered: 2007-11-16
Posts: 78
I have a piece of code returning a "null" / "Empty" value, and i would like to say that if that value is found like an @error then goto error processing or something.

Thanks

Indigo

Top
#185743 - 2008-02-26 09:22 PM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
KiX does not support NULL

 Code:
If @ERROR <>0
  ;Do something
Else
  ;Quit
EndIf


 Code:
If Not @ERROR
  ;Do something
Else
  ;Quit
EndIf

Is that what you're meaning?

Top
#185747 - 2008-02-26 10:10 PM Re: I am not sure how to use the @Error Macro [Re: NTDOC]
Indigoseth Offline
Getting the hang of it

Registered: 2007-11-16
Posts: 78
Sometimes when i run WMIuptime UDF it returns a blank value.. I am not sure what this would happen, but to compensate i want to make an error trap to report this error, so i can go look at the computers that are returning blank values.

Indigo


Edited by Indigoseth (2008-02-26 10:11 PM)

Top
#185748 - 2008-02-26 10:14 PM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
duo Offline
Getting the hang of it

Registered: 2007-10-25
Posts: 61
Loc: iowa
Is a null return an actual error from this code? you could also do something like
 Code:
if $returnvalue=''
;report error
endif

Top
#185749 - 2008-02-27 01:17 AM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
It is probably due to the fact that WMI is disabled/corrupted/broken on the remote PC

Take a look at running this UDF before making a WMI call.

WMIConfirm() - Confirm access and version of WMI

Top
#185754 - 2008-02-27 09:29 AM Re: I am not sure how to use the @Error Macro [Re: duo]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: duo
Is a null return an actual error from this code? you could also do something like
 Code:
if $returnvalue=''
;report error
endif


I think you've got it. You might as well code for all scenarios:
 Code:
If @ERROR OR $uptime=''
   "ERROR: Cannot retrieve uptime or uptime is null. Error code is ["+@ERROR+"] "+@SERROR+@CRLF
   If Not @ERROR $=Execute("Exit 4203") EndIf
EndIf

Top
#185761 - 2008-02-27 02:08 PM Re: I am not sure how to use the @Error Macro [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Please run this debug-enabled version - define $Host to reference the system that fails, and report back ALL output.

Thanks,

Glenn
 Code:
$Host = ''           ; define host that returns null data

WMIUptime($Host)[4] ?
@ERROR ' / ' @SERROR ?


;;
;;======================================================================
;;
;;FUNCTION       WMIUptime()
;;
;;ACTION         Reads the boot and current time from a defined host and
;;               calculates the length of time the system has been up.
;;
;;AUTHOR         Glenn Barnas
;;
;;VERSION        1.1 / 2007/04/05 - Authentication, rename to WMIUptime
;;			Adjustment for uptime across timezone changes
;;
;;SYNTAX         WMIUptime([host] [,AuthPtr])
;;
;;PARAMETERS     host    - OPTIONAL - name of system to query
;;
;;               AuthPtr - OPTIONAL - pre-authenticated WMI object pointer
;;                 Use WMIAuthentication() udf to create the AuthPtr value
;;                 AuthPtr is not needed if user has admin rights
;;
;;REMARKS        Gets both the boot and local time from the remote system, 
;;               eliminating issues with timezone differences between target
;;               and client. 
;;
;;RETURNS        Array containing Days, Hours, Minutes, and Seconds of uptime.
;;               Also returns the cTime value in element 4
;;
;;DEPENDENCIES   WMI support
;;
;;TESTED WITH    W2K, WXP, W2K3
;;
;;EXAMPLES       $Host = ''
;;               $Up   = WMIUptime($Host)
;;               $Host has been up for ' $Up[0] ' days!'
;;
;;               $Host = 'server'
;;               $objWMI = WMIAuthenticate('computer', 'userid', 'User-P@ss!')
;;               $Up = WMIUptime($Host, $objWMI)
;;               'Secure host ' $Host ' has been up for ' $Up[0] ' days!'
;
Function WMIUpTime(OPTIONAL $_Target, OPTIONAL $_pAuth)

  Dim $_Bt[3], $_Ct[3], $_U[4]			; time arrays for boot, current, and uptime
  Dim $_, $_O					; Index var, Offset flag
  Dim $_B, $_C					; boot and current time strings
  Dim $_Bo, $_Co				; boot and current time offsets
  Dim $objWMIService, $colItems, $objItem	; WMI object vars

  $WMIUptime = -1				; default to invalid value
 
  $_Target = IIf($_Target, $_Target, '.')
  If InStr($_Target, '\') $_Target = Join(Split($_Target, '\'), '') EndIf
  If $_pAuth
    $objWMIService = $_pAuth
  Else
    $objWMIService = GetObject('winmgmts:{impersonationLevel=impersonate}!\\' + $_Target + '\root\cimv2')
    If @ERROR Exit Val('&' + Right(DecToHex(@ERROR), 4)) EndIf
  EndIf
  $colItems = $objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)

  For each $objItem in $colItems

    $_B = $objItem.LastBootUpTime	; host-local boot time
    $_C = $objItem.LocalDateTime	; host-local current time
'B: ' $_B ?
'C: ' $_C ?

    ; format is YYYYMMDDhhmmss.ssssss+ooo (or -ooo)
    ; Get the timezone offset. The offset will not play into the calculation unless
    ; the timezone switched between reboots, such as going from Standard to DaylightSavings
    If InStr($_B, '-') Or InStr($_B, '+')
      $_Bo = Val(SubStr($_B, 23))	; boot timezone offset
      $_Co = Val(SubStr($_C, 23))	; current timezone offset
      $_O = ($_Co - $_Bo) * 60		; find the difference, usually will be zero
    EndIf

    ; strip off the fractional seconds and time zone offset value
    $_B = Split($_B, '.')[0]
    $_C = Split($_C, '.')[0]

    ; format is now YYYYMMDDhhmmss - put into array
    $_Bt[0] = Val(SubStr($_B, 1, 4)) - 2000
    $_Bt[1] = Val(SubStr($_B, 5, 2))
    $_Bt[2] = Val(SubStr($_B, 7, 2))
    $_Bt[3] = 1.0 * SubStr($_B,13) + 60.0 * SubStr($_B,11,2) + 3600.0 * SubStr($_B,9,2)
    $_Ct[0] = Val(SubStr($_C, 1, 4)) - 2000
    $_Ct[1] = Val(SubStr($_C, 5, 2))
    $_Ct[2] = Val(SubStr($_C, 7, 2))
    $_Ct[3] = 1.0 * SubStr($_C,13) + 60.0 * SubStr($_C,11,2) + 3600.0 * SubStr($_C,9,2)


    ; Convert dates to Days, then convert to seconds and add the time value
    If $_Bt[1] < 3
      $_Bt[1] = $_Bt[1] + 12
      $_Bt[0] = $_Bt[0] - 1
    EndIf
    $_B = $_Bt[2] + ( 153 * $_Bt[1] - 457 ) / 5 + 365 * $_Bt[0] + $_Bt[0] / 4 - $_Bt[0] / 100 + $_Bt[0] / 400 - 306
    $_B = CDbl($_B) * 86400.0
    $_B = $_B + $_Bt[3]
    If $_Ct[1] < 3
      $_Ct[1] = $_Ct[1] + 12
      $_Ct[0] = $_Ct[0] - 1
    EndIf
    $_C = $_Ct[2] + ( 153 * $_Ct[1] - 457 ) / 5 + 365 * $_Ct[0] + $_Ct[0] / 4 - $_Ct[0] / 100 + $_Ct[0] / 400 - 306
    $_C = CDbl($_C) * 86400.0
    $_C = $_C + $_Ct[3]

    ; Calculate uptime as cTime, then convert to days / hours / minutes / seconds
    $_ = $_C - $_B + $_O
    $_U[4] = $_			; return the cTime value
    $_U[0] = Int($_ / 86400)
    $_ = $_ Mod 86400
    $_U[1] = $_ / 3600
    $_ = $_ Mod 3600
    $_U[2] = $_ / 60
    $_U[3] = $_ Mod 60

 Next

  $WMIUptime = $_U

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

Top
#185771 - 2008-02-27 05:10 PM Re: I am not sure how to use the @Error Macro [Re: Glenn Barnas]
Indigoseth Offline
Getting the hang of it

Registered: 2007-11-16
Posts: 78
I will look into the error codes when i have the time,

Thanks Doc for the WMICheck I included this as a fail safe to see if it will weed out the systems that are giving me null values.

Indigo

Top
#185773 - 2008-02-27 06:08 PM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Indigo

If you can report back the errors and the data values (or lack thereof), I can adjust the UDF to return appropriate errors that can be trapped.

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

Top
#185774 - 2008-02-27 08:08 PM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
Indigoseth Offline
Getting the hang of it

Registered: 2007-11-16
Posts: 78
I placed the Wmiconfirm.udf in my program and incapsolated the guts of the program inside of an if statement.

 Code:
$WMICheck = WMIConfirm('$computer')
    If $WMICheck
      Do what i need done.
    else
      Do Error proccessing.
    endif


Will this work as intended because i notice it is still letting the null information through. \:\(

Indigo


Edited by Indigoseth (2008-02-27 08:09 PM)

Top
#185775 - 2008-02-27 08:15 PM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
Indigoseth Offline
Getting the hang of it

Registered: 2007-11-16
Posts: 78
Glenn i ran the script you posted earlier, and it comes back with an error code of 0 saying it completed.

however, i did have to change a couple things to get it to work. Please review, and tell me if what i changed was ok.

 Code:
$Host = '$computer'           ; define host that returns null data

$uptime = WmiUptime($Host) ?
@ERROR ' / ' @SERROR ?
? $uptime[0]
Sleep 5



Thanks

Indigo

P.S. when i print the output for WMIUptime do i need to specify the [0]?


Edited by Indigoseth (2008-02-27 08:16 PM)

Top
#185776 - 2008-02-27 09:03 PM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Indigo,

I corrected the example with "WMIUptime($Host)[4]" - please re-copy the example and run it. Do not make any other changes (except to define the computer name between the quotes), or combine it with any other code.

You should get something like:
 Code:
B: 20080117171637.181125-300
C: 20080227145756.992000-300
3534079
0 / The operation completed successfully.

Please copy/paste the exact results from the screen when you reply.

This should be run from a command prompt, something like "Kix32 test.kix". Don't run it from explorer!

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

Top
#185786 - 2008-02-28 08:01 AM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
I'll leave you to work with Glenn, you're in good hands.

I've been a bit too busy with work and home to provide detailed support that Glenn is currently providing you.

Top
#185803 - 2008-02-28 04:52 PM Re: I am not sure how to use the @Error Macro [Re: NTDOC]
Indigoseth Offline
Getting the hang of it

Registered: 2007-11-16
Posts: 78
I re-ran the code and the message that was returned was:

0 / The operation completed successfully

I didn't see the other items that you mentioned would be there.

Indigo

Top
#185810 - 2008-02-28 06:25 PM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Did you copy the entire script, including the modified WMIUptime UDF? I added debugging statements to the UDF that will display when the UDF is called. If the UDF encounters an error, it will exit with that error and the debug message will not display. If the initial connection is successful, the UDF will make the queries and display the values. If the values are empty, you will still see the "B:" and "C:" lines with no data after them.

The fact that you're not getting the data prior to the error message tells me you don't have the updated UDF. Copy the script+UDF that I posted, save it, define the $Host value, and run it to generate the debug data.

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

Top
#185811 - 2008-02-28 06:27 PM Re: I am not sure how to use the @Error Macro [Re: NTDOC]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Doc:

I need to know if the UDF is not failing to connect, but is returning empty data. I've not encountered that situation, so didn't code for it. If that's happening, I'll adjust the UDF to return an appropriate exit status.

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

Top
#185813 - 2008-02-28 07:31 PM Re: I am not sure how to use the @Error Macro [Re: Glenn Barnas]
Indigoseth Offline
Getting the hang of it

Registered: 2007-11-16
Posts: 78
I am not getting the result your asking for.

#185761 - This is the post number i copied the code from. I am positive i have all the code. I tried running it from a batch file and from a command prompt.

Any other ideas on what i should try?

Indigo

Top
#185814 - 2008-02-28 07:35 PM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
Indigoseth Offline
Getting the hang of it

Registered: 2007-11-16
Posts: 78
To address the failing to connect, I used WMIConfirm in my original script to catch systems that the WMI Wasn't working? and the system still had the issue of the null value. It passed the WMI Confirms check?

Indigo

Top
#186013 - 2008-03-06 02:13 PM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
Indigoseth Offline
Getting the hang of it

Registered: 2007-11-16
Posts: 78
I don't understand... is everyone dropping this?
Top
#186015 - 2008-03-06 04:08 PM Re: I am not sure how to use the @Error Macro [Re: Indigoseth]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Copy the script exactly as shown. Do not modify anything. Save it to a file UT.KIX - preferrably on a file share somewhere. Make sure Kix32.exe is there. DO NOT USE WKIX32.EXE!!!

Log into the computer that is not returning data. Open a command prompt and run KIX32 UT.KIX while in the directory / file share where you saved the file.

Post back the output from the screen.

You can then edit the file to define a computer - change the line
$Host = ''
to
$Host = 'some_computer'

Do not use a variable, and do not add anything other than the target hostname. Run the command as above - KIX32 UT.KIX and post the screen output.

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 640 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.16 seconds in which 0.073 seconds were spent on a total of 12 queries. Zlib compression enabled.

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