PowerShell - Restart Services

  • Comments posted to this topic are about the item PowerShell - Restart Services

  • When putting code on the site, it would be easier to handle if it had simple double-quote characaters instead of those "smart quotes" which have to be fixed with a search-and-replace. Not all are smart quotes, but some are.

    Also several hundred of the characters that look like blank spaces come out as an unknown character when pasted into my text editor, requiring another global search-and-replace to fix them.

  • If I can offer an improvement

    Function Restart-ServicesAndDependents {

    param(

    $ServiceName

    )

    $service = get-service -computer $Server $ServiceName

    Restart-Service -InputObject $service -Force -Confirm:$False -verbose

    # Get Dependent services - stop each of those

    $Dependents = $service.DependentServices

    ForEach ( $depService in $Dependents ) {

    $DepServiceName = $depService.Name

    $StartMode = (Get-WmiObject Win32_Service -filter "Name='${DepServiceName}'").StartMode

    if ( $StartMode -ne "Auto" ) {

    continue

    }

    if ( $depService.Status -ne "Running" ) {

    Start-Service -InputObject $depService -Confirm:$False -verbose

    }

    }

    }

    This grabs the dependent services, and starts those up as well. This is handy if you are hitting a service which has dependencies - in which case, Restart-Service alone will not start those back up, but it will stop them. So you have e.g. Dhcp which has WinHttpAutoProxySvc as a dependent. This will ensure WinHttpAutoProxySvc is started up after.

  • and then attempts to restart the service regardless of the service state

    (emphasis mine)

    What could possibly go wrong? 😀

  • Thanks for the script.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply