我有一个充当打印服务器的 Windows 2008 R2 服务器。
几乎所有在此服务器上出现的问题都可以通过重新启动后台打印程序服务来解决。
我想出了一个每天晚上自动重启服务的计划,我发现了这个命令:
Powershell.exe -ExecutionPolicy Bypass -Command { Restart-Service -Name spooler }
问题是我的假脱机程序有三个依赖于它的服务,所以这个命令不起作用。在“spooler”之后添加 -force 命令是否安全,或者还有其他方法吗?
最佳答案
重新启动具有依赖项的服务需要首先停止依赖项服务。有一个Dell KB article带有示例代码。如果链接失效,稍微调整一下的版本就像这样,
# 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
关于windows - 带有依赖服务的定时任务重启服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32323731/