我有一台充当打印服务器的Windows 2008 R2服务器。

通过重新启动后台打印程序服务,几乎可以解决此服务器上发生的所有问题。

我想出了一个计划,每晚自动重新启动该服务,并且发现了以下命令:



问题是我的后台打印程序有三个依赖于它的服务,因此该命令将不起作用。在“假脱机程序”之后添加-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

07-27 20:01
查看更多