5

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"

Dan
  • 1,205
  • 3
  • 11
  • 9
  • What is the purpose for the batch file, are you trying to remove a Windows Service via the batch file? If so there are some easier ways to do this using an Installer class in your service project. Let me know and I will post the code I always use. – Jason Stevenson Sep 19 '08 at 02:03

6 Answers6

7

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
  • 1,646
  • 1
  • 12
  • 15
  • 3
    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 Nov 19 '08 at 08:41
  • 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 Sep 01 '09 at 18:22
1

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 I 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:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LanceSc
  • 2,094
  • 12
  • 14
0

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
  • 63
  • 2
  • 6
0

What worked for me was to specify the full path of the .bat file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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
  • 4,693
  • 5
  • 33
  • 52
0

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
  • 22,580
  • 14
  • 83
  • 137