5

I'm trying (under Windows 7) to use the runas command to stop then restart a service. (Win7 requires admin privs to do this; thus the use of runas.)

Stopping the service works fine, but starting it does not. Here's the command I'm using for stopping the service:

runas /user:myDomain\myUserId "net stop serviceName"

Here's the command for starting the service:

runas /user:myDomain\myUserId "net start serviceName"

When I run the above command another command window opens, but flashes away before I can see anything in it; thus I have no idea what's going wrong.

So, my question is: How might I capture stdout and/or stderr from the net start command when run via runas? I've tried just using redirection but just get an empty file. Another solution would be to get the window opened by runas for the subtask to remain open.

Thanks in advance.

2 Answers2

10

Launch cmd.exe instead with the command to run, and specify that the output be written to a file.

runas /user:myDomain\myUserId "cmd.exe /c net stop serviceName > output.txt"

You can use 2> for error output from net stop.

Michael
  • 52,910
  • 5
  • 118
  • 142
  • 1
    this does not work for me. I am doing "runas /user:username "cmd /c time > output.txt"". If you could help me understand why this doesnt work it would really do me a favor! – Feytality Jan 05 '16 at 18:58
  • what directory does output.txt appear in? – user3450049 Nov 02 '19 at 00:15
5

Also, if you don't want to bother with the output file, you can use cmd.exe /k instead of /c to launch the command and it will leave the session window open for you. Might be easier/quicker if you just want a quick peek.

Just some guy
  • 61
  • 1
  • 1
  • 2
    Next time write this kind of response as a comment, as it is related to another answer. As the order of answers may change later on, it would be difficult to reconstruct your answer. – decden Apr 17 '13 at 15:25