Video Screencast Help
Symantec to Separate Into Two Focused, Industry-Leading Technology Companies. Learn more.

VBScript to Install Application with Exit Code

Created: 27 Jan 2009 | 5 comments
Sidd's picture
+11 11 Votes
Login to vote

The following script will return the Exit Code of application installation (legasy setup application /Windows installer application).

'*******************************************

Dim oShell, MyApp, i  

Set oShell = CreateObject("WScript.Shell")

' MyApp is the application path with silent switch.
' Use appropriate silent switches as per the application

MyApp = """C:\MyApplication\Setup.exe"" /Q"

i = 0

i = oShell.Run(MyApp, 1 ,True)

WScript.Echo "Exit Code is: " & (i)

'Uncomment the following line of code while using this script in software delivery and comment the above line

'WScript.Quit(i)

Set oShell = Nothing

'*******************End Of Code**************

Thanks,
Sid

License: AJSL
By clicking the download link below, you agree to the terms and conditions in the Altiris Juice Software License
Support: User-contributed tools on the Juice are not supported by Altiris Technical Support. If you have questions about a tool, please communicate directly with the author by visiting their profile page and clicking the 'contact' tab.

Comments 5 CommentsJump to latest comment

Eshwar's picture

Cheers,

Eshwar

+2
Login to vote
TheSmiz's picture

Sid,

I was looking at your script and thinking about how we could add to it and make it even better.  We could add some command line argument checking so the file to execute is no longer needed to be hard coded in the script.  This makes for a great re-usable piece of code with no need to edit for each application.  Also, we could add a file.exists check to make sure the file is on the path we state before trying to execute.  If we don't make that check and the file is not there, the script will bail with error b/c we didn't use "On Error Resume Next".

I have not tested this, but think something like this could enhace the functionality of your current script:

******************************************
Dim oShell, MyApp, i 
Dim oFso, args, argCount

' get the command line arguments
Set Args = WScript.Arguments
argCount = Args.Count

' if we don't have at least on argument, bail with status of -1
If argCount < 1 Then
    WScript.Quit -1
End If

MyApp = args(0)

Set oShell = CreateObject("WScript.Shell")
Set oFso = CreateObject("Scripting.FileSystemObject")

' check to make sure the file exists, if not, bail
If oFso.FileExists(MyApp) Then
' MyApp is the application path with silent switch.
' Use appropriate silent switches as per the application
' add quotes around the app path and add the /q as a arg
MyApp = Chr(34) & MyApp & Chr(34) & " /Q"

i = 0

i = oShell.Run(MyApp, 1 ,True)

' clean up
Set oShell = Nothing
Set oFso = Nothing

WScript.Echo "Exit Code is: " & (i)
'Uncomment the following line of code while using this script in software delivery and comment the above line
'WScript.Quit(i)

Else ' file didn't exist, so we bail
' clean up
    Set oShell = Nothing
    Set oFso = Nothing
    WScript.Quit -1
End If
' end
******************************************

RS

+10
Login to vote
aspen's picture

The only function I would ask for is for it to spit out a log file detailing the errors it encountered to the install directory.

0
Login to vote
Eshwar's picture

aspen,
thats a good idea. And also you can just write an entry to the event viewer with error code.

Cheers,

Eshwar

0
Login to vote
TheSmiz's picture

I updated the one a little bit with a new sub and a new function.  One will log the status to both a log file and as an event to be viewed in event viewer.  The other gets the file name from te path you have passed in via command line and uses it for the log file name.  The log file is saved in the same location the script is launched from.  Should this be reposted as a new download?  Thoughts?

Otherwise, copy and paste into notepad and save with a .vbs extension.

'******************************************
Dim oShell, MyApp, i
Dim oFso, args, argCount
Dim strFileName

' get the command line arguments
Set Args = WScript.Arguments
argCount = Args.Count

' if we don't have at least on argument, bail with status of -1
If argCount < 1 Then
WScript.Quit -1
End If

MyApp = args(0)

Set oShell = CreateObject("WScript.Shell")
Set oFso = CreateObject("Scripting.FileSystemObject")
strFileName = GetFileFromPath(MyApp, "\")
' check to make sure the file exists, if not, bail
If oFso.FileExists(MyApp) Then
' MyApp is the application path with silent switch.
' Use appropriate silent switches as per the application
' add quotes around the app path and add the /q as a arg
LogInfo 0, strFileName & " - File Exists", strFileName & ".log"

MyApp = Chr(34) & MyApp & Chr(34) & " /Q"

i = 0

i = oShell.Run(MyApp, 1 ,True)
LogInfo 0, strFileName & " - Executed", strFileName & ".log"
' clean up
Set oShell = Nothing
Set oFso = Nothing

LogInfo 0, strFileName & " - exit code is " & i, strFileName & ".log"
If i <> 0 Then
      LogInfo 1, strFileName & " - exit code is " & i, strFileName & ".log"
      LogInfo 1, strFileName & " - " & Err.Description, strFileName & ".log"
End if
WScript.Echo "Exit Code is: " & (i)
'Uncomment the following line of code while using this script in software delivery and comment the above line
'WScript.Quit(i)
Else ' file didn't exist, so we bail
' clean up
LogInfo 1, strFileName & " - does not exist" & i, strFileName & ".log"
Set oShell = Nothing
Set oFso = Nothing
WScript.Quit -1
End If
' end

Private Function GetFileFromPath(line, Delimiter)
Dim i
Dim Count
Dim quote
Dim InQuotes
Dim rtrnarray()

quote = Chr(34)
InQuotes = False
ReDim rtrnarray(0)
Count = 0
For i = 1 To Len(line)
If Mid(line, i, 1) = quote Then
InQuotes = Not InQuotes
Else
If Mid(line, i, 1) = Delimiter And InQuotes = False Then
Count = Count + 1
ReDim Preserve rtrnarray(Count)
Else
rtrnarray(Count) = rtrnarray(Count) & Mid(line, i, 1)
End If
End If
Next
GetFileFromPath = rtrnarray(UBound(rtrnarray))
End Function

Sub LogInfo (intType, strMessage, strLfName)
' constants for the event log message.  Commented out since we are simply passing the int via the main sub.
' Const EVENT_SUCCESS = 0
' Const EVENT_ERROR = 1
' Const EVENT_WARNING = 2
' Const EVENT_INFORMATION = 4
' Const EVENT_AUDITSUCCESS = 8
' Const EVENT_AUDITFAILURE = 16
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim strLogFile
Dim objWsShell, objFso, objLogFile

Set objWsShell = CreateObject("Wscript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")

strLogFile = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName)) & strLfName
'wscript.Echo Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))

Set objLogFile = objFso.OpenTextFile(strLogFile, ForAppending, True)
objLogFile.WriteLine Now() & ": " & intType & " - " & strMessage
objWsShell.LogEvent intType, strMessage

Set objWsShell = Nothing
Set objLogFile = Nothing
Set objFso = Nothing
End Sub
******************************************

RS

+1
Login to vote