Jump to content

setting error level on completetion of script


Recommended Posts

Hi I'm currently attempting to write a script which checks the time on a server, if the time is prior to 7:45 I want to force the script to exit and set errorlevel which I'll be able to pick up externally of the script using altiris. the script at present will create a file which I can then do a dir command and if exist continue, it would be easier and cleaner if I could sipmly set the error level and pick that up, hope this makes sense.

#include <Date.au3>

FileDelete ("c:\temp\time.txt")

$var1 = _NowTime ()

if $var1 < "07:45:00" Then

FileOpen ("c:\temp\time.txt", 1)

FileWrite ("c:\temp\time.txt", $var1)

FileClose ("c:\temp\time.txt")

endif

Thanks for any help

Link to comment
Share on other sites

You could start by reading the help file.

You might check out the Exit command in the help file.

This might help (it's from the help file):

Exit

--------------------------------------------------------------------------------

Terminates the script.

Exit [return code]

Parameters

return code [optional] Integer that sets the script's return code. This code can be used by Windows or the DOS variable %ERRORLEVEL%. The default is 0. Scripts normally set an errorlevel of 0 if the script executed properly; error levels 1 and above typically indicate that the script did not execute properly.

It would probably help to spend more time with the help file, too.

Do we see a pattern here?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I have tried the help file, I modified my script to display the exit code but in dos %errorlevel% is reporting back as 0 regardless of what EXIT is set to.

#include <Date.au3>

Opt("OnExitFunc", "endscript")

Func endscript()

MsgBox(0,"","after last statement " & @EXITCODE)

EndFunc

FileDelete ("c:\temp\time.txt")

DirCreate ("c:\temp")

$var1 = _NowTime ()

if $var1 < "07:00:00" Then

$file = FileOpen ("c:\temp\time.txt", 1)

FileWrite ("c:\temp\time.txt", $var1)

FileClose ("c:\temp\time.txt")

Exit (1)

EndIf

exit (0)

Link to comment
Share on other sites

Using < or > with strings may not give you the results you want.

This may work before 7am with some luck :)

#include <Date.au3>
Opt("OnExitFunc", "endscript")

Func endscript()
    MsgBox(0, "", "after last statement " & @exitCode)
EndFunc

FileDelete("c:\temp\time.txt")
DirCreate("c:\temp")
$var1 = _NowTime(4)

If Int(StringLeft($var1, 2)) < 7 Then
    $handle = FileOpen("c:\temp\time.txt", 1)
    If $handle <> -1 Then
        FileWrite($handle, $var1)
        FileClose($handle)
    EndIf
    Exit 1
EndIf

Exit

Use the file handles also.

Link to comment
Share on other sites

Maybe simpler:

#include <File.au3>

FileDelete("c:\temp\time.txt")
If (@HOUR < 7) Or (@HOUR = 7 And @MIN < 45) Then
    _FileWriteLog("c:\temp\time.txt", "Tried to start script before 07:45.")
    Exit 1
Else
    Exit 0
EndIf

Note, _FileWriteLog() adds a date/time stamp automatically, which for me is cleaner and easier.

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...