3

I want to know if it is possible to configure a service to call a batch/powershell script when I stop it from services.msc.

While in Linux init.d services are fully programmable and even systemd services can have additional procedures I've yet to find a way to accomplish this on Windows.

Thanks in advance

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • If you are running a batch file or powershell script to stop the service why don't you just put whatever commands you want to run after that? – Squashman Dec 12 '18 at 14:40
  • 2
    Bear in mind that one of the reasons that services stop is because the system is being shut down. That's *not* a good time to start new activity. Is this activity that "has" to be done after stop or could it be re-formulated to be something done during startup (when you expect to have plenty of time)? (Also, of course, loss of power "shuts down" services and there's zero possibility of running additional code) – Damien_The_Unbeliever Dec 12 '18 at 15:42

1 Answers1

1

You can configure services to run a program on failure, but if you are stopping the service via services.msc then that likely wouldn't count as a failure.

The only other option I can think of would be to set up a PowerShell script running as a scheduled task that either periodically checks the services running status, or (for a more foolproof option) looks at the event log for events indicating that the service has been stopped (since the last time the script checked) and then performs whatever actions you require.

Per the comment from montonero, you wouldn't need to run the scheduled task periodically as it could be configured to run when the event itself occurs. This is described here: https://blogs.technet.microsoft.com/wincat/2011/08/25/trigger-a-powershell-script-from-a-windows-event/

Use the Event Viewer “Attach Task to This Event…” feature to create the task.

Launch "Event Viewer" and find the event. Once found, right-click on the event and select "Attach Task to This Event...".

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • 5
    It's possible to create a scheduler's task which will be triggered by a specific event. Just right click the event in the Event Viewer and select 'Attach task to this event' – montonero Dec 12 '18 at 14:46
  • Oh really, I didn't know that! Well that's even simpler then :). – Mark Wragg Dec 12 '18 at 14:47
  • Thanks for the replies, I was unable to create a custom event that would be triggered by my service's start/stop. The service Control Manager creates a generic system event for every start/stop, yet that's true for any service and I was unable to distinguish them. – Joao Morais Dec 13 '18 at 09:38
  • 1
    Have a look at this related answer as it shows how can you send details from the event through to the PowerShell script, which you could then use to distinguish: https://stackoverflow.com/questions/53744938/trigger-powershell-based-on-event-log/53745836#53745836 – Mark Wragg Dec 13 '18 at 09:41
  • Event/System/Provider/@Name Is there a path for the log value (General info)? It has the name of the service called and the status change in there. That would do the trick – Joao Morais Dec 13 '18 at 09:57
  • I'm not sure which value you mean, but it looks to me that the service name can be got from `Event/System/Provider/@Name` or `Event/System/Provider/@EventSourceName`. I'm looking at Event 1013 in the App log. If you go to the event details in Event Viewer > details tab > XML view you can see all the different fields/values you can query. – Mark Wragg Dec 13 '18 at 10:03