Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: need a vbscript to install a .msi software package?

Status
Not open for further replies.

Teknoratti

Technical User
Aug 11, 2005
183
US
I'm looking for help to create a vbscript to install a .msi software package. Here is my goal:

I want to place a .msi file on a network folder, i.e \\computerA\Package\software.msi

The .vbs file will be on another network drive folder. Basically any user who executes this .vbs file will have the .msi file installed on their local computer.

The software will be installed in the default location of c:\program files, and there will be no user interaction. Once the software is install. I also want to append the following script as the second step in the process, basically it will be a two part script: The first part will install the software, the second part will change a few registry settings which are listed below.

Option Explicit
Dim sName
Dim objReg
Dim strKeyPath
Dim ValueName
Dim strValue
Const HKEY_LOCAL_MACHINE = &H80000002
sName = InputBox("Input Computer Name to Remove Symantec Password")
Set objReg = GetObject("winmgmts:\\" & sName & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\INTEL\LANDesk\VirusProtect6\CurrentVersion\AdministratorOnly\Security"
ValueName = "LockUnloadServices"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
End If
ValueName = "UseVPUninstallPassword"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
End If

After both scripts run I want the computer to reboot. Can anyone help?

Thanks
 
Below snippit will install the program silently.

Code:
strRem2 = "\\servername\sharename\program.msi /qn"
intError = 0
	Set objProcess = objWMIService.Get("Win32_Process")
	Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance_
	objProgram.CommandLine = strRem2
	Set strShell = objWMIService.ExecMethod("Win32_Process","Create",objProgram)
	WScript.Echo Now & " " & strPCName & " start installer intError reports as " & intError
	If Not Err = 0 Then
	    LogFile.WriteLine "," & Now & ",Error: Could not start Installer on," & strPCName
	Else
		LogFile.WriteLine "," & Now & ",Run Tuner Installer Started on," & strPCName
	'	WScript.Sleep(JobSleep)
	End If  'intError = 0

and this one will reboot either windows 2000 or XP

Code:
Function Reboot(strPCName)
On Error Resume Next
WScript.Echo "Funtion Reboot has been called"

Dim objWMIService, intError, objProcess, strShell, objProgram
Dim strOS, colOperatingSystems, ObjIperatingSystem

strRebootxp = "shutdown -r -f"
strReboot2k = "shutdown /l /r /y /c"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPCName & "\root\cimv2")
WScript.Echo "Error level is " & err & " " & err.description
	Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
		For Each objOperatingSystem in colOperatingSystems
		strOS = objOperatingSystem.Caption
		Next
If strOS = "Microsoft Windows XP Professional" Then
		WScript.Echo strPCName & " " & strOS
			'Block to execute Rebootxp
			intError = 0
			Set objProcess = objWMIService.Get("Win32_Process")
			Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance_
			objProgram.CommandLine = strRebootxp
			intError = objWMIService.ExecMethod("Win32_Process","Create",objProgram)
			WScript.Echo Now & " " & strPCName & " Rebootxp intError reports as " & intError
			If intError <> 0 Then
    			LogFile.WriteLine strPCName & "," & err.description
			Else
				LogFile.WriteLine Now & "," & strPCName & ",Rebootxp Started"
				WScript.Sleep(JobSleep)
			End If  'intError = 0
		Elseif strOS = "Microsoft Windows 2000 Professional" Then
			'Block to execute Reboot2k
			intError = 0
			Set objProcess = objWMIService.Get("Win32_Process")
			Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance_
			objProgram.CommandLine = strReboot2k
			intError = objWMIService.ExecMethod("Win32_Process","Create",objProgram)
			WScript.Echo Now & " " & strPCName & " Reboot2k intError reports as " & intError
			If intError <> 0 Then
    			LogFile.WriteLine strPCName & "," & err.description
			Else
				LogFile.WriteLine Now & "," & strPCName & ",Reboot2k Started"
				WScript.Sleep(JobSleep)
			End If  'intError = 0
		Else
			LogFile.WriteLine now & "," & strPCName & ",No matching OS," & err.description
		End If 'strOS
		Set objWMIService = Nothing

