views:

13396

answers:

10

Sometimes while debugging, I need to restart a service on a remote machine. Currently, I'm doing this via Remote Desktop. How can it be done from the command line on my local machine?

+19  A: 

You can use the services console, clicking on the left hand side and then selecting the "Connect to another computer" option in the Action menu.

If you wish to use the command line only, you can use

sc \\machine stop <service>
Vinko Vrsalovic
Works perfectly from cmd.exe.Does not work in PowerShell, probably because of the \\, error follows: Set-Content : A parameter cannot be found that matches parameter name '[the name of the service]'.Only minor drawback is that it returns you to the command line before the operation is finished.
Josh Kodroff
Wow, so many different answers to this question :)
ripper234
since the sc command works asyncronously, if you need to script a service restart, take a look at the batch scripts I posted here: http://stackoverflow.com/questions/1405372/stopping-starting-a-remote-windows-service-and-waiting-for-it-to-open-close
Eric Falsken
A: 

Well, if you have Visual Studio (I know it's in 2005, not sure about earlier versions though), you can add the remote machine to your "Server Explorer" tag. At that point, you'll have access to the SERVICES that are running, or can be ran, from that machine (as well as event logs, and queues, and a couple other interesting things).

Stephen Wrighton
Bizarrely, this works in VS2005 but NOT in VS2008. You can still BROWSE the services but no longer start or stop them directly. In lieu of this, you can right-click the Services node and start Service Manager for that host.
Peter Wone
A: 
alexmac
Telnet is most definitely unencrypted.
Josh Kodroff
A: 

Agreed with the Connect to Another computer option. I've used the Computer Management MMC to do similar stuff in the past.

NET START SERVICENAME is the syntax for command line, but I don't see a way to do target a remote machine.

This sounds like a job for PowerShell.

Mike L
+2  A: 

You can use mmc:

  1. Start / Run. Type "mmc".
  2. File / Add/Remove Snap-in... Click "Add..."
  3. Find "Services" and click "Add"
  4. Select "Another computer:" and type the host name / IP address of the remote machine. Click Finish, Close, etc.

At that point you will be able to manage services as if they were on your local machine.

Ryan Duffield
Oops, didn't see the "command line" requirement.
Ryan Duffield
+1  A: 

You can use System Internals PSEXEC command to remotely execute a net stop yourservice, then net start yourservice

Philibert Perusse
+1  A: 

I would suggest you to have a look at RSHD

You do not need to bother for a client, Windows has it by default.

tafa
+2  A: 

I don't think enabling telnet is a good way to administer any server, let alone a Windows server. You're just opening a security hole.

The services console is definitely a more sane solution,

Derek
agree but he wanted command line so maybe he had in mind a batch file or something
alexmac
+1  A: 

Using command line, you can do this:

AT \\computername time "NET STOP servicename"
AT \\computername time "NET START servicename"
Andrew Moore
to make it clear, the "AT" command uses the windows scheduler to schedule a command. it's overkill for scripting a number of remote commands and doesn't guarantee execution order or command completion before the next command. The SC command is much better for this. Take a look at the batch scripts I posted in this question: http://stackoverflow.com/questions/1405372/stopping-starting-a-remote-windows-service-and-waiting-for-it-to-open-close
Eric Falsken
A: 

Several good solutions here. If you're still on Win2K and can't install anything on the remote computer, this also works:

Open the Computer Management Console (right click My Computer, choose Manage; open from Administrative Tools in the Start Menu; or open from the MMC using the snap-in).

Right click on your computer name and choose "Connect to Remote Computer"

Put in the computer name and credentials and you have full access to many admin functions including the services control panel.