Search code examples
windowsbatch-filecmdrestartpsexec

Prevent remote cmd window from closing


So I am working on rebooting a remote computer. Psexec is not installed on the remote machine so my usual cmds won't work. After tinkering a while I was able to get the reboot to go through by using:

runas /netonly /user:DOMAIN\USERNAME "shutdown -m \\XXX.XXX.XXX.XXX -r -f -t 0"

That works just fine about 99% of the time the issue being that the cmd window that it spawns closes immediately upon execution so there is no chance to see the error code that it returns.

I understand using cmd /k, pause, pinging 127 etc can all be using to hold open local cmd windows, but what can I do to keep a remote window open.


Solution

  • You can execute several commands remotely:

    • pause always: runas ..... "cmd /c shutdown ........ & pause"
    • pause in case of error: runas ..... "cmd /c shutdown ........ & if errorlevel 1 pause"