简体   繁体   English

计划任务重启服务与依赖服务

[英]Scheduled task restart service with dependent services

I have a Windows 2008 R2 server that acts as a Print Server.我有一台用作打印服务器的 Windows 2008 R2 服务器。

Almost all of the problems that occur on this server is fixed by restarting the Print Spooler service.此服务器上发生的几乎所有问题都可以通过重新启动 Print Spooler 服务来解决。

I came up with a plan to restart the service automaticly every night, and i found this command:我想出了一个每天晚上自动重启服务的计划,我找到了这个命令:

Powershell.exe -ExecutionPolicy Bypass -Command { Restart-Service -Name spooler } Powershell.exe -ExecutionPolicy Bypass -Command { Restart-Service -Name spooler }

The problem is that my spooler has three services that depend on it, so this command will not work.问题是我的假脱机程序有三个依赖它的服务,所以这个命令不起作用。 Is it safe to add a -force command after "spooler" or is there any other way to do it?在“spooler”之后添加-force命令是否安全,或者还有其他方法吗?

Restarting a service with dependencies nicely requires that dependent services are stopped first.重新启动具有依赖关系的服务很好地要求首先停止依赖服务。 There's a Dell KB article with sample code.一篇包含示例代码的戴尔知识库文章 In case of link rot, a bit tuned version is like so,在链接腐烂的情况下,稍微调整的版本是这样的,

# Service to be restarted
$restartedService = "FooBar"

# Get service dependencies
$dependents = (get-service $restartedService).dependentservices  

# information about dependent services
$dependentservices = gwmi Win32_Service | Select-object name,state,startmode | ? {$dependents.name -contains $_.name}

# Stop dependencies
Write-Host "Stopping Services" -f Yellow

foreach ($service in $dependentservices){

Write-Host "`r`nAnalyzing $($service.name)" -f Yellow

    if($service.startmode -eq "auto" -or $service.status -eq "Running"){
        Write-Host "Stopping $($service.name)"
        stop-service $service.name
    } else{
        "$($service.name) is $($service.state) with the startmode: $($service.startmode)"
    }
}

# Stop the service
stop-service $restartedService -force

Write-Host "Starting Services" -f Yellow

# start dependencies
foreach ($service in $dependentservices){

    Write-Host "`r`nAnalyzing $($service.name)" -f Yellow

    if($service.startmode -eq "auto"){
        "Starting $($service.name)"
        start-service $service.name
    } else{
        "$($service.name) is $($service.state) with the startmode: $($service.startmode)"
    }
}

# start service
start-service $restartedService

To restart a service with dependencies, in the latest versions of PowerShell (v5 onwards), you only need to include the -Force flag.要重新启动具有依赖项的服务,在最新版本的 PowerShell(v5 及更高版本)中,您只需要包含-Force标志。

As in the example below with/out the flag:如下面带有/不带标志的示例所示:

PS C:\Temp> Restart-Service -Name "nlasvc"
Restart-Service : Cannot stop service 'Network Location Awareness (nlasvc)' because it has dependent services.
It can only be stopped if the Force flag is set.
At line:1 char:1
+ Restart-Service -Name "nlasvc"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException
+ FullyQualifiedErrorId : ServiceHasDependentServices,Microsoft.PowerShell.Commands.RestartServiceCommand

PS C:\Temp> Restart-Service -Name "nlasvc" -Force
PS C:\Temp>

And as per the PowerShell references for Stop-Service and Restart-Service .并根据Stop-ServiceRestart-Service的 PowerShell 参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM