This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'On error resume next | |
'**************************************************************************************** | |
' This is a template file used for Installtion of multiple msi's kept in various folder | |
' USAGE : Please Use the folderNames and 00_ , 01_ , 02_ ….and so on as prefix | |
' PLEASE UN-COMMENT Line#75 for actual Installtion. This is commented for a safety | |
' AUTHOR : Vikas Pandey | |
'**************************************************************************************** | |
'*** SET DEBUG | |
Dim Debug : Debug = False | |
Dim Logging : Logging = False 'Please set the Path below while Making it true | |
Dim strLogPath : strLogPath = "C:\windows\Temp" ' Default Location | |
'*** OBJECTS AND VARIABLES | |
Dim objShell : Set objShell = CreateObject("WScript.Shell") | |
Dim objFSO : Set objFSO = CreateObject("Scripting.Filesystemobject") | |
Dim objWindowsInstaller : Set objWindowsInstaller = CreateObject("WindowsInstaller.Installer") | |
Dim objInstallProducts : Set objInstallProducts = CreateObject("Scripting.Dictionary") | |
Dim ScriptPath : ScriptPath = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName)-Len(WScript.ScriptName))) | |
Dim Product | |
'*** Installation from each folder | |
Dim colFolder : Set colFolder = objFSO.GetFolder(ScriptPath).SubFolders | |
For Each folder In colFolder | |
Dim strInstallFolderName :strInstallFolderName = ScriptPath & folder.name | |
Dim strMsiName :strMsiName = ReturnMsiNameInFolder(strInstallFolderName) | |
Dim strMstName :strMstName = ReturnMstNameInFolder(strInstallFolderName) | |
If Not strMsiName = "" Then | |
strProductcode = GetMSIProductCode(strInstallFolderName & "\" & strMsiName ) | |
If strMstName = "" Then | |
InstallProductMsiMst strProductcode, _ | |
Chr(34) & strInstallFolderName & "\" & strMsiName & Chr(34) | |
Else | |
InstallProductMsiMst strProductcode, _ | |
Chr(34) & strInstallFolderName & "\" & strMsiName & Chr(34) &_ | |
" TRANSFORMS=" & Chr(34) & strMstName & Chr(34) | |
End If | |
Else | |
If Debug Then WScript.Echo("FolderName : " & folder.name & vbnewline & "Does not contain valid MSI file") | |
If Logging Then WriteToFile strLogPath & "\InstallTemplate.log", "FolderName : " & folder.name & " Does not contain valid MSI file" | |
End If | |
Next | |
'*** FUNCTIONS & SUBs | |
Function InstallProductMsiMst(strProductcode, strInstallString) | |
'*** CONSTANTS | |
Const msiInstallStateNotUsed = -7 | |
Const msiInstallStateBadConfig = -6 | |
Const msiInstallStateIncomplete = -5 | |
Const msiInstallStateSourceAbsent = -4 | |
Const msiInstallStateInvalidArg = -2 | |
Const msiInstallStateUnknown = -1 | |
Const msiInstallStateBroken = 0 | |
Const msiInstallStateAdvertised = 1 | |
Const msiInstallStateRemoved = 1 | |
Const msiInstallStateAbsent = 2 | |
Const msiInstallStateLocal = 3 | |
Const msiInstallStateSource = 4 | |
Const msiInstallStateDefault = 5 | |
Const ReinstallModeFileOlderVersion = 4 | |
If objWindowsInstaller.Productstate(strProductcode) <> msiInstallStateDefault Then | |
If Debug Then WScript.Echo "Installing:" & VbCrLf & strProductcode | |
If Logging Then WriteToFile strLogPath & "\InstallTemplate.log", "Installing:" & strInstallString | |
'objShell.Run "msiexec -i " & strInstallString & " ALLUSERS=1 REBOOT=r -qb!", 1, True | |
End If | |
End Function | |
Function ReturnMsiNameInFolder(strFolderPath) | |
''************************************************* | |
' Function to return the msi name exists in a folder | |
' returns a null or blank string if no file found | |
' returns a collection if mutiple file exists | |
'************************************************* | |
'Object and Variables | |
Dim objFSO : Set objFSO = CreateObject("Scripting.Filesystemobject") | |
Dim ScriptPath : ScriptPath = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName)-Len(WScript.ScriptName))) | |
Dim colFiles : Set colFiles = objFSO.GetFolder(strFolderPath).Files | |
ReturnMsiNameInFolder = "" | |
For Each file In objFSO.GetFolder(strFolderPath).Files | |
If Right(file.Name, 4) = ".msi" Then | |
ReturnMsiNameInFolder = file.Name | |
End If | |
Next | |
Set objFSO = Nothing | |
End Function | |
Function ReturnMstNameInFolder(strFolderPath) | |
''************************************************* | |
' Function to return the msi name exists in a folder | |
' returns a null or blank string if no file found | |
' returns a collection if mutiple file exists | |
'************************************************* | |
'Object and Variables | |
Dim objFSO : Set objFSO = CreateObject("Scripting.Filesystemobject") | |
Dim ScriptPath : ScriptPath = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName)-Len(WScript.ScriptName))) | |
Dim colFiles : Set colFiles = objFSO.GetFolder(strFolderPath).Files | |
ReturnMstNameInFolder = "" | |
For Each file In objFSO.GetFolder(strFolderPath).Files | |
If Right(file.Name, 4) = ".mst" Then | |
ReturnMstNameInFolder = file.Name | |
End If | |
Next | |
Set objFSO = Nothing | |
End Function | |
Function GetMSIProductCode(msi) | |
''************************************************* | |
' Return the Product Code from a given msi | |
'************************************************* | |
On Error Resume Next | |
GetMSIProductCode="" | |
If msi = "" Then Exit Function End If | |
Dim FS, TS, WI, DB, View, Rec | |
Set WI = CreateObject("WindowsInstaller.Installer") | |
Set DB = WI.OpenDatabase(msi,2) | |
If Err.number Then Exit Function End If | |
Set View = DB.OpenView("Select `Value` From Property WHERE `Property` ='ProductCode'") | |
View.Execute | |
Set Rec = View.Fetch | |
If Not Rec Is Nothing Then | |
GetMSIProductCode=Rec.StringData(1) | |
End If | |
End Function | |
Sub WriteToFile(strFilePath, strMessage) | |
On Error Resume Next | |
''************************************************** | |
' Function to write messages to file name specified | |
' it puts time stamp as well to be sutable for logs | |
' Creates file in not exists | |
'*************************************************** | |
'Object and Variables | |
Const ForReading = 1 'Open a file for reading only. You can't write to this file. | |
Const ForWriting = 2 'Open a file for writing. | |
Const ForAppending = 8 'Open a file and write to the end of the file. | |
Dim objFSO : Set objFSO = CreateObject("Scripting.Filesystemobject") | |
If Not objFSO.FileExists(strFilePath) Then objFSO.CreateTextFile(strFilePath) | |
Set objFileText = objFSO.OpenTextFile(strFilePath, ForAppending) | |
strMessage = "[" & Date()& Chr(32) & Time() & "]" & Chr(32) & strMessage | |
objFileText.WriteLine(strMessage) | |
Set objFileText = Nothing | |
Set objFSO = Nothing | |
End Sub |