2017-01-09 104 views
-1

这里就是我想要做:返回错误代码为VBScript

  1. 获得一个VBScript运行其他的VBScript。
  2. 获取第二个VBScript在完成时发布错误,如果成功为0,或者如果不是返回到原始脚本,则返回> 0,然后根据返回的错误代码工作。

卸载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 
+0

可能重复的[VBScript - 使用错误处理](http://stackoverflow.com/questions/157747/vbscript-using-error-handling) – Lankymart

回答

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 

不过,既然你说你要在错误和Err.Number的情况下,返回值> 0是一个无符号整数,它可能会被解释为正值或负值根据其实际价值,这样的事情可能是一个更好的选择:

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 
0

是的,你可以从你的第二个脚本如下返回退出代码以第一...

WScript.Quit(-1) 

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

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方法将返回