I need to create a script which checks if a service is running and starts it if failed. Regardless of the outcome I would like an email which tells me the results. I would like this to be in a .bat file although .vbs is acceptable.
I have found various posts on how to do this. I am currently using:
set service=###
for /F "tokens=3 delims=: " %%H in ('sc query %service% ^ | findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (net start %service%)
This runs successfully and will start the service if stopped however I am getting
%%H was unexpected at this time
This causing it to register as failed on Task scheduler
The second part of the script I am using is:
for /F "tokens=3 delims=: " %%H in ('sc query %service% ^| findstr " STATE"') do (
if /I "%%H" EQ "RUNNING" (
SendMail /smtpserver localhost /to [email protected] /from [email protected] /subject Service Autostart Notification /body Autostart on service %service% succeded.
) else (
SendMail /smtpserver localhost /to [email protected] /from [email protected] /subject Service Autostart Notification /body Autostart on service %service% failed.
)
)
)
)
This is not sending the email as "Sendmail is not recognised as an internal or external command, operable command or batch file"
Please could someone give me assistance with this. I have tried to find solutions online but have been unsuccessful thus far.
Kind Regards
SendMail? Where is it located ?Sendmail? It is not a default command on Windows.