将错误代码返回给VBScript

时间:2017-01-09 12:16:10

标签: vbscript error-handling

以下是我要做的事情:

  1. 获取VBScript以运行另一个VBScript。
  2. 获取第二个VBScript在完成时发布错误,如果成功则为0,如果不回原始脚本则为> 0然后根据返回的错误代码处理条件。
  3. 卸载2010&复制办公室2013

    'Copy files from a network share to machine
    Set FSO = CreateObject("Scripting.FileSystemObject")
    WScript.Echo "Starting to uninstall Microsoft Office 2010 from the machine"
    
    FSO.CopyFile "\\data01\Tools\WSM\Copy_2013.vbs", "C:\temp\Copy_2013.vbs"
    FSO.CopyFile "\\data01\Tools\WSM\OffScrub10.vbs", "C:\Temp\OffScrub10.vbs"
    FSO.CopyFile "\\data01\Tools\WSM\DeleteOffice13Package.vbs", "C:\temp\DeleteOffice13Package.vbs"
    
    'Wait to execute rest of script where copied filed need to be in location
    WScript.Sleep 5000
    
    'Executes Office 2013 copy at the same time, do not wait to continue uninstalling office 2010
    Set objShell = WScript.CreateObject("WScript.Shell")
    Call objShell.Run("C:\temp\Copy_2013.vbs", 0, False)
    
    WScript.Sleep 3000
    
    'Run VBScript that uninstalls office 2010 (currently set to copy a non existent path for error capture test)
    strRemoveOffice10 = "c:\Temp\offscrub10.vbs ALL /Quiet /NoCancel"
    Call objShell.Run(strRemoveOffice10, 0, True)
    
    WScript.Echo Err.Number
    
    If Err.Number <> 0 Then  WScript.Echo " Microsoft Office 2010 could not be uninstalled. Please uninstall again manually."
    If Err.Number = 0 Then WScript.Echo "Microsoft Office 2010 has uninstalled from the machine"
    
    Set objFileSys = Nothing
    
    WScript.Quit
    

    OffScrub10.vbs

    Dim objFileSys
    
    Set objFileSys = CreateObject("Scripting.FileSystemObject")
    objFileSys.GetFolder("C:\Temp\Temp1\bla").Copy "C:\WSM\Test"
    
    On Error Resume Next
    
    If Err.Number <> 0 WScript.Quit Err
    

3 个答案:

答案 0 :(得分:2)

要启用错误处理,您需要在之前放置On Error Resume Next 可能导致错误的语句。然后您可以返回如下状态代码:

Set fso = CreateObject("Scripting.FileSystemObject")

On Error Resume Next
fso.GetFolder("C:\Temp\Temp1\bla").Copy "C:\WSM\Test"
WScript.Quit Err.Number

但是,因为你说你想要一个错误的返回值&gt; 0 Err.Number是一个无符号整数,可能会被解释为正值或负值,具体取决于它的实际值,像这样的东西可能是一个更好的选择:

Set fso = CreateObject("Scripting.FileSystemObject")

On Error Resume Next
fso.GetFolder("C:\Temp\Temp1\bla").Copy "C:\WSM\Test"
If Err Then WScript.Quit 1
WScript.Quit 0   'can be omitted, because it's the default

要检查调用脚本中的返回值,您需要在变量中捕获它。在使用Call语句时,就像在第一个脚本中一样,返回值就会被丢弃。 VBScript不会在Err对象中放置外部命令的返回值。您可能还希望确保使用cscript.exe运行脚本,以避免消息/弹出窗口阻止执行。

strRemoveOffice10 = "cscript.exe c:\Temp\offscrub10.vbs ALL /Quiet /NoCancel"
rc = objShell.Run(strRemoveOffice10, 0, True)
If rc = 0 Then
  'OK
Else
  'an error occurred
End If

答案 1 :(得分:0)

是的,您可以将第二个脚本的退出代码返回到第一个脚本,如下所示......

    WScript.Quit(-1)

其中-1是您选择的退出代码。

答案 2 :(得分:0)

Option Explicit

    If WScript.Arguments.Count = 0 Then 
        ' If we don't have any arguments, call ourselves to retrieve 
        ' the exit code. It will be returned by the call to the 
        ' Run method
        Dim returnCode
        returnCode = WScript.CreateObject("WScript.Shell").Run ( _ 
            Chr(34) & WScript.ScriptFullName & Chr(34) & " myArgument " _ 
            , 0 _ 
            , True _ 
        )
        ' Note ^ the "True" 
        ' We need to wait for the "subprocess" to end to retrieve the exit code

        Call WScript.Echo(CStr( returnCode ))

    Else 

        ' We have arguments, leave current process with exit code
        Call WScript.Quit( 1234 )
    End If 

快速测试样本。

需要考虑两个因素:

  • 被调用的子进程使用WScript.Quit方法将进程退出代码返回给调用者
  • 调用者必须等待子进程结束才能检索退出代码。 Run方法将返回子流程
  • 的退出代码