There would be some situations where we need to call batch file from scripts. Here the script to call: INSTALL.bat;
- Copy the below code to Notepad (or equivalent text editor, like Notepad++ or other).
- Save the file as .VBS;
A small Addition
For the Run method:
It’s good to trap any potential errors into a variable and to tell the method not to execute the next line until the current process is complete.
errTrap = Shell.Run("INSTALL.BAT", 1, True)
This will return any error codes into errTrap. From there you can log the error or act on it accordingly in the script.
- "True" tells wscript/cscript to "Wait until this command is complete before running the next line." The "1" is just the window style. Not that important.
Whenever you are returning values you have to put the arguments into parenthesis.
An addition Script
Following is the template that not only return an exit code but also writes to the Event Log.
‘GET CURRENT FOLDER PATH
sLogHeader = "Install batch file"
path = FileSystemObject.GetParentFolderName(WScript.ScriptFullName)
If Right(path, 1) <> "\" Then path = path & "\"
errTrap = shell.Run(path, 1 ,True)
If (errTrap = 0) Or (errTrap = 3010) Then
‘WRITE EXIT CODE [0-success/3010-success&requires reboot] TO EVENTLOG
shell.LogEvent vbLogSuccess, sLogHeader & "Installation completed successfully." & VbCrLf & "Exit code: " & errTrap
‘WRITE EXIT CODE TO EVENTLOG
shell.LogEvent vbLogError, sLogHeader & "Installation failed." & VbCrLf & "Exit code: " & errTrap
Set FileSystemObject = Nothing
Set Shell = Nothing