Tuesday, 14 October 2014

VBScript to Display Progress Messages

I got an application which was installing very silent without any progress messages. But, the customer required some progress message to be displayed to the users. Hence, I wanted to accomplish this requirement somehow. My first choice was AutoIt. But, VBScript is the simple way.

Thought might be useful to others.

Here we go.

Option Explicit

'----------------------------------------------------------------------------------
'Global Script Variables
'----------------------------------------------------------------------------------

Dim strComputer, ObjShell, intTime, strPopUp, iReturn,strMessage,strProcessExe
Dim i,objExplorer,m,strPathLen,strScriptPath, strCmdLine

'----------------------------------------------------------------------------------
'Global Script Constants
'----------------------------------------------------------------------------------

'----------------------------------------------------------------------------------
'variable initialization
'----------------------------------------------------------------------------------
strComputer = "."
Set ObjShell = CreateObject("WScript.Shell")

'----------------------------------------------------------------------------------
'Start of Script (MAIN)
'----------------------------------------------------------------------------------

Set objExplorer = CreateObject _
    ("InternetExplorer.Application")

strPathLen = Len(Wscript.ScriptFullName) - Len(Wscript.ScriptName)
strScriptPath = Left(Wscript.ScriptFullName, strPathLen)

If (Right(strScriptPath, 1) <> "\") Then
strScriptPath = strScriptPath & "\"
End If

objExplorer.Navigate "about:blank"  
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Left = 200
objExplorer.Top = 200
objExplorer.Width = 350
objExplorer.Height = 150
objExplorer.Visible = 1  
objExplorer.Document.Body.Style.Cursor = "wait"
objExplorer.Document.Title = "CRM Outlook"

i = 1
m = 0

strCmdLine = chr(34) & strScriptPath & "application1.exe" & chr(34)
strMessage="Installing application1"
strProcessExe="application1.exe"
iReturn= InstallWithProgressMessage(strCmdLine, strMessage, strProcessExe)

If iReturn = 0 then
strCmdLine = chr(34) & strScriptPath & "application2.exe" & chr(34)
strMessage="Installing application2"
strProcessExe="application2.exe"
iReturn= InstallWithProgressMessage(strCmdLine, strMessage, strProcessExe)
End If

If iReturn = 0 then
strCmdLine = chr(34) & strScriptPath & "application3.exe" & chr(34)
strMessage="Installing application3"
strProcessExe="application3.exe"
iReturn= InstallWithProgressMessage(strCmdLine, strMessage, strProcessExe)
End If

objExplorer.Quit

wscript.quit iReturn

'Functions

'----------------------------------------------------------------------------------
' Function To Check Process Running
'----------------------------------------------------------------------------------
Function IsProcessRunning(strProcess)
On Error Resume Next
Dim ObjWMIService, ObjProcessList, ObjProcess

Set ObjWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set ObjProcessList = ObjWMIService.ExecQuery("Select * from Win32_Process Where Name = '"& strProcess &"'")

For Each ObjProcess In ObjProcessList
IsProcessRunning = True
Exit Function
Next
IsProcessRunning = False
End Function

'----------------------------------------------------------------------------------
' Function To Install with Progress Message
'----------------------------------------------------------------------------------
Function InstallWithProgressMessage(strCmdLine, strMessage, strProcessExe)
InstallWithProgressMessage= objShell.Run(strCmdLine, 0, FALSE)
Do while IsProcessRunning(strProcessExe)
objExplorer.Document.Body.InnerHTML = strMessage & "...<br/><br/>Lapsed Time: " & m & " Mins & " & i & " Seconds</a>"
WScript.Sleep 500
objExplorer.Document.Body.InnerHTML = strMessage & "....<br/><br/>Lapsed Time: " & m & " Mins & " & i & " Seconds</a>"
i = i + 1
If i = 60 Then
m = m + 1
i=0
End If
WScript.Sleep 500
objExplorer.Document.Body.InnerHTML = strMessage & "....<br/><br/>Lapsed Time: " & m & " Mins & " & i & " Seconds</a>"
Loop
End Function

Content Disclaimer: Use my blog at your own risk. I’m not responsible for your use of the information contained in or linked from these web pages. This information is being given to you gratuitously and there is no agreement or understanding between you and me regarding your use.

No comments:

Post a Comment