0

Here is what I am trying to do:

  1. Get a VBScript to run another VBScript.
  2. get the second VBScript to post an error on completion, either 0 if successful or >0 if not back to the original script and then work on conditions Based on the error code returned.

Uninstall 2010 & copy office 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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
V Krishan
  • 153
  • 3
  • 15
  • Possible duplicate of [VBScript -- Using error handling](http://stackoverflow.com/questions/157747/vbscript-using-error-handling) – user692942 Jan 10 '17 at 02:51

3 Answers3

2

To enable error handling you need to put On Error Resume Next before the statement that may cause an error. Then you can return a status code like this:

Set fso = CreateObject("Scripting.FileSystemObject")

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

However, since you said you want a return value >0 in case of an error and Err.Number is an unsigned integer that might be interpreted as a positive or negative value depending on its actual value, something like this might be a better choice:

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

To check the returned value in the calling script you need to capture it in a variable. When using the Call statement like you do in your first script the return value is simply discarded. VBScript does not put return values of external commands in the Err object. You may also want to make sure that your script is being run with cscript.exe to avoid messages/popups blocking execution.

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

Yes, you can return an exit code from your second script to the first as follows...

    WScript.Quit(-1)

Where -1 is your exit code of choice.

Nick Allan
  • 387
  • 4
  • 10
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 

Quick sample for testing.

There are two elements to consider:

  • The called subprocess uses the WScript.Quit method to return the process exit code to the caller
  • The caller must wait for the subprocess to end to retrieve the exit code. The Run method will return the exit code of the subprocess
MC ND
  • 69,615
  • 8
  • 84
  • 126