我尝试了几个版本的Wscript exec方法。
如果我使用cmd.exe / C MyRequest,如果MyRequest失败,则执行不报告错误,但如果未使用cmd.exe / c则返回错误。
我当时认为cmd.exe报告了对MPyRequest的调用的返回码,但似乎没有。在这种情况下如何检索返回代码。 这是我的测试的简化版本(注释直接版本没有失败)
Environnement主要是Windows 7(通常没有其他系统,也许是XP)
' Missing.cmd does not exist to force the failure test
'version with cmd.exe (CmdDir content is a valid and working cmd.exe)
ExecCmd = CmdDir & " /c Missing.cmd 1"
' direct version
ExecCmd = "Missing.cmd 1"
Set objShell = CreateObject("WScript.Shell")
On Error Resume Next
Set oExec = objShell.exec( ExecCmd)
' -- Post treatment ---------------------------
If ( err.Number = 0) Then
If ( oExec.ExitCode = 0 ) Then
' No error
wscript.echo "Execution OK"
Else ' Exit with error
wscript.echo "Error :" & oExec.ExitCode
end if
Else ' error on exec itself
wscript.echo "Execution. Error on object at call: " & err.Number _
& " Source: " & Err.Source & " Desc: " & Err.Description
end if
解决方案基于您的所有回复(感谢所有人)[@Damien,@ Ekkehard.Horner,@ Hans Passant]
答案 0 :(得分:4)
您需要检查ExitCode属性。
答案 1 :(得分:3)
如果它实际上正在返回错误代码,那么您可能正在尽早处理exitcode。也许添加oexec.StdOut.ReadAll()
使其等到完成。
也许你可以添加类似的东西
....
....
Set oExec = objShell.exec(ExecCmd)
oExecResult = oexec.StdOut.ReadAll()
SessionExitCode = oexec.ExitCode
' -- Post treatment ---------------------------
If ( err.Number = 0) Then
If ( SessionExitCode = 0 ) Then
' No error
wscript.echo "Execution OK"
Else ' Exit with error
wscript.echo "Error :" & SessionExitCode
end if
Else ' error on exec itself
....
....