1

Unable to fire command (or file) when a service crashes/fails.

I have a script that I can run manually that emails me event logs that match search criteria. When I set said service to run a program using the following parameters:

Program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Command line Parameter: -ExecutionPolicy Bypass -File C:\PowerShell_Scripts\tftp_restarttftpd_restart.ps1

I've also tried just echoing some text to a file as well as send-mailmessage in Command Line Parameter. I've also used the -Command suffix. None work.

I trigger the service failure buy capturing the PID and running taskkill against it. I see the service fail but no email triggers.

I've looked for events to indicate the script was at least attempted but I don't see anything.

Help appreciated.

Cœur
  • 37,241
  • 25
  • 195
  • 267
awreneau
  • 11
  • 2
  • My guess would be that the permissions of the account running the service don't allow for what you're doing. Might be that the service account doesn't have permissions to the script file or the output file you were trying. If you're using one of the built-in ones, try creating a user account just for the service. See also [this question](https://stackoverflow.com/q/510170/696808) or [this doc](https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/dd548356(v=ws.10)?redirectedfrom=MSDN) which is still generally correct AFAIK. – Bacon Bits Dec 05 '19 at 17:44
  • Thanks for the first article, very good info. But, it raises more questions, the service is logging on Local System Account, so one would think that it *could* run the script with its unlimited privileges. If i understand correctly the script will be executed as the same "user" that is running the service. Is that correct? – awreneau Dec 05 '19 at 19:28
  • Solved this via different method. I have a scheduled tasks that triggers on the event ID I supplied. The action is then calls powershell and the Arguments are the bypassing of the execution policy followed by the script. – awreneau Dec 10 '19 at 15:25
  • STILL doesn't answer the syntax question about "powershell -command " for "Run a Program" on a service not working. – Patrick Burwell Aug 31 '21 at 20:45

0 Answers0