End Function 'reboot(strPCName)

Hope that helps some...
 
We only have Professional versions of the OS, but if you wanted to check for any Xp or 2000, then you could use

Code:
OSFind = instr(strOS,"xp",1)
if OSFind <> 0 and OSFind <> "" Then
 (( code to reboot xp ))
else
(( code to reboot 2000 ))

small letters and the ",1" so that it is not case sensitive...
 
BKearan,

I got a script error on line 3 character 5
error: object required: 'objWMIService'

The script you posted is below. I changed the install path to reflect where the .msi file was located.

strRem2 = "\\Corona\VPHOME\CLT-INST\WIN32\Symantec AntiVirus.msi /qn"
intError = 0
Set objProcess = objWMIService.Get("Win32_Process")
Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance_
objProgram.CommandLine = strRem2
Set strShell = objWMIService.ExecMethod("Win32_Process","Create",objProgram)
WScript.Echo Now & " " & strPCName & " start installer intError reports as " & intError
If Not Err = 0 Then
LogFile.WriteLine "," & Now & ",Error: Could not start Installer on," & strPCName
Else
LogFile.WriteLine "," & Now & ",Run Tuner Installer Started on," & strPCName
' WScript.Sleep(JobSleep)
End If 'intError = 0



 
Sorry, missed an important part of the snippit...
Put this below the "intError = 0" line.

Code:
Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPCName & "\root\cimv2")

Those are snippits (just pieces of a script "snipped" out of the whole), so constructing a full script will take some 'stitching' and tweaking. Like, you used sName for the computer name and my snippit used strPCName... I used logfile.writeline to output to a log file, but you can take it out or you need to initialize and define the logfile.
 
BKearan, please understand, I am a complete noob and know nothing about vbscripting. If you can explain the script it would be appreciated. Needless to say, I added the line like you said but still got a vb error. Here is the script including the line you added.

strRem2 = "\\corona\VPHOME\CLT-INST\WIN32\Symantec AntiVirus.msi /qn"
intError = 0
Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"
& strPCName & "\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")
Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance_
objProgram.CommandLine = strRem2
Set strShell = objWMIService.ExecMethod("Win32_Process","Create",objProgram)
WScript.Echo Now & " " & strPCName & " start installer intError reports as " & intError
If Not Err = 0 Then
LogFile.WriteLine "," & Now & ",Error: Could not start Installer on," & strPCName
Else
LogFile.WriteLine "," & Now & ",Run Tuner Installer Started on," & strPCName
' WScript.Sleep(JobSleep)
End If 'intError = 0
 
'got this off the internet, not sure what is wrong with good old fashioned WshShell.Run msiexec.exe /i & strCurrentfolder & \..,.,


Install Software on a Local Computer
Const ALL_USERS = True

Set objService = GetObject("winmgmts:")
Set objSoftware = objService.Get("Win32_Product")
errReturn = objSoftware.Install("c:\scripts\database.msi", , ALL_USERS)




Install Software on a Remote Computer

Installs a hypothetical software program (using a Windows Installer package) on a remote computer. Requires delegation for the computer and user accounts involved in the procedure.

Const wbemImpersonationLevelDelegate = 4

Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objConnection = objwbemLocator.ConnectServer _
("WebServer", "root\cimv2", "fabrikam\administrator", _
"password", , "kerberos:WebServer")
objConnection.Security_.ImpersonationLevel = wbemImpersonationLevelDelegate

Set objSoftware = objConnection.Get("Win32_Product")
errReturn = objSoftware.Install("\\atl-dc-02\scripts\1561_lab.msi",,True)

 
Here is a full script that takes a computer name from targets.txt (which should be in the same folder) and runs your command line.

Then it will wait.

Then it will remove the password using your code to edit the registry.

