# Task not working in Task Scheduler



## gib88 (Aug 18, 2005)

Hello,
I have a PowerShell script that restarts our SMTP service when it's down. It looks like this:
$Computer = "localhost"
$SMTPServiceName = "SMTPSVC"
$AllServices = get-service -ComputerName $Computer
$SMTPService
foreach ($Service in $AllServices)
{
if ($Service.name -eq $SMTPServiceName)
{
$SMTPService = $Service
break
}
}
if ($SMTPService.status -eq "StopPending")
{
write-host "Service" $SMTPService "is pending stop. Attempting to stop and restart."
$servicePID = (gwmi win32_Service | where {$_.Name -eq $SMTPServiceName}).ProcessID
Stop-Process $ServicePID
Start-Service -InputObject (get-Service -ComputerName $Computer -Name $SMTPServiceName)
write-host "Service" $SMTPService "restarted."
}
elseif ($SMTPService.status -eq "Paused")
{
write-host "Service" $SMTPService "is paused. Attempting to resume."
Resume-Service -InputObject (get-Service -ComputerName $Computer -Name $SMTPServiceName)
write-host "Service" $SMTPService "resumed."
}
elseif ($SMTPService.status -eq "Stopped")
{
write-host "Service" $SMTPService "is stopped. Attempting to restart."
Start-Service -InputObject (get-Service -ComputerName $Computer -Name $SMTPServiceName)
write-host "Service" $SMTPService "restarted."
}
else
{
write-host "Service" $SMTPService "is running."
}
I've verified that this works. I shut the SMTP service down, run the script, and the SMTP service is back up and running.
The I created a .bat file for Task Scheduler to run. It looks like this:
cd "C:\Scripts\" & powershell.exe C:\Scripts\restart-smtp.ps1
I've verified that this works. I shut the SMTP service down, run the batch file, and the SMTP service is back up and running.
Now I want to get the Task Scheduler to run it every 5 minutes. I setup the task, point it to the batch file, but it doesn't work. I'm wondering if someone can help me troubleshoot this.
The only error I get is: The operator or administrator has refused the request. (0x800710E0)
I'm telling it to run as the same administrator that I log in as, the same administrator that runs the script and the batch file from the PowerShell prompt.
Here are my task settings:








One thing you might notice is that History is disabled. I'm wondering if I would see error logs in the history if it was enabled, but I'm not sure how to enable it.
This is running on Windows Server 2016.


----------



## dmccoy (Oct 7, 2017)

I would test with "Run with highest privileges" checked. Also make sure the location the batch file is in doesn't require special permissions.


----------



## gib88 (Aug 18, 2005)

dckeks said:


> I would test with "Run with highest privileges" checked. Also make sure the location the batch file is in doesn't require special permissions.


Did not work.


----------



## dmccoy (Oct 7, 2017)

Are the powershell and bat extensions added to your default path variables?


----------



## dmccoy (Oct 7, 2017)

http://www.thewindowsclub.com/how-to-schedule-batch-file-run-automatically-windows-7


----------



## gib88 (Aug 18, 2005)

Thanks for the help dckeks, but I found a much simpler solution:


----------



## dmccoy (Oct 7, 2017)

Okay great. Yeah that should work as well. Please mark this solved.


----------

