Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Is the user you have access to on the far end a local admin? –  Zypher Jan 19 '10 at 22:18
    
Yes, it has admin rights. my question is how do I choose that user when using SC or other similar tool. –  Scott Chamberlain Jan 19 '10 at 22:31

5 Answers 5

up vote 3 down vote accepted

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.

share|improve this answer
    
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 2:59

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.

share|improve this answer
    
hey look, something that works without domain user accounts, been searching for this for weeks. Thanks so much! –  deltree May 26 at 16:13
    
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 support.microsoft.com/kb/951016 –  zeldi Oct 21 at 22:19

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

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

share|improve this answer
    
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 3:00

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.

share|improve this answer

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?

share|improve this answer
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 (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! –  Bryan Hoffpauir 'BJ Hoffpauir' Jan 20 '10 at 15:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.