Can I use vbscript to return the last result of a

2019-06-01 14:39发布

问题:

I have a scheduled job that runs on an XP machine. From time to time this job fails with an exit code other than "0". I want to write a vbscript that can run behind the job and get the "Last Result". If that result is not correct then I want it to kick of an email to me notifying me that the job failed.

Is this possible to do?

回答1:

Try this: instead of your scheduled job, schedule a script similar to:

 VB Script Document
    option explicit
    'On Error Resume Next
    On Error GoTo 0

    Dim strResult: strResult = Wscript.ScriptName & vbNewLine

    Dim WshShell, oExec, appExitCode, appExitString
    Set WshShell = CreateObject("WScript.Shell")

    ShowResult( "%comspec% /C dir *.*")
    ShowResult( "%comspec% /C dir*.*")
    ShowResult( "YourSchedApp")

    Wscript.Echo strResult
    Wscript.Quit

    Private Sub ShowResult( ByVal AppToRun)
    strResult = strResult & vbNewLine & AppToRun & vbNewLine
    On Error Resume Next
    Set oExec = WshShell.Exec( AppToRun)
    appExitCode = Err.Number
    If appExitCode = 0 Then
        Do While oExec.Status = 0
         WScript.Sleep 100
        Loop
        appExitCode = oExec.ExitCode
        appExitString = ""
    Else
        appExitString = "Error 0x" & Hex( appExitCode) & vbTab & Err.Description
        Err.Clear
    End If
    On Error GoTo 0
    strResult = strResult & "Exit code " & CStr( appExitCode) & vbNewLine & appExitString
    Set oExec = Nothing
    End Sub

in strResult variable you can collect all info to e-mail...



回答2:

for /f "tokens=3 delims=: " %A in ('schtasks /query /tn alarm2 /v /fo list^|findstr /c:"Last Result"') do echo %A

Gives you the last result

This is how to access the TS via COM

Set service = CreateObject("Schedule.Service")
call service.Connect("Serenity")

' Get the task folder that contains the tasks. 
Dim rootFolder
Set rootFolder = service.GetFolder("\")

Dim taskCollection
Set taskCollection = rootFolder.GetTasks(0)