Wait.

Then it will reboot the computer - if it is windows 2000 or xp.

Then it will go to the next computer name in targets.txt (each name should be the only thing on the line)

It creates a report called Results.csv that tells you what happened on each computer.

You should run it with cscript.exe (instead of the defaults wscript.exe). You can do an right click, "Open With", browse to \windowsfolder\system32\cscript.exe and open the .vbs with cscript. Keeps you from having to click through a TON of "OK"s

Code:
Option Explicit


'----- Customize the following constants for your needs -----
Const HostList = "targets.txt"
Const ResultsFile = "Results.csv"
Const ScriptVersion = 1.0
Const JobSleep = 300000				'Time to sleep between jobs, in milliseconds



'----- Constant Declarations -----
Const ForReading = 1
Const ForWriting = 2
Dim sName
Dim objReg
Dim strKeyPath
Dim ValueName
Dim strValue
Const HKEY_LOCAL_MACHINE = &H80000002

Dim objFSO, strFileEntry, strPCName, wshShell, intRC, intError
Dim ListFile, LogFile, strPCDN, strRem1, strRem2, strRem3, strRem4, strRem5, strRem6
Dim objWMIService, intError, objProcess, strShell, objProgram
Dim strOS, colOperatingSystems, ObjIperatingSystem
Dim strRebootxp, strReboot2k


'----- Script Banner -----
WScript.Echo
WScript.Echo ("********************************************************")
WScript.Echo
WScript.Echo "Install Symantec Antivirus Script version " & ScriptVersion
WScript.Echo "Starting Timestamp: " & Now
WScript.Echo



Set objFSO = CreateObject("Scripting.FileSystemObject")  

If (objFSO.FileExists(HostList)) Then 
    Set ListFile = objFSO.OpenTextFile(HostList, ForReading)
Else 
    wscript.echo ("ERROR:  Unable to find input file " & HostList & ".")
    wscript.echo ("        Please make sure that the file exists and your user")
    wscript.echo ("        account has rights to access it.")
    Set objFSO = Nothing
    wscript.quit(2)
    
End If '  (objFSO.FileExists(HostList))

Set LogFile = objFSO.OpenTextFile(ResultsFile, ForWriting, True)

If Err.Number <> 0 Then
    wscript.echo ("ERROR:  Unable to initialize requested log file, " & ResultsFile & ".")
    wscript.echo ("        Please make sure that the path is valid.")
    WScript.Echo
    WScript.Echo ("        Error Code was " & Err.Number & " with description: " & Err.Description & ".")
    ListFile.Close
    Set objFSO = Nothing
    wscript.quit(2)
End If 'Err.Number <> 0




Do while ListFile.AtEndOfStream <> True
	strFileEntry = ListFile.ReadLine
	strPCName = Trim(strFileEntry)
	If strPcName <> "" Then
		InstallAntiVirus(strPCName)
	End If  'strPcName <> "" 
Loop  'while ListFile.AtEndOfStream <> True	                        


LogFile.Close
ListFile.Close

set LogFile = Nothing
Set ListFile = Nothing
Set objFSO = Nothing

WScript.Echo 
WScript.Echo "Ending Timestamp: " & Now
WScript.Echo ("********************************************************")
WScript.Echo
WScript.Quit

'*******************************************************************
Function InstallAntiVirus(strPCName)
WScript.Echo "Funtion InstallAntiVirus has been called"

'  Based on code found at MS Scripting Site:
'  [URL unfurl="true"]http://www.microsoft.com/technet/scriptcenter/scripts/os/process/procthd/pcthvb03.mspx?mfr=True[/URL]
'  and 
'  [URL unfurl="true"]http://www.computerperformance.co.uk/vbscript/wmi_process_start.htm[/URL]

'  WMI Win32_Process info
'  [URL unfurl="true"]http://msdn2.microsoft.com/en-us/library/aa389388.aspx[/URL]

