Using powershell to turn off/on service multiple servers from a central location

I am looking for a sample powershell script to turn off/on services on multiple servers from a central location. Greatly appriciate it.
dongocdungAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chad FranksSenior System EngineerCommented:
I have used this site in the past for these types of scripts.  This will break it down into a text file with a list of servers, manually entering the server names etc...

https://4sysops.com/archives/restart-a-service-on-multiple-remote-computers-using-powershell/
0
Shahnawaz AhmedTechnical Services SpecialistCommented:
Dear dongocdung,

$ComputerName = Get-Content d:\test.txt

foreach($computers in $ComputerName){

start service BITS



}



d:\test.txt   = this is the location where you need to put the test.txt file in which you need to mention all servers name on which you want to stop/start the services.
0
becraigCommented:
There are many ways to accomplish this, are you wanting to check service state etc  before performing an action ?

e.g. if service is not running and service is disabled - set service to - automatic then start etc

of you just want basic start service on computer A (If so this can be done via command line as well)
for /f %a in (serverlist.txt) do sc \\%a start servicename 

Open in new window

0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

dongocdungAuthor Commented:
I was able to run the script locally on the server but when I ran it from a central location, the script did not work. What could the reason why I couldn't run it from a central location?
0
becraigCommented:
I am not sure what you mean a central location ?

The input is simple:
1. serverlist.txt - will be a file with all the servernames in (one on each line)
2. you will have to specify the service name on the command line


e.g
for /f %a in (\\uncpath\C$\folder\serverlist.txt) do sc \\%a start servicename 

Open in new window

0
dongocdungAuthor Commented:
I don't think our environment allows us to run sc tool. Is there any other ways we can run it?
0
becraigCommented:
sc is not a tool, it is a "built-in" windows command.

If SC is not working in your environment I am not sure what else would work if basic windows commands do not work for you.
0
Justin YeungSenior Systems EngineerCommented:
@Shahnawaz Ahmed
your command is incorrect =P

to start a service by using start-service will require powershell remoting turned on on the target machine.

and it could be done this way. (where abc,bcd,dce are the computers)

invoke-command -computername abc,bcd,dce {start-service -Name "whatever services"}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.