'How to send "Enter-key" through batch file to server connected via batch file
Hi guys
With Gerhard's help, these below codes work perfectly for me
@echo off
set "range=10.151.12.11 10.151.13.11 10.151.27.11"
for %%i in (%range%) do (
start "" cmdkey.exe /generic:"%%i" /user:"buffer" /pass:"123$"
start "" mstsc.exe /admin /w:1600 /v:"%%i"
timeout /t 1
)
In the below code, I made some changes to send the enter key, but it does not work
@echo off
set "range=10.151.12.11 10.151.13.11 10.151.27.11"
for %%i in (%range%) do
(
start "" cmdkey.exe /generic:"%%i" /user:"buffer" /pass:"123$"
start "" mstsc.exe /admin /w:1600 /v:"%%i"
WScript.CreateObject("WScript.Shell").SendKeys("{Enter}");
timeout /t 1
)
Does anyone know how to send "Enter-key" in between these loops?
I want to send "Enter-key" after server is connected, then second server will connect
Solution 1:[1]
The simplest way to assemble an hybrid Batch file is combining it with JScript code, as in this example:
@if (@CodeSection == @Batch) @then
@echo off
set "range=10.151.12.11 10.151.13.11 10.151.27.11"
for %%i in (%range%) do (
start "" cmdkey.exe /generic:"%%i" /user:"buffer" /pass:"123$"
start "" mstsc.exe /admin /w:1600 /v:"%%i"
Cscript //nologo //E:JScript "%~F0"
timeout /t 1
)
goto :EOF
@end
WScript.CreateObject("WScript.Shell").SendKeys("{Enter}");
However, in order for this trick to work, the receiving window must have keyboard focus when the key is "pressed". Perhaps you need to also use AppActivate method to do that...
Solution 2:[2]
You are very close, but your problem is that WScript.CreateObject is not a batch command, but rather a VBScript command. In order to run that command in a batch script, you need to invoke the cscript interpreter for that line. Unfortunately, cscript only accepts files so you have to create a file with the WScript command in it.
set "range=10.151.12.11 10.151.13.11 10.151.27.11"
for %%i in (%range%) do (
start "" cmdkey.exe /generic:"%%i" /user:"buffer" /pass:"123$"
start "" mstsc.exe /admin /w:1600 /v:"%%i"
>hit_enter.vbs echo WScript.CreateObject("WScript.Shell"^).SendKeys("{Enter}"^)
cscript /nologo hit_enter.vbs
del hit_enter.vbs
timeout /t 1
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | SomethingDark |