Dim objWMIService, intError, objProcess, strShell, objProgram, OSFind, OSFind2

strRem2 = "\\corona\VPHOME\CLT-INST\WIN32\Symantec AntiVirus.msi /qn"

On Error Resume Next
Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPCName & "\root\cimv2")
WScript.Echo "Error level is " & err & " " & err.description
If err = "429" Then 
	Wscript.Echo "Can't Create " & strPCName
	LogFile.WriteLine strPCName & ",Can't start Copy"
Elseif err = "462" Then
	WScript.Echo "Computer not Responding."
	LogFile.WriteLine strPCName & ",PC not Responding to ping"
Elseif err <> 0 Then
	LogFile.WriteLine err & " " & err.description
	LogFile.WriteLine strPCName & ",Check for errors/"
Else

'Block to execute Rem2
    Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"
    & strPCName & "\root\cimv2")
    Set objProcess = objWMIService.Get("Win32_Process")
    Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance_
    objProgram.CommandLine = strRem2
    Set strShell = objWMIService.ExecMethod("Win32_Process","Create",objProgram)
    WScript.Echo Now & " " & strPCName & " start installer intError reports as " & intError
    If Not Err = 0 Then
        LogFile.WriteLine "," & Now & ",Error: Could not start Installer on," & strPCName
    Else
        LogFile.WriteLine "," & Now & ",Antivirus Installer Started on," & strPCName
    WScript.Sleep(JobSleep)
    End If  'Not Err = 0

' Your Password removal code
sName = strPCName
Set objReg = GetObject("winmgmts:\\" & sName & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\INTEL\LANDesk\VirusProtect6\CurrentVersion\AdministratorOnly\Security"
ValueName = "LockUnloadServices"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
End If
ValueName = "UseVPUninstallPassword"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue
If strValue=1 Then
objReg.setDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, 0
End If
WScript.Sleep(JobSleep)
' End Password Removal Code

'Reboot Windows 2000 or XP
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPCName & "\root\cimv2")
WScript.Echo "Error level is " & err & " " & err.description
	Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
		For Each objOperatingSystem in colOperatingSystems
		strOS = objOperatingSystem.Caption
		Next
		OSFind = instr(strOS,"xp",1)
		OSFind2 = instr(strOS,"2000",1)
If OSFind <> 0 and OSFind <> "" Then
		WScript.Echo strPCName & " " & strOS
			'Block to execute Rebootxp
			intError = 0
			Set objProcess = objWMIService.Get("Win32_Process")
			Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance_
			objProgram.CommandLine = strRebootxp
			intError = objWMIService.ExecMethod("Win32_Process","Create",objProgram)
			WScript.Echo Now & " " & strPCName & " Rebootxp intError reports as " & intError
			If intError <> 0 Then
    			LogFile.WriteLine strPCName & "," & err.description
			Else
				LogFile.WriteLine Now & "," & strPCName & ",Rebootxp Started"
				WScript.Sleep(JobSleep)
			End If  'intError = 0
		Elseif OSFind2 <> 0 and OSFind <> "" Then
			'Block to execute Reboot2k
			intError = 0
			Set objProcess = objWMIService.Get("Win32_Process")
			Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance_
			objProgram.CommandLine = strReboot2k
			intError = objWMIService.ExecMethod("Win32_Process","Create",objProgram)
			WScript.Echo Now & " " & strPCName & " Reboot2k intError reports as " & intError
			If intError <> 0 Then
    			LogFile.WriteLine strPCName & "," & err.description
			Else
				LogFile.WriteLine Now & "," & strPCName & ",Reboot2k Started"
				WScript.Sleep(JobSleep)
			End If  'intError = 0
		Else
			LogFile.WriteLine now & "," & strPCName & ",No matching OS," & err.description
		End If 'strOS
'End Reboot code


Set objWMIService = Nothing
End If
End Function  'InstallAntiVirus(strPCName)

*note: not thoroughly tested with specified commands*
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top