Running a VBS within a msi setup - visual-studio-2010

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?

Indeed, Windows Installer does not support WScript objects directly. Have you tried to use the "CreateObject" function directly?
Set objShell = CreateObject("WScript.Shell")

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)

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.

Related

vbs sometimes hangs during executing

I have vbs file myscript.vbs
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile="c:\temp\txt.txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "test" & vbCrLf
objFile.Close
Set objFSO=Nothing
when i executing it on Windows 7 x64 by calling
cscript //B //NOLOGO "myscript.vbs"
sometimes it not executing and in task manager there is cscript.exe proccess that does not terminate
also i try add //T:2 or use wscript but it behaves the same
this happens with other vbs scripts too
sfc /scannow found no errors
how can this be resolved?
this is problem of windows. it is tests with new windows image on virual (no antivirus and some other thing). Create new image of virtual windows and it is work great. windows for some "skynet" reason behaves like this

Cannot run vb6/make command in vbscript

My script contains the following code.
Set objShell=CreateObject("WScript.Shell")
ObjShell.Run "vb6/make Project1.vbp Project1.exe"
But my script doesn't make my project compile into exe.
I have the visual basic project Project1.vbp in the same folder which contains the script and the path for vb6 is also set in the Environment variable.
However when I tried to compile the project manually into exe it worked fine, by the command given below
vb6/make Project1.vbp Project1.exe
Kindly suggest some way to resolve my script's problem.
Always use full paths in sutch cases, I presume the error is there.
You can catch the error produced in the following way.
Here with a Ruby script I want to run but deliberatly made a mistake in the path.
Set objShell = CreateObject("WScript.Shell")
command = "cmd.exe /S /C ruby ""C:\Users\Gebruiker\ruby\excel\ru.rb"""
Set objWshScriptExec = objShell.Exec(command)
Set objStdOut = objWshScriptExec.StdOut
Set objStdErr = objWshScriptExec.StdErr
While Not objStdOut.AtEndOfStream
WScript.echo objStdOut.ReadLine
Wend
While Not objStdErr.AtEndOfStream
WScript.echo objStdErr.ReadLine
Wend
' ruby: No such file or directory -- C:/Users/Gebruiker/ruby/excel/ru.rb (LoadError)
I made the following changes
Set objWshScriptExec=objShell.Exec("cmd.exe /S /C vb6/make ""Project1.vbp""")
And it worked.
Since I am a beginner in vbscript, I don't know the function of
/S /C
so please let me know.

classic asp WScript.Shell run error

first time post here.
I am using classic ASP (vbscript) on a windows 2008 server with IIS7.
I am using a command-line utility with the following code:
(the dir is not the actual code but used for testing purposes!)
dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.run "%COMSPEC% /C dir c:\ > c:/tmp/dir.txt", 0, TRUE
set oShell = nothing
Now here is the problem: this works on most of my customers servers without any problems,
but one in particular gives an error without a description on the oShell.run line.
If I kill w3wp.exe or in IIS manager, recycle the application pool, it starts working again...
but the error always returns...
I have tried using process monitor, but I don't know what to look for...
Any idea what could be causing this behavior? or possibly some sort of workaround?
Try set oShell = Server.CreateObject("wscript.shell")

Execute a file in VBScript with cscript.exe and not wscript.exe

I know how to execute a exe with wscript.exe, something like this:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\myprogram.exe"
But I'm forced to run my vbscript with cscript.exe, and can't use the WScript object. Are there any way to execute an exe when loaded with cscript.exe?
Both wscript.exe and cscript.exe provide the WScript object; so "Set WshShell = WScript.CreateObject(...)" is okay for .vbs files started with "w|cscript.exe whatever.vbs". VBScript - the language - provides its own CreateObject() function, so you can use plain "Set WshShell = CreateObject(...)" in all scripts (.hta, html too). The WScript COM object is another object. You can use it 'everywhere' (if we disregard security settings). In short: your code will work (or fail) with both hosts.
Set objShell = CreateObject("WScript.Shell")
objShell.run("cscript d:\Test2.vbs")

Hiding a simple batch window

I've searched this and some pages came which weren't really useful or were too complicated (I am not a skilled batch file programmer!)! What I need is to run a batch file in hidden form (no console window). The batch file will not be called from external application or code. It will be clicked on by the client and then I want no console pages to be shown (only pages which are called by call command should be shown)! The batch file is exactly as follows:
#echo off
call setup.exe
IF EXIST "C:/caillog" goto tracking
IF NOT EXIST "C:/caillog" goto end
:tracking
call dotnet4.exe
call ClientService.msi
goto end
:end
I use VBScripts to open it hidden, like this:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("%batchfile%"), 0, True
for e.g the bat file I want to run is run.bat then I'll do like this
objShell.Run("run.bat"), 0, True
Instead of running the batch file run the vb file.
Write it in notepad and save it as *.vbs
If your Windows system supports powershell you can place this infront of "#echo off":
cmd /c powershell -Nop -NonI -Nologo -WindowStyle Hidden "Write-Host"
As others have said, use VBS.
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\FilePath" & Chr(34), 0
Set WinScriptHost = Nothing
This is what I use.

Resources