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 would like to execute the SC command against a network target as a different user. I need to be able to specify username and password on the commandline as well, as this is for scripting purposes. However, I see no options on SC for executing a command as another user. I see the 'obj' option which accepts a username, but that appears to be for setting the user account under which the service itself should run under.

If there is no way to change the user-context of the execution, is there another command that will allow me to create/start/stop a windows service in a scriptable manner (calling from TeamCity)

share|improve this question
    
Are the to machines joined to the same domain? –  Mathias R. Jessen Feb 10 '12 at 23:24
    
@MathiasR.Jessen Yes, would be supplying domain\username as the username ideally –  CoolUserName Feb 10 '12 at 23:27

3 Answers 3

Use the "RunAs" commandline tool, it lets you supply an explicit credential set, and supports both "Domain\Username" and UPN formatting, allowing you to authenticate as another user and the run your command:

   Runas /user:Domain\user "sc \\remoteServer stop serviceKeyName"

Alternative:

Set up a scheduled task with a specific logon account

share|improve this answer
    
Unfortunately this command does not allow one to specify the password in the command line, it can only be provided when prompted after execution. Hence, is not scriptable. –  CoolUserName Feb 13 '12 at 16:05
    
Maybe useful to change windows service credential in remote Server. –  Kiquenet Oct 16 at 21:27
    
runAs tool similar in SysInternals ? Who's support that tool ? –  Kiquenet Oct 16 at 21:29

In PowerShell:

(get-wmiobject win32_service -comp servername -cred domain\user -filter "name='browser'").stopservice()

-cred will pop up a credentials dialog box also

Replace servername, domain\user and the name= with proper values

with -cred you can even used stored credentials.

share|improve this answer

Ended up installing and using PsExec - http://technet.microsoft.com/en-us/sysinternals/bb897553. Allows for inline specification of username/password, so is scriptable.

share|improve this answer
    
Works Psexec to change windows service credential in remote Server ? –  Kiquenet Oct 16 at 21:28

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.