Controlling services on another computer

16,316

Solution 1

I would take a look at PsService from Microsoft's Sysinternals (its part of the excellent tool set written by Mark Russinovich).

Straight from the website:

Usage: psservice [\\computer [-u username] [-p password]] <command> <options>

For <command>, you can use start, stop, or restart as the most common options. What is nice about this script, is that it is not dependent upon the two machines being members of the same domain or having any other trust.

Solution 2

Just for completeness here, if you want to do this with only stock Windows tools you can first establish an IPC connection to the remote machine (to establish a security token on that machine) and then use the "SC" command:

net use \\remote\IPC$ /user:local-username-on-remote-machine passowrd
sc \\remote stop service-name
net use \\remote\IPC$ /delete

That'll let you control services on the remote machine, irrespective of the combination of workgroup / domain membership of both machines.

Solution 3

Never personally tried it in a non-domain config but perhaps run SC.exe with "RunAs" and use credentials for the target computer?

Runas /user:TargetComputerName\TargetAccountName "sc.exe \\testsvr stop ""myService"""

Share:
16,316

Related videos on Youtube

Scott Chamberlain
Author by

Scott Chamberlain

Any opinions expressed here are my own and not that of Amazon nor any of it's subsidiaries.

Updated on September 17, 2022

Comments

  • Scott Chamberlain
    Scott Chamberlain almost 2 years

    I am currently writing a service for windows and I am frequently tweeking it and updating it on to my test computer. I would like to write a script that automaticly stops the service, updates the exe and restarts the service. however its that stop and start part that I am having trouble with.

    The computer I am connecting to is not on a domain and we do not share a username and password. I figure my best bet is to do sc.exe \\testsvr stop "myService" however because I do not have a domain or shared usernames and passwords this gives me a "Access is denied" message.

    I can very easily just add the user but I wanted this as a learning experience for when I am not able to change the users.

    • Zypher
      Zypher over 14 years
      Is the user you have access to on the far end a local admin?
    • Scott Chamberlain
      Scott Chamberlain over 14 years
      Yes, it has admin rights. my question is how do I choose that user when using SC or other similar tool.
  • Joseph Kern
    Joseph Kern over 14 years
    This should work, across domain boundaries, just make sure to protect those usernames and passwords. Oh, and use an IP address if the NETBIOS name does not resolve.
  • Joseph Kern
    Joseph Kern over 14 years
    Runas only works for locally accessible accounts. Local domain, and local computer accounts. Not "remote" local computer accounts. If that made any sense.
  • Scott Chamberlain
    Scott Chamberlain over 14 years
    Batch, it is actually a set of batch commands VS2008 will run any time the output exe is updated.
  • Bryan 'BJ' Hoffpauir Jr.
    Bryan 'BJ' Hoffpauir Jr. over 14 years
    Batch is probably the simplest to get setup and running quickly using either with NET USE / SC or with PSTools. If you're up for it, though, I'd recommend the PowerShell route as it's not only more powerful, but comes with hooks for Visual Studio Integration as well (msevents.microsoft.com/CUI/…). There are pre-existing templates (psvs2008.codeplex.com) you can use, and even though the learning code is steeper, you'll be able to leverage PowerShell in a lot of other ways, so it might be worth the time!
  • Sebtic
    Sebtic about 10 years
    hey look, something that works without domain user accounts, been searching for this for weeks. Thanks so much!
  • zeldi
    zeldi over 9 years
    In case of Vista/W7/W8 system you need to allow elevation potential to connect as full administrator (which might represent a security issue), you need to add DWORD LocalAccountTokenFilterPolicy=1 to HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Syst‌​em support.microsoft.com/kb/951016
  • Andrew
    Andrew about 7 years
    @JosephKern That's not entirely true. You can use the /netonly switch to make Runas work with remote accounts, including "remote" local (that is, non-domain) accounts. It works for this command after disabling remote UAC restrictions anyway: runas /netonly /user:remotemachine\remoteuser "sc \\remotemachine stop TheServiceName"