Go Back   Computer Forums > General Computing > Software and Operating Systems
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 01-23-2018, 03:27 PM   #1
Baseband Member
 
Join Date: May 2012
Location: Canada
Posts: 71
Default Task not working in Task Scheduler

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.
__________________

gib88 is offline   Reply With Quote
Old 01-24-2018, 06:01 AM   #2
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,033
Default Re: Task not working in Task Scheduler

rather than running the batch file, try running the command in the batch file as your scheduled task.
__________________

__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 01-24-2018, 11:24 AM   #3
Baseband Member
 
Join Date: May 2012
Location: Canada
Posts: 71
Default Re: Task not working in Task Scheduler

thanks for the help root, but I found a much simpler solution:

gib88 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 09:52 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0