Video Screencast Help
Search Video Help Close Back
to help
New in the Rewards Catalog: Vouchers for "Symantec Technical Specialist" and "Symantec Certified Specialist" exams.

Calling a Batch File Through VB Script

Updated: 14 Apr 2008 | 8 comments
TilakGovind's picture
0 0 Votes
Login to vote

There would be some situations where we need to call Batch file from Scripts. Here the script to call INSTALL.bat

  1. Copy the below code to Notepad.
    Function Tilak()
    dim shell
    set shell=createobject("wscript.shell")
    shell.run "INSTALL.bat"
    set shell=nothing
    
    End Function
    
  2. Save the file as .VBS
  3. Then use this VBscript in Wise Package Studio's CustomActions pane.

Comments

PRASANNA KUMAR RAJA's picture
15
Apr
2008
0 Votes 0
Login to vote

Nice code

Nice code.....It really hepls ....

Thanks
Prasanna Kumar Raja
Sr.Application Packager
Anovatek

Prasanna Kumar Raja
Sr.Application Packager
Anovatek

Kinetic's picture
22
Apr
2008
0 Votes 0
Login to vote

a small addition

For the Run method:

shell.run "INSTALL.bat"

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.

Eshwar's picture
24
Nov
2008
4 Votes +4
Login to vote

Additional script

Whenever we are executing vbscripts using Altiris, it is a best practice to return Exit Codes/Return Codes to the Altiris. Following is the template that not only return an Exit code but also writes to the Event Log.

Note: Place the executable and script in one place

'-------------------------------------------------- 
'GET CURRENT FOLDER PATH
path = FileSystemObject.GetParentFolderName(WScript.ScriptFullName)
If Right(path, 1) <> "\" Then path = path & "\"
End If

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
	WScript.Quit (errTrap)

Else

       	'WRITE EXIT CODE TO EVENTLOG

shell.LogEvent vbLogError, sLogHeader & "Installation failed." & VbCrLf & "Exit code: " & errTrap 
       	WScript.Quit (errTrap)

End If


Set FileSystemObject = Nothing
Set Shell = Nothing
'--------------------------------------------------

I hope this may help.

Thanks,
Eshwar

Thanks,
Eshwar

Sidd's picture
24
Nov
2008
10 Votes +10
Login to vote

Need to define sLogHeader variable

One needs to define sLogHeader variable so that the hedder of the event log will written with this name, befor the following line in the above script
(shell.LogEvent vbLogSuccess, sLogHeader & "Installation completed successfully." & VbCrLf & "Exit code: " & errTrap).

Example
sLogHeader = "Install batch file"

Preferably sLogHeader name should be task name that you are running so that it will be essy identify in event log.

I hope this may help.

Thanks
Sid

riva11's picture
05
May
2008
2 Votes +2
Login to vote

Question about nothing ...

I found really helpful your tip, just a question about the command line :
[quote]set shell=nothing[/quote]
What is the meaning for this line in this case ?

Thanks,
Paolo

R-Vijay's picture
06
May
2008
0 Votes 0
Login to vote

Clears the memory allocated.

Its just to clear the cache memory which is being used by the variable shell. It frees the memory once the script completes its execution.

Cheers'
Vijay

Microsoft MVP [Setup-Deploy]
Weblog: www.msigeek.com

TilakGovind's picture
20
Apr
2010
1 Vote +1
Login to vote

how to run batch file in background

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing yes

Cheers Tillu

Juun's picture
23
Feb
2011
0 Votes 0
Login to vote

I am trying to package a .p12

I am trying to package a .p12 certificate.I tried to use capicom.dll and wrote the batch file.Called the batch file using the vbs u have given. But the certificate is not appearing in the certificate manager. I also tried using another vbs which installs the certificate to the certificate manager,but gives error 1720 at end of installation.Any suggestions where im goin wrong?

.bat file:

@echo off

echo Importing digital licence...

rem Define the file locations as environment variables

set DLLFILE="C:\Program Files\Microsoft CAPICOM 2.1.0.2 SDK\Lib\X86\capicom.dll"

set CSTOREFILE="C:\Program Files\Microsoft CAPICOM 2.1.0.2 SDK\Samples\vbs\CStore.vbs"

set CERTFILE="C:\Users\User\AppData\Local\Temp\MNPCertificate\Cert.p12"

set CERTPASS=****

rem Import the Cert.p12 certificate with the supplied password

cscript /nologo %CSTOREFILE% import %CERTFILE% %CERTPASS%

 

.vbs to call .bat from custom action:

Option Explicit

Dim objShell

Set objShell = CreateObject("WScript.Shell")

objShell.run "C:\path to file.bat"

WScript.Quit