The bat file connects 35 servers at one click, but they connect randomly instead of what I mention in the bat. I need to connect the first server, which I list on the bat, but they won't connect. I would appreciate any help you can give me
start cmdkey /generic:"10.151.12.201" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.13.201" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.27.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.28.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.29.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.31.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.32.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.33.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.35.11" /user:"nas" /pass:"itti@123$"
start cmdkey /generic:"10.151.36.11" /user:"nas" /pass:"itti@123$"
start mstsc /admin /w:1600 /v:"10.151.12.201"
start mstsc /admin /w:1600 /v:"10.151.13.201"
start mstsc /admin /w:1600 /v:"10.151.27.11"
start mstsc /admin /w:1600 /v:"10.151.28.11"
start mstsc /admin /w:1600 /v:"10.151.29.11"
start mstsc /admin /w:1600 /v:"10.151.31.11"
start mstsc /admin /w:1600 /v:"10.151.32.11"
start mstsc /admin /w:1600 /v:"10.151.33.11"
start mstsc /admin /w:1600 /v:"10.151.35.11"
start mstsc /admin /w:1600 /v:"10.151.36.11"
CodePudding user response:
To run your commands in sequential order, add the /W
or /WAIT
to each line.
start /WAIT cmdkey /generic:"10.151.12.201" /user:"nas" /pass:"itti@123$"
start /WAIT mstsc /admin /w:1600 /v:"10.151.12.201"
In a batch script, a START command without /wait will run the program and just continue, so a script containing nothing but a START command will close the CMD console and leave the new program running.
The default behaviour of START is to instantiate a new process that runs in parallel with the main process.
The /WAIT option should reverse the default 'run in parallel' behaviour of START
See https://ss64.com/nt/start.html
CodePudding user response:
To ease life a bit, you really only need the commands once off by utilizing a for loop and only passing the meta variables to the commands. The meta variable, %%i
in this instance, is a temp variable holding the current value until all the commands after do
is completed where it will assign the next value to %%i
Then, depending on your exact requirement, which is not 100% clear there are a few options:
add a timeout between each command to ensure it starts up before the next one:
@echo off
set "range=10.151.12.201 10.151.13.201 10.151.27.11 10.151.28.11 10.151.29.11 10.151.31.11 10.151.32.11 10.151.35.11 10.151.36.11"
for %%i in (%range%) do (
start "" cmdkey.exe /generic:"%%i" /user:"nas" /pass:"itti@123$"
start "" mstsc.exe /admin /w:1600 /v:"%%i"
timeout /t 1
)
This however will take you 35 seconds to start up each remote session because of the delay, so the next would be to force them using conditional operators. Principal is simple &&
will tell the system to only start up the next, if the previous was successful or %errorlevel%
or exitcode
is not larger than 0.
So we use the same loop to build macros and and then launch the macros.
@echo off & set mac_cmdkey= & set mac_mstsc=
setlocal enabledelayedexpansion
set "range=10.151.12.201 10.151.13.201 10.151.27.11 10.151.28.11 10.151.29.11 10.151.31.11 10.151.32.11 10.151.35.11 10.151.36.11"
for %%i in (%range%) do (
set "mac_cmdkey=!mac_cmdkey! start "" cmdkey.exe /generic:"%%i" /user:"nas" /pass:"itti@123$" &&"
set "mac_mstsc=!mac_mstsc! start "" mstsc.exe /admin /w:1600 /v:"%%i" &&"
)
set "mac_cmdkey=!mac_cmdkey! echo cmdkey Done"
set "mac_mstsc=!mac_mstsc! echo mstsc Done"
%mac_cmdkey%
%mac_mstsc%
Lastly, if the intension was to open one at a time, do something, then close and start the next, use start "" /wait
@echo off
set "range=10.151.12.201 10.151.13.201 10.151.27.11 10.151.28.11 10.151.29.11 10.151.31.11 10.151.32.11 10.151.35.11 10.151.36.11"
for %%i in (%range%) do (
start "" /wait cmdkey.exe /generic:"%%i" /user:"nas" /pass:"itti@123$"
start "" /wait mstsc.exe /admin /w:1600 /v:"%%i"
timeout /t 1
)