Stop Windows Service during Uninstall
I have an app that installs a service when installed. I've forced a reboot after the MSI is deployed and this ensure the service is then started before the user tries to run the app.
Problem is that I need to stop this service before the application uninstalls, as it will fail otherwise. Is there an easy way to do this? I know it can be scripted using Custom Action, but unsure as to exactly where/how to run this. Any help greatly appreciated.
Problem is that I need to stop this service before the application uninstalls, as it will fail otherwise. Is there an easy way to do this? I know it can be scripted using Custom Action, but unsure as to exactly where/how to run this. Any help greatly appreciated.
0 Comments
[ + ] Show comments
Answers (19)
Please log in to answer
Posted by:
misk
18 years ago
choose call vb script from embedded code. call it anything, dont use spaces though.
paste the code i sent you into the 'enter the vbscript to execute' field. make sure you add the name of your service to the part where i have yourservicenamehere.
under location
sequence: Normal Execute Immediate/Deferred
add it after : Installinitialize
condition : REMOVE="ALL"
under properties
IN-SCRIPT OPTIONS: deferred execution - system context
processing :synchronous ignore exit code
these should do the trick!
paste the code i sent you into the 'enter the vbscript to execute' field. make sure you add the name of your service to the part where i have yourservicenamehere.
under location
sequence: Normal Execute Immediate/Deferred
add it after : Installinitialize
condition : REMOVE="ALL"
under properties
IN-SCRIPT OPTIONS: deferred execution - system context
processing :synchronous ignore exit code
these should do the trick!
Posted by:
misk
18 years ago
glad you got sorted, couple of grey hairs emerged no doubt. i must not have been clear enough when i gave the following info the other day in the above earlier posts:-
'indeed there is a simple way. go to the services section of windows installer editor in wise.
click add. you can then see you options, i usualy click on stop and delete under the uninstall section. make sure you enter the correct name for the service. '
no matter now. you are happy that is the main thing.
'indeed there is a simple way. go to the services section of windows installer editor in wise.
click add. you can then see you options, i usualy click on stop and delete under the uninstall section. make sure you enter the correct name for the service. '
no matter now. you are happy that is the main thing.
Posted by:
dgdavis
18 years ago
Posted by:
MSIPackager
18 years ago
Posted by:
aogilmor
18 years ago
ORIGINAL: MSIPackager
Working with the tables directly is the best way even it it takes a bit longer... forget all that GUI stuff - it does make life easier in many ways but you don't learm much about the wheels and cogs of an MSI by clicking boxes [;)]
you got that one right! i never learned so much about MSIs as when I had to write a macro to add values to tables directly. I had to learn about all the related tables stuff has to go into, key values, etc. you can't learn about that from the gui.
Posted by:
misk
18 years ago
Posted by:
dgdavis
18 years ago
Posted by:
misk
18 years ago
alas indeed, sometimes the CA is your only hope. sorry that didnt work. here is the code i usually use to stop a service.
run this early in the sequence.
Set WshShell = CreateObject("WScript.Shell")
Set oWmi = GetObject("winmgmts:")
sWmiq = "select * from Win32_Process where name='servicenamehere.exe'"
Set oQResult = oWmi.Execquery(sWmiq)
If oQResult.Count > 0 Then
For Each oProcess In oQResult
iRet = oProcess.Terminate(1)
Next
End If
run this early in the sequence.
Set WshShell = CreateObject("WScript.Shell")
Set oWmi = GetObject("winmgmts:")
sWmiq = "select * from Win32_Process where name='servicenamehere.exe'"
Set oQResult = oWmi.Execquery(sWmiq)
If oQResult.Count > 0 Then
For Each oProcess In oQResult
iRet = oProcess.Terminate(1)
Next
End If
Posted by:
dgdavis
18 years ago
Posted by:
dgdavis
18 years ago
Posted by:
MSIPackager
18 years ago
misk has provided all the info you need to get this working...
Forget the script for a mo, have you tried stopping the service manually from the command line (net stop servicename) ? If that works OK then go back to your script and note that 'servicenamehere.exe' should be the .exe file which is running (look in task manager for the exact name) and not the service name shown in the services GUI.
You should be able to control installation, start, stop and removal of your service in the MSI - at the very least you should be able to start it without resorting to a reboot (unless the package is installing other dependencies which are only functioning after a restart).
The script he's provided will only stop the service, you will need still need to arrange removal in the pacakge - either through Wise or by editing the MSI tables directly using the servicecontrol and serviceinstall tables...
Keep trying, I'm sure you will get there!
Regards,
Rob.
Forget the script for a mo, have you tried stopping the service manually from the command line (net stop servicename) ? If that works OK then go back to your script and note that 'servicenamehere.exe' should be the .exe file which is running (look in task manager for the exact name) and not the service name shown in the services GUI.
You should be able to control installation, start, stop and removal of your service in the MSI - at the very least you should be able to start it without resorting to a reboot (unless the package is installing other dependencies which are only functioning after a restart).
The script he's provided will only stop the service, you will need still need to arrange removal in the pacakge - either through Wise or by editing the MSI tables directly using the servicecontrol and serviceinstall tables...
Keep trying, I'm sure you will get there!
Regards,
Rob.
Posted by:
dgdavis
18 years ago
MSIPackager,
I can assure you I have followed misk's instructions to the letter and the service is still not stopping. I can do a net stop of the service when logged on as a normal user, so permissions is not an issue here. I've tried using the Services option within Wise to Stop and Delete the service on uninstall but that fails aswell. I've tried adding a batch file to the package and calling it from a custom action - fails. I'm really trying, but just need some more help. Any other ideas or reasons as to why this might be failing greatly appreciated.
I can assure you I have followed misk's instructions to the letter and the service is still not stopping. I can do a net stop of the service when logged on as a normal user, so permissions is not an issue here. I've tried using the Services option within Wise to Stop and Delete the service on uninstall but that fails aswell. I've tried adding a batch file to the package and calling it from a custom action - fails. I'm really trying, but just need some more help. Any other ideas or reasons as to why this might be failing greatly appreciated.
Posted by:
MSIPackager
18 years ago
Posted by:
MSIPackager
18 years ago
Well until you get the script working there's no way your CA is going to work! [;)]
Try starting from this script and see how you go...
Cheers,
Rob.
Try starting from this script and see how you go...
Cheers,
Rob.
Posted by:
dgdavis
18 years ago
Even that script from Microsoft is coming up with an error on Line 9! I have managed to write a script that does stop the service as follows:
Option Explicit
Dim objShell, intShortSleep, intLongSleep
Dim strService
Set objShell = CreateObject("WScript.Shell")
strService = " ""service name"""
intShortSleep = 1500
intLongSleep = 3500
objShell.Run "cmd",2
Wscript.Sleep intShortSleep
objShell.SendKeys "net stop" & strService
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"
Wscript.Sleep intLongSleep
objShell.SendKeys "Exit"
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"
WScript.Quit
I have tried putting this into a custom action in the MSI, and it still doesn't seem to work. The settings I have for the CA are:
Return Processing - Synchronous (ignore exit code)
In-Script Execution - Deferred in System context
Exec Scheduling - always execute
After InstallInitialise in Install Exec Sequence
Install Exec condition - REMOVE=ALL
Does this all look OK or am I going wrong somewhere?
Thanks,
Doug
Option Explicit
Dim objShell, intShortSleep, intLongSleep
Dim strService
Set objShell = CreateObject("WScript.Shell")
strService = " ""service name"""
intShortSleep = 1500
intLongSleep = 3500
objShell.Run "cmd",2
Wscript.Sleep intShortSleep
objShell.SendKeys "net stop" & strService
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"
Wscript.Sleep intLongSleep
objShell.SendKeys "Exit"
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"
WScript.Quit
I have tried putting this into a custom action in the MSI, and it still doesn't seem to work. The settings I have for the CA are:
Return Processing - Synchronous (ignore exit code)
In-Script Execution - Deferred in System context
Exec Scheduling - always execute
After InstallInitialise in Install Exec Sequence
Install Exec condition - REMOVE=ALL
Does this all look OK or am I going wrong somewhere?
Thanks,
Doug
Posted by:
MSIPackager
18 years ago
The MS Script works just fine for me... Have you tried it with the NetDDE service (you may need to start the service manually first).
Not sure about your script as your using sendkeys and sleep commands - this is risky. What happens if the user presses a key or switches windows / runs an app when your script is running? Your sendkeys wouldn't be hitting the right target.. Also if the processor is very busy or lower spec than the one you are testing on then your sleep delays may not be long enough - it's not a good way to be scripting as there are too many variables [8|]
Cheers,
Rob.
Not sure about your script as your using sendkeys and sleep commands - this is risky. What happens if the user presses a key or switches windows / runs an app when your script is running? Your sendkeys wouldn't be hitting the right target.. Also if the processor is very busy or lower spec than the one you are testing on then your sleep delays may not be long enough - it's not a good way to be scripting as there are too many variables [8|]
Cheers,
Rob.
Posted by:
dgdavis
18 years ago
OK. Managed to get the MS script working for the service I want to stop. Put it in the CA, and it doesn't stop the service before the error 1306 occurs regarding exclusing access to a file that the service uses. Should I be putting this CA earlier in the sequence? Does the fact this app is assigned to users (therefore advertised in Start Menu) make any difference i.e should I be putting the CA in the AdvtExec sequence instead?
Thanks,
Doug.
Thanks,
Doug.
Posted by:
dgdavis
18 years ago
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.