SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


PowerShell - Restart Services


PowerShell - Restart Services

Author
Message
Yokers
Yokers
SSChasing Mays
SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)

Group: General Forum Members
Points: 616 Visits: 444
Comments posted to this topic are about the item PowerShell - Restart Services
wodom
wodom
SSC Eights!
SSC Eights! (942 reputation)SSC Eights! (942 reputation)SSC Eights! (942 reputation)SSC Eights! (942 reputation)SSC Eights! (942 reputation)SSC Eights! (942 reputation)SSC Eights! (942 reputation)SSC Eights! (942 reputation)

Group: General Forum Members
Points: 942 Visits: 256
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.



nathan.kulas
nathan.kulas
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 48
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.
Scott D. Jacobson
Scott D. Jacobson
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1788 Visits: 1020
and then attempts to restart the service regardless of the service state

(emphasis mine)

What could possibly go wrong? :-D
Iwas Bornready
Iwas Bornready
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25438 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search