How to detect or stop a service
The following article uses options that are available starting with the Professional edition and project type.
Windows Installer does not offer support for detecting or stopping a service on the target machine. However, in Advanced Installer this can be done through a predefined custom action.
1. Detect service
You can detect if a service is started, stopped or exists by using a "Detect service" predefined custom action. This custom action can be added at any point of the installation. If you want it to run at the beginning of the install process you can schedule it before "Wizard Dialogs Stage" -> "Searches". It should also be dragged (holding SHIFT key) before "Install Execution Stage" -> "Searches" to make sure that it runs even if there is no UI.
Since it uses installer properties, this custom action can only run as Immediate.
After adding the custom action, in its Custom Action Properties page, the Process Name field should be set to the name of the service you want to detect. When the custom action runs, it will set the AI_SERVICE_STATE property to one of these values:
- Started - the service is installed and it is started
- Stopped - the service is installed, but it is stopped
- Not Found - the service is not installed
The AI_SERVICE_STATE property can be used as a Custom Launch Condition in order to stop the installation based on the service state.
2. Stop service
In order to stop a running service, you can use a Stop Service predefined custom action. If you want the custom action to run in the beginning of the installation, you can schedule it before "Wizard Dialogs Stage" -> "Searches".
It should also be dragged before "Install Execution Stage" -> "Searches" while the SHIFT key is pressed to make sure it runs even if there is no UI. In this case the custom action should use the "Execute only once if present in both sequence tables" option in the Custom Action Properties page.
After adding the custom action, in its "Custom Action Properties" page, the Service name field should be set to the name of the service you want to stop.
3. Starting service
Similar to stopping a service, to start a service, you can use Start Service predefined custom action.
Most of the time, we want a service to be started in the late stage of the installation, ensuring the service finds on the machine all the other resources your setup package is installing. The most common place to schedule the start service custom action is inside "Install Execution Stage", between “Add Resources” and “Finish Execution” . Execution time should be set to “When the system is being modified(deferred)
After adding the custom action, in its "Custom Action Properties" page, the Service name field should be set to the name of the service you want to start.
Stopping or starting a service may require Administrator privileges. In this case, the custom action should be configured to Run under the LocalSystem account with full privileges(no impersonation), When the system is being modified (deferred). Since deferred custom actions can only run after "Preparing" action group, your custom action should be scheduled accordingly.