简体   繁体   中英

Batch script stops executing after winrs command

I have a script that contains a call to winrs in order to remotely start the execution of a .exe on a user specified target machine.

I want the following functionality:

  • start script
  • prompt user for name of target PC
  • winrs into target PC executing the .exe and tell user who ran the script that this is being done.
  • stop the execution of winrs and tell the user who ran the script that it has finished.
  • exit.
  • I want all the output on the machine that the script was run on and don't want any show of activity on the target machine.

The code I have is as follows:

@echo off

echo -------------------------------------------------------
echo PLEASE ENTER PC NAME OF DISCONNECTED MACHINE
echo -------------------------------------------------------
SET /p pcToReconnect= 
echo Attempting to contact Agent.

call winrs -r:%pcToReconnect% "C:\Path\To The\exe that I want\toExecute.exe" >> logfile.txt 2>>&1

echo Agent reconnected. Please allow ~5mins for your management console to update.

The code executes until the winrs call, and it does infact execute the .exe on the target machine, but it seems then that the remote shell just stays open doing nothing after this.

If I push ctrl+c at this point "Terminate the shell? (y/n)" gets placed in my logfile, (but no output in the cmd prompt) and I can then push "y" and enter, upon which the remote shell exits and "Terminate batch job (Y/N)" appears in the cmd prompt, however the last echo statement never executes.

How can I get the remote shell to automatically close after the .exe has been run, and echo some sort of confirmation that the script has completed on the prompt of whoever has executed it?

All help appreciated!

You are using call which executes a new cmd.exe window and running winrs in that new window and exiting. Remove it and it will work. I added pause at the end, in case you are running batch by double clicking it.

@echo off

echo -------------------------------------------------------
echo PLEASE ENTER PC NAME OF DISCONNECTED MACHINE
echo -------------------------------------------------------
SET /p pcToReconnect= 
echo Attempting to contact Agent.

winrs -r:%pcToReconnect% "C:\Path\To The\exe that I want\toExecute.exe" >> logfile.txt 2>>&1

echo Agent reconnected. Please allow ~5mins for your management console to update.
pause

EDIT

After some analysis it seems that the batch is waiting for the program to finish, so it would be possible to just call it with start which should call it and return to the original prompt.

start winrs -r:%pcToReconnect% "C:\Path\To The\exe that I want\toExecute.exe" >> logfile.txt 2>>&1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM