简体   繁体   中英

Issue error checking a batch file run via VBScript

I am unsure why InstallResult always returns a 1 in my VBScript. I have put an echo in my batch file to confirm if I delete the source file before a copy it returns a 4 and that it returns nothing if it is successful. Any help would be appreciated. My files should do the following:

  1. Copy a script from a network share to the local machine.
  2. Run a batch file to install office (currently some test code for error checking). The batch file should run and post an error code on exit or a 0 if successful.
  3. Go back to the VBScript to error check and run another cleanup VBScript.

Here is my code:

Run install bat (VBScript)

Dim objshell, InstallResult

Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")

FSO.CopyFile "\\altirisdata\AssetMgmt\Tools\WSM\DeleteOffice13Package.vbs", "C:\source\DeleteOffice13Package.vbs"
'DeleteMS2013FilePath = objShell.run ("c:\source\DeleteOffice13Package.vbs", 0, True)

WScript.Sleep 3000

Set objShell = WScript.CreateObject("WScript.Shell")
InstallResult = objShell.run ("cscript.exe C:\source\Microsoft_Office_2013_01\install.bat", 0, True)

WScript.Echo InstallResult

If InstallResult <> 0 Then WScript.Echo "Unable to install Microsoft Office 2013. Please manually check the install results"

If InstallResult = 0 Then 
    DeleteDelScript = objShell.Run("cscript.exe c:\source\DeleteOffice13Package.vbs", 0, True)
End If

If DeleteDelScript = 0 Then 
    FSO.DeleteFile("C:\source\DeleteOffice13Package.vbs")
End If

Set FSO = nothing

WScript.Quit

install.bat

@echo off

xcopy "C:\source\test again\test.txt" "C:\Temp\Temp1\TempTest" /y

if %errorlevel% neq 0 (
    exit /b %errorlevel%
)

exit

You get a return value of 1, because you're trying to run a batch script with a VBScript interpreter:

InstallResult = objShell.run (" C:\sourc...l", 0, True)

Remove cscript.exe from the commandline, or replace it with %COMSPEC% /c :

InstallResult = objShell.run (" C:\sourc...l.bat", 0, True)

As a side note, you shouldn't need a condition in your batch script. Simply returning the errorlevel should suffice:

@echo off
xcopy "C:\source\test again\test.txt" "C:\Temp\Temp1\TempTest" /y
exit /b %errorlevel%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM