/build/static/layout/Breadcrumb_cap_w.png

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.

0 Comments   [ + ] Show comments

Answers (19)

Posted by: misk 18 years ago
Orange Senior Belt
2
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!
Posted by: misk 18 years ago
Orange Senior Belt
0
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.
Posted by: dgdavis 18 years ago
Orange Belt
0
misk, I did actually follow those instructions and it didn't work. I had to directly edit the servicecontrol table in AdminStudio 6.0 to get it to work - must just be a particularly flaky MSI!
Posted by: MSIPackager 18 years ago
3rd Degree Black Belt
0
Nice one Doug,

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 [;)]

Have a good weekend,
Rob.
Posted by: aogilmor 18 years ago
9th Degree Black Belt
0
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
Orange Senior Belt
0
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.
hope that helps.
Posted by: dgdavis 18 years ago
Orange Belt
0
Gave this a go, but alas no joy. The uninstall still fails stating that a file is being used and the service is still running. Any other ways to do this?
Posted by: misk 18 years ago
Orange Senior Belt
0
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
Posted by: dgdavis 18 years ago
Orange Belt
0
Can you recommend the most effective settings and sequence position for the custom action?
Posted by: dgdavis 18 years ago
Orange Belt
0
Can't seem to get this to work, no matter what. Any other methods to this or ideas?
Posted by: MSIPackager 18 years ago
3rd Degree Black Belt
0
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.
Posted by: dgdavis 18 years ago
Orange Belt
0
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.
Posted by: MSIPackager 18 years ago
3rd Degree Black Belt
0
Does the vb script sucessfully stop the service if you run it on it's own?

Have you switched on logging for msiexec to try and get some more info?

Cheers,
Rob.
Posted by: dgdavis 18 years ago
Orange Belt
0
Just tried this, and it actually doesn't stop the service.
Posted by: MSIPackager 18 years ago
3rd Degree Black Belt
0
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.
Posted by: dgdavis 18 years ago
Orange Belt
0
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
Posted by: MSIPackager 18 years ago
3rd Degree Black Belt
0
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.
Posted by: dgdavis 18 years ago
Orange Belt
0
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.
Posted by: dgdavis 18 years ago
Orange Belt
0
OK - finally cracked it!!! Used the ServiceControl table to stop the service instead, something which I didn't know how to utilise until reading about it today. Many thanks to misk and MSIPackager for all your help which has been a learning experience in itself for me.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