Starting and Stopping Services on Remote Servers using PowerShell
Posted by: Steven Smith,
on 10 Feb 2011 |
View original | Bookmarked: 0 time(s)
This wraps up my mini-series on using PowerShell to help manage services running on multiple remote servers. In my case, these scripts exist to make it easy for me to globally start and stop services on a number of web servers that are part of Lake Quincy Medias AdSignia ad platform you can read more about the message-based architecture were using here.
I learned the basics of starting and stopping services here. I adapted this so that I could store all of my servers names in one PowerShell script, and call into it with another to list all of the running servers on remote servers using another PowerShell script. I also made sure to make the ListServices.ps1 script use a function as well, so that I could call into it from my StartServices and StopServices scripts. Here they are:
StartServices.ps1
# Start Services on Production Servers
. .\ListServices.ps1; $result = ListServices
if
($result -eq $null) { "Result is null" }
foreach($result in $result)
{
"Starting " + $result.DisplayName + " on " + $result.MachineName
(new-Object System.ServiceProcess.ServiceController($result.DisplayName,$result.MachineName)).Start()
}
.\ListServices.ps1
StopServices.ps1
# Stop Services on Production Servers
. .\ListServices.ps1; $result = ListServices
if($result -eq $null) { "Result is null" }
foreach($result in $result)
{
"Stopping " + $result.DisplayName + " on " + $result.MachineName
(new-Object System.ServiceProcess.ServiceController($result.DisplayName,$result.MachineName)).Stop()
}
.\ListServices.ps1
These two scripts will work with whatever list of services you return back from ListServices.ps1 they are not specific to my applications services. I would caution you not to return a full list of services on the machine stopping ALL services on the machine, especially if its a production server, would probably be a Bad Thing. Dont do that.

