Wednesday 1 July 2009

StopStart-Services

The Need:
To stop and restart a service across many computers.


The Script:
#StopStartServices.ps1
#This script will stop services and then start the services on a list of servers.

$service = Read-Host "Enter Service Name"
$ServerList = get-content (Read-Host "Please Provide Server List")

function StopService {
$ServerList | % { gwmi win32_service -ComputerName $_ -Filter "name='$Service'" | % { $_.stopservice() }} | out-null
write-host "$Service has stopped on $_"
}

Function StartService {
$ServerList | % { gwmi win32_service -ComputerName $_ -Filter "name='$Service'" | % { $_.startservice() }} | out-null
write-host "$Service has Started on $_"
}

stopservice
startservice

Write-host "All instances of $service have now restarted"