As a workaround for various issues affecting the health of a server protected with AppAssure, it may be necessary to restart a service either at snapshot time or on a schedule. A common example is the WMI service. The problem to solve is identifying and restarting the dependent services pertaining to the service to be restarted as well. Powershell offers a way solving this issue.
NOTE: Quest support of PowerShell scripting for AppAssure: https://support.quest.com/appassure/kb/119405
Restarting a service via powershell can be done via the restart-service commandlet. If desiring to do the operation in separate steps, the stop-service and start-service commandlets can be used. However, if attempting to restart (or stop & start) a service with dependencies using Powershell (i.e., in the case of WMI using) restart-service winmgmt , an the error message below is thrown out and the operation fails.
restart-service : Cannot stop service 'Windows Management Instrumentation (winmgmt)' because it has dependent services. It can only be stopped if the Force flag is set.
At line:1 char:1
+ restart-service winmgmt
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException
+ FullyQualifiedErrorId : ServiceHasDependentServices,Microsoft.PowerShell.Commands.RestartServiceCommand
Adding the -Force flag as indicated in the error message does not fully solve the issue. Although some (or sometimes all) of the dependent services stop, when the the winmgmt service is started, the dependent services remain stopped.
To mention that the start- stop- restart-service commandlets do not write a resultant object to the pipe. To override this behavior, the -passthru parameter is required.
As such, for services with just a few dependencies, running
# restart-service winmgmt -force -passthru
May suffice as all the currently running dependencies are restarted.
However, this is an unsafe way do to the restart as some services may have been previously stopped.
For production machines, the following approach is recommended (the example is using the WMI service):
# cls
# Write-Host "Restarting Service with Dependencies`r`n" -f Green
1. Get wmi dependencies
$wmidependents = (get-service winmgmt).dependentservices
#2. Get all necessary information about dependent services
$wmidependentservices = Get-WmiObject Win32_Service | Select-object name,state,startmode | where {$wmidependents.name -contains $_.name}
2. Stop wmi dependencies
Write-Host "`r`nStopping Services`r`n-----------------`r`n" -f Yellow
foreach ($service in $wmidependentservices){
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
#you can add more logic in the block
}
else{
"$($service.name) is $($service.state) with the startmode: $($service.startmode)"
}
}
#equivalent to stop-service $wmidependents.name
3. Stop the WMI service
stop-service winmgmt -force
Write-Host "`r`nStarting Services`r`n -----------------`r`n" -f Yellow
4. start dependencies
foreach ($service in $wmidependentservices){
Write-Host "`r`nAnalyzing $($service.name)" -f Yellow
if($service.startmode -eq "auto"){
"Starting $($service.name)"
start-service $service.name
#you can add more logic in the block
}
else{
"$($service.name) is $($service.state) with the startmode: $($service.startmode)"
}
}
#equivalent to start-service $wmidependents.name
5. start WMI
start-service winmgmt
Please note that both delayed-auto and auto startup types are shown as "auto" by WMI.
For exemplification purposes, the code above is attached as a script to this KB.
© ALL RIGHTS RESERVED. Feedback Terms of Use Privacy