Reputation: 394
I have a Windows 2008 R2 server that acts as a Print Server.
Almost all of the problems that occur on this server is fixed by restarting the Print Spooler service.
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 }
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?
Upvotes: 2
Views: 8021
Reputation: 21
I think we can combine tips from other answers above, (powershell 5.1)
powershell -ExecutionPolicy Bypass -Command "Restart-Service -Name spooler -Force -include (get-service spooler).dependentservices"
and you can check dependent services status, before and after
powershell -ExecutionPolicy Bypass -Command "(get-service spooler).dependentservices"
Upvotes: 0
Reputation: 1376
To restart a service with dependencies, in the latest versions of PowerShell (v5 onwards), you only need to include the -Force
flag.
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.
Upvotes: 1
Reputation: 24071
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
Upvotes: 3