Win7 runas command: How to capture output of command that is run?

Bill Cohagan picture Bill Cohagan · Sep 22, 2009 · Viewed 12.7k times · Source

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.

Answer

Michael picture Michael · Sep 22, 2009

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.