2

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!

1 Answers1

0

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
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • This had the exact same behaviour. I thought call just made sure that the script returned to itself after calling the specified command? Pause is a helpful tip for running by double clicking though thank you! – Ciaran McKenzie Sep 26 '17 at 11:12
  • @CiaranMcKenzie what command are you executing? it is supposed to exit after the comman has been run. Perhaps test the command itself, so replace the command with something like a ping command. and see if it returns? – Gerhard Sep 26 '17 at 11:13
  • I want to remotely run a .exe file so just entering its path/name into a remote cmd. I have the two pcs here, so when I run either your code or my code, the .exe is remotely run on the target machine as I can see that the relevant services have started on that machine, and the logfile is error free. So the actual end-goal of running that .exe in the background of the target is achieved. It's as if the script stops then, the .exe has been executed on the remote machine, the errors/successes get logged into the logfile, and then I sit there at a prompt and the last echo never happens. – Ciaran McKenzie Sep 26 '17 at 11:32
  • have you tried a different command as a test? such as just doing `ping localhost` and pipe to a log and see if it returns? – Gerhard Sep 26 '17 at 11:48
  • This worked! It executed the command and returned to the script, finishing up by echoing the last statement and outputting ping info into the logfile, so I guess the problem is that this .exe doesn't return at all, which actually makes sense as it's a program being kicked off that will continually run in the background. So I suppose I want to start the .exe but not wait around to see what it does after it's been started? Is there any way of doing this that you know about? Thanks for the help getting a step closer! – Ciaran McKenzie Sep 26 '17 at 12:01
  • @CiaranMcKenzie try `cmd /C winrs -r:...` and see what happens? – Gerhard Sep 26 '17 at 12:09
  • This changes none of the behaviour, but also this doesn't seem to do what it's supposed to at all? if I run any sort of command cmd /C ipconfig or anything like that, the command runs as normal, I would have expected either the command prompt terminates after the command is executed, or else a new command prompt would open, the command executed and this prompt closed after. However it just seems to run in the existing window, which then remains open upon the command being executed.... Upvoting your answer as helpful as it's helped guide my testing and guessing at least! – Ciaran McKenzie Sep 26 '17 at 15:46
  • Thanks. let me see if I can figure this out quick, it cannot be too difficult. – Gerhard Sep 27 '17 at 07:07
  • do this and let me know. `start winrs -r:...` – Gerhard Sep 27 '17 at 07:18
  • I also added it to the answer. – Gerhard Sep 27 '17 at 07:19
  • I thought that I came back here and responded apologies! The following is what I found worked in the end! Execution does take a while but the script does terminate after a few minutes and the end goal is achieved. I'm sure it's not the best solution, but it completes my requirements of the script terminating, and some feedback given to the user. `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 console to update.` – Ciaran McKenzie Oct 19 '17 at 13:13