2

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.

Scott Chamberlain
  • 1,445
  • 2
  • 21
  • 37

5 Answers5

7

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.

Evan Anderson
  • 141,071
  • 19
  • 191
  • 328
  • hey look, something that works without domain user accounts, been searching for this for weeks. Thanks so much! – deltree May 26 '14 at 16:13
  • 2
    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\System http://support.microsoft.com/kb/951016 – zeldi Oct 21 '14 at 22:19
3

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.

Evan
  • 531
  • 2
  • 5
  • 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 Jan 20 '10 at 02:59
1

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"""

techie007
  • 1,892
  • 17
  • 24
  • Runas only works for locally accessible accounts. Local domain, and local computer accounts. Not "remote" local computer accounts. If that made any sense. – Joseph Kern Jan 20 '10 at 03:00
  • 1
    @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" – Andrew Jun 01 '17 at 16:38
0

You might want to look at Powershell 2.0. It might have this functionality built in as a sort of RDP, thus you wouldn't need a domain, but rather enable the connection and open some ports. Look here for a slight description and some keywords that you can Google.

0

In a DOMAIN environment, you're able to use the NET USE commands, assuming the proper user rights have been granted to your logged in user that runs the script. PowerShell is probably the most powerful option you have, but if you're running older Microsoft Operating Systems, you may have some issues running some PowerShell commands. If everything is Windows 2008 or newer, I'd definitely go with PowerShell.

If that isn't an option, I've had great success with the PSTools package. If you DO go with this option, as Evan mentioned earlier, you should probably familiarize yourself with the pskill utility in the same package. Some services don't die gracefully when you're mucking about with them remotely, and this little utility is GREAT for killing them.

Out of curiosity, what script language are you coding this in? VBScript? BATch? Perl?

  • 1
    Batch, it is actually a set of batch commands VS2008 will run any time the output exe is updated. – Scott Chamberlain Jan 20 '10 at 14:12
  • 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 (http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?culture=en-US&EventID=1032370949&CountryCode=US). There are pre-existing templates (http://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! – Bryan 'BJ' Hoffpauir Jr. Jan 20 '10 at 15:59