Win7 runas command: how to capture the output of a command that is running?

I am trying (under Windows 7) to use the runas command to stop and then restart the service. (Win7 requires admin privs for this, so use runas.)

The service stop works fine, but it does not start. Here is the command I use to stop the service:

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

Here's the service start command:

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

When I run the above command, another command window opens, but blinks before I can see anything in it; therefore, I have no idea what is going on.

So my question is: how can I grab stdout and / or stderr from the net start command when run through runas? I tried using redirection but just got an empty file. Another solution would be to open a window open by runas so that the subtask remains open.

Thanks in advance.

+4
source share
2 answers

Run cmd.exe instead of the command to run and specify that the output will be written to the file.

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

You can use 2> to output an error from a network stop.

+9
source

Alternatively, if you don't want to bother with the output file, you can use cmd.exe / k instead of / c to run the command, and it will leave the session window open for you. You may find it easier / faster if you just want a quick peek.

+4
source

All Articles