【问题标题】:Task not working in Task Scheduler任务在任务计划程序中不起作用
【发布时间】:2018-07-02 18:53:51
【问题描述】:

我有一个 PowerShell 脚本,当它关闭时它会重新启动我们的 SMTP 服务。它看起来像这样:

$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."
}

我已验证此方法有效。我关闭了 SMTP 服务,运行脚本,然后 SMTP 服务重新启动并运行。

我创建了一个 .bat 文件供任务计划程序运行。它看起来像这样:

cd "C:\Scripts\" & powershell.exe C:\Scripts\restart-smtp.ps1

我已验证此方法有效。我关闭了 SMTP 服务,运行批处理文件,然后 SMTP 服务重新启动并运行。

现在我想让任务计划程序每 5 分钟运行一次。我设置了任务,将其指向批处理文件,但它不起作用。我想知道是否有人可以帮我解决这个问题。

我得到的唯一错误是:

操作员或管理员拒绝了该请求。 (0x800710E0)

我告诉它以与我登录时相同的管理员身份运行,即从 PowerShell 提示符运行脚本和批处理文件的相同管理员身份。

这是我的任务设置:

您可能注意到的一件事是历史记录被禁用。我想知道如果启用它是否会在历史记录中看到错误日志,但我不确定如何启用它。

这是在 Windows Server 2016 上运行的。

【问题讨论】:

  • 尝试启用“以最高权限运行”选项
  • 没有用。没用。
  • 您为什么不直接将您的服务配置为由 Windows 服务控制管理器自动重新启动?无论如何,您的 SMTP 服务器应该是按需启动的服务,不需要一直运行。
  • 任务计划程序可以直接运行PowerShell,无需启动cmd.exe。
  • 这确实更像是SuperUser 之类的问题。

标签: powershell batch-file taskscheduler


【解决方案1】:

感谢大家的回答,但我想出了一个更简单的解决方案:

【讨论】:

    猜你喜欢
    • 2012-09-27
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2015-02-26
    相关资源
    最近更新 更多