For anyone who is interested, I have written a "stop service" script in JScript that stops a named service using WMI. It will wait for the service to be stopped up to an (approximate) number of seconds. If the service does not stop within the timeout period, the script returns error 1053 (ERROR_SERVICE_REQUEST_TIMEOUT).
Code: Select all
// Stops a service. Apparently configuring a service control action in
// the ServiceControl table doesn't work on Win 7. Sequence this script
// before the InstallValidate standard action.
//
// Author: Bill Stewart
// Creates the global SWbemServices object.
var SWbemServices = GetObject("winmgmts:{impersonationlevel=impersonate}!" +
"root/cimv2");
// You can't use the WScript object in a Custom Action (CA), so the
// stopService function (below) can't use its Sleep method. Workaround:
// Create a temporary .js script in the temporary directory and call it.
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var SystemFolder = 1, TemporaryFolder = 2, ForWriting = 2;
var TempScriptName = "";
// Creates the temporary Sleep script if it doesn't already exist and
// writes the WScript.Sleep method to it.
function createTempSleepScript(milliSeconds) {
var result = 0;
// Only continue if the file doesn't already exist.
if (TempScriptName == "") {
// Create a temporary filename with a '.js' extension.
do {
TempScriptName = FSO.BuildPath(FSO.GetSpecialFolder(TemporaryFolder),
FSO.GetTempName());
TempScriptName = TempScriptName.substr(0, TempScriptName.length - 3) +
"js";
}
while (FSO.FileExists(TempScriptName));
// Open it as a text stream, write the script line, and close it.
try {
var textStream = FSO.OpenTextFile(TempScriptName, ForWriting, true);
textStream.WriteLine("WScript.Sleep(" + milliSeconds + ");");
textStream.Close();
}
catch(err) {
result = err.number;
}
}
return result;
}
// Sleeps for approximately 'milliSeconds'. The WshShell object is a global
// to avoid the overhead of creating it each sleep iteration.
var WshShell = new ActiveXObject("WScript.Shell");
function sleep(milliSeconds) {
var result = createTempSleepScript(milliSeconds);
if (result != 0)
return result;
var cmdLine = FSO.BuildPath(FSO.GetSpecialFolder(SystemFolder),
"wscript.exe") + " " + TempScriptName;
// Run the temporary script and wait for it to complete.
return WshShell.Run(cmdLine, 0, true);
}
// Retrieves the Win32_Service instance for the service named 'serviceName'
// using the global SWbemServices object. Returns a service object if
// successful or an error code number if failure.
function getService(serviceName) {
var result;
try {
result = SWbemServices.Get("Win32_Service.Name='" + serviceName + "'");
}
catch(err) {
result = err.number;
}
return result;
}
// Tells a service to stop and waits for up to 'waitSeconds' for it.
// Returns 0 if successful, non-zero if failure.
function stopService(serviceName, waitSeconds) {
var ERROR_SERVICE_REQUEST_TIMEOUT = 1053;
var service = getService(serviceName);
if (typeof service == "number")
return service;
var delay = 1;
service.StopService();
do {
if (service.State == "Stopped")
break;
sleep(1000);
delay += 1;
service = getService(serviceName);
}
while (delay <= waitSeconds);
// Delete the temporary script.
if (FSO.FileExists(TempScriptName))
FSO.DeleteFile(TempScriptName);
return delay > waitSeconds ? ERROR_SERVICE_REQUEST_TIMEOUT : 0;
}
Because you can't use the WScript object in a custom action, this script writes a temporary .js script in the Temp folder containing the "sleep" command and calls it to delay the script while while waiting for the service to stop.
To use it, add a function to the end of the script that looks like this:
Code: Select all
function stopYourService() {
return stopService("your_service_name", 20);
}
Use 'stopYourService' as the function name in your custom action, and of course change 'your_service_name' to match your service's name and 20 is the number of seconds the script should wait before it considers the service stop request to have timed out. As Gabriel mentioned previously, set the script to execute before the InstallValidate standard action.
Seems to be a lot of work to do something that used to work in XP but doesn't work in Windows 7...I'd like to know why I have do do this.
HTH,
Bill
FOLLOW US
Get the latest news in Application Packaging