Running a VBS within a msi setup

2019-08-12 05:38发布

问题:

I am trying to create a setup for an application that I'm developing using the Visual Studio 2010 setup. One of the things I need to do is run some exe programs. I am using a custome action to run a VBS. This the method that im using to execute:

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute """c:\prog.exe""","-parm bla" ,"","","" 

The problem with this is that I cant wait for the program to finish using this method. So I tried using this method:

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "c:\prog.exe -parm bla",1,True

But is seems that when the MSI runs the script is dosnt have the WScript object. So my question is can i somehow get acess to the WScript object from the MSI or is there some better way to do this?

回答1:

Yes you cannot use WScript object in scripts that are called by MSI. As a workaround what you can do is create a new custom action with Action = NewAction, type =38, Source = (blank) TArget = add the vb script file as TARGET by running the following commands CScript WiTextIn.vbs mymsi.msi CustomAction NewAction Target YourVBscript.vbs.

WiTextIn file is located in C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\msi\scripts

(PS: When you try to run VBScript it might fail because vbscripts are disabled and you might have to delete the key from registry and enable vbscript)



回答2:

Indeed, Windows Installer does not support WScript objects directly. Have you tried to use the "CreateObject" function directly?

Set objShell = CreateObject("WScript.Shell")



回答3:

This is what I did in my vbs script to open a firewall exception for the service. I couldn't use the standard interactive pop-up for a service (that asks for permission to open the firewall), since it doesn't have a UI.

set oShell  = CreateObject("WScript.shell")
oShell.run "cmd /C netsh advfirewall firewall add rule program=""C:\Program Files (x86)\foo\bar\prog.exe"" name=""my-service"" dir=in action=allow"  

I added this vbs script to the "Commit" CustomAction of the Setup&Deployment Project, leaving the properties as defaults.

To debug problems with the vbs stage, I ran the msi from DOS using

msiexec /i mysetup.msi /L* install.log

Note that I originally used "Wscript.CreateObject" but that failed. This worked.