Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


Task not working in Task Scheduler

  • Please log in to reply
No replies to this topic

#1 gib65


  • Members
  • 159 posts
  • Local time:11:07 AM

Posted 23 January 2018 - 03:33 PM

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
foreach ($Service in $AllServices)
if ($Service.name -eq $SMTPServiceName)
$SMTPService = $Service
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."
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.

BC AdBot (Login to Remove)



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users