【发布时间】:2014-04-30 16:56:09
【问题描述】:
我有一个在 XP 机器上运行的计划作业。该作业有时会失败,退出代码不是“0”。我想编写一个可以在作业后面运行并获得“最后结果”的 vbscript。如果该结果不正确,那么我希望它向我发送一封电子邮件,通知我工作失败。
这可能吗?
【问题讨论】:
标签: windows batch-file vbscript scheduled-tasks
我有一个在 XP 机器上运行的计划作业。该作业有时会失败,退出代码不是“0”。我想编写一个可以在作业后面运行并获得“最后结果”的 vbscript。如果该结果不正确,那么我希望它向我发送一封电子邮件,通知我工作失败。
这可能吗?
【问题讨论】:
标签: windows batch-file vbscript scheduled-tasks
试试这个:代替您计划的作业,安排一个类似于以下的脚本:
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
在 strResult 变量中,您可以将所有信息收集到电子邮件...
【讨论】:
for /f "tokens=3 delims=: " %A in ('schtasks /query /tn alarm2 /v /fo list^|findstr /c:"Last Result"') do echo %A
给你最后的结果
这是通过COM访问TS的方法
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)
【讨论】: