Chimaera

Error log based on exit of runwait

3 posts in this topic

I was adding some logging to one of my programs but ive hit a snag

the loop is like this and it just goes through an array of items to remove

For $TaskLoop = 1 To $aSchTaskNames[0]
    Local $sSchTask = RunWait(@ComSpec & ' /c ' & 'schtasks.exe' & ' /delete /tn ' & '"' & $aSchTaskNames[$TaskLoop] & '"' & ' /f ', '', @SW_HIDE)
;~  ConsoleWrite( $aSchTaskNames[$TaskLoop] & @CRLF)
    If @error < 0 Then
        ContinueLoop
    ElseIf $sSchTask >= 0 Then
        ConsoleWrite( '>0 ' & @error & @CRLF)
        FileWrite(@ScriptDir & '\errorlog.txt', @CRLF & @UserName & ' - ' & $aSchTaskNames[$TaskLoop]) ; create txt error log
        ContinueLoop
    EndIf
Next

The problem is it writes every item in the array to the log.. I only want it to write the entry when it removes one from the machine

Ive tried it this way as well

For $TaskLoop = 1 To $aSchTaskNames[0]
    RunWait(@ComSpec & ' /c ' & 'schtasks.exe' & ' /delete /tn ' & '"' & $aSchTaskNames[$TaskLoop] & '"' & ' /f ', '', @SW_HIDE)
;~  ConsoleWrite( $aSchTaskNames[$TaskLoop] & @CRLF)
    If @error < 0 Then
;~      ConsoleWrite( '<0 ' & @error & @CRLF)
        ContinueLoop
    ElseIf @error > 0 Then
;~      ConsoleWrite( '>0 ' & @error & @CRLF)
        FileWrite(@ScriptDir & '\errorlog.txt', @CRLF & @UserName & ' - ' & $aSchTaskNames[$TaskLoop]) ; create txt error log
        ContinueLoop
    EndIf
Next

I think the problem lies with the Runwait as it doesnt give a defined exit and it can often be 0 which doesnt help at all

Is there a way to solve this so the log only triggers when it removes something?

Share this post


Link to post
Share on other sites



I would use a two-step approach. First check for errors returned by RunWait. Then check the return value for the value that denotes a correct execution. This value is being taken from the array:
 

Global $aSchTaskNames[][] = [[2, 2], ["Task1", 0], ["DOK", 0]] ; First task should return value 0, the second 0
For $TaskLoop = 1 To $aSchTaskNames[0][0]
    Local $iSchTask = RunWait(@ComSpec & ' /c ' & 'schtasks.exe' & ' /delete /tn ' & '"' & $aSchTaskNames[$TaskLoop][0] & '"' & ' /f ', '', @SW_HIDE)
    If @error <> 0 Then
        FileWriteLine(@ScriptDir & '\errorlog.txt', @UserName & ' - ' & $aSchTaskNames[$TaskLoop][0] & ': RunWait encountered error ' & @error) ; RunWait encounterd an error
    ElseIf $iSchTask <> $aSchTaskNames[$TaskLoop][1] Then
        FileWriteLine(@ScriptDir & '\errorlog.txt', @UserName & ' - ' & $aSchTaskNames[$TaskLoop][0] & ': Execution returned unexpected return value of ' & $iSchTask) ; The task returned an unexpected return value
    EndIf
Next

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
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