views:

4931

answers:

8

I need to execute a batch file as part of the un-install process in a Windows installer project (standard OOTB VS 2008 installer project-vdproj). One cannot execute a bat file directly from the Custom Actions in the installer project, so I wrote a quick vbs script to call the required bat file.
vbs code:

Set WshShell = WScript.CreateObject( "WScript.Shell" )
command = "uninstall-windows-serivce.bat"
msgbox command
WshShell.Run ("cmd /C " & """" & command & """")
Set WshShell = Nothing

When this script is run independent of the uninstall, it works perfectly. However, when run as part of the uninstall, it does not execute the bat file (but the message box is shown, so I know the vbs file is called). No errors reported (at least that I can tell). Why doesn't this script work as part of the "Uninstall Custom Action"

A: 

In your installer class, are you overriding the Uninstall method:

 Public Overrides Sub Uninstall(ByVal savedState As System.Collections.IDictionary)
            MyBase.Uninstall(savedState)
           'Shell to batch file here
    End Sub

And secondly, have you qualified the full path to the batch file?

Mike L
A: 

Have you checked that the batch file is in the current directory as seen by the script? I would add another message showing the directory it is using to ensure it is actually trying to execute the batch file where you think it is located.

Phil Wright
A: 

Mike L: I am not actually overriding the class manually; via the GUI I am adding the custom action in the Uninstall folder. Both the vbs file and bat file are in the same directory.

Phil: Yes, they are both in the same directory (that was my first thought as well).

Dan
A: 

Windows Installer scripts generally run as System, unless you tell it otherwise. Is it possible that your batch file needs to be run by the interactive user?

Mark Verrey
+5  A: 

I've run into this same problem and the issue is that you can't call WScript within the vbs file - you will need to JUST call CreateObject

ie.

Set WshShell = CreateObject( "WScript.Shell" )
command = "uninstall-windows-serivce.bat"
msgbox command
WshShell.Run ("cmd /C " & """" & command & """")
Set WshShell = Nothing
JustinD
The reason is, as I discovered yesterday, is that your vbs file isn't hosted in the Windows Scripting Host, but by the MSI itself. Consequently the WMI API isn't available. No "WScript". Though it seems WScript is accessible (ie "WScript.Shell"), that's just a namespace and not the object "WScript".
Matt Jacobsen
WScript.Shell is a COM object. It is not the same as the WScript object that is available when a script is run through WSH.
Cheeso
+1  A: 

Have a look at Wix Visual Studio built-in setup project is terrible.

Albert
+1  A: 

The wider you need to distribute your application the more strongly I would recommend against scripted custom actions. I had written a bunch in the past, but found that too many computers have problems running VBScript or JavaScript. I ended up rewriting them all in C++ to handle this situation. Here are a couple of posts that give an in-depth explanation on why you should avoid scripted custom actions.

http://blogs.msdn.com/robmen/archive/2004/05/20/136530.aspx http://bonemanblog.blogspot.com/2004/06/vbscript-and-jscript-msi-custom.html

LanceSc
A: 

What worked for me is to Specify the full path of the .bat file

CuberIce