So, I wanted a simple batch script that calls a servers lists. This makes it super easy to add in new servers as well as provide decent information for newbies when hired on, hence the IP address and msg info. This also ensures that the proper method to remote into said server is followed, if needed.
Here is my script:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
echo.
set selectedrdpservername=
set selectedrdpserverip=
set selectedrdpservermsg=
:RUN
set count=0
rem How to use
rem Create rdpservers.txt file in the same folder as the cmd script with the following syntax
rem <SERVER><tab><IP><tab><MSG>
rem EXAMPLE:
rem DC1 10.20.30.10 Domain Controller 1
rem DC2 10.20.30.11 Domain Controller 2
rem When launching the RDP script, it will read the servers in the rdpservers.txt file and will
rem populate a list in which the user can select the number of the server to remote into.
rem Running the script
rem Change the below folder where the rdpservers.txt is located. Just easier than having to
rem keep the two files together if you have a lot of scripts already. Or, keep them together.
cd /d D:\scripts
echo Select a server number to RDP into:
for /F "tokens=1,2,3 delims= " %%a in (rdpservers.txt) do (
set /a count+=1
rem set var[!count!]=!count!
set "rdpserver!count!name=%%a"
set "rdpserver!count!ip=%%b"
set "rdpserver!count!msg=%%c"
echo !count!. %%a %%b %%c
)
echo.
if DEFINED selectedrdpservername (
rem The "call" command is important, otherwise it won't work properly!
call echo The last server you've selected was: %selectedrdpservername% && echo.
)
set /p selectedrdpserver= "Select a Server (1..,12..,30.. etc.): "
set "selectedrdpservername=%%rdpserver%selectedrdpserver%name%%"
set "selectedrdpserverip=%%rdpserver%selectedrdpserver%ip%%"
set "selectedrdpservermsg=%%rdpserver%selectedrdpserver%msg%%"
echo.
rem The "call" command is important, otherwise it won't work properly!
call echo You've selected server: %selectedrdpserver%. %selectedrdpservername% (%selectedrdpserverip%) - %selectedrdpservermsg%
echo Please enter your Windows credentials to continue...
call mstsc /v:%selectedrdpservername% /h:640 /w:1024 /admin
goto EXIT
:EXIT
cls
goto RUN
The script does count the lines in the rdpservers.txt file. But this is the reason I wanted to post this. I was working on the server list and thought it was be easier to organize it if the server list had comments or headers to organize a large list.
The current server list, as noted in the script above, is as follows:
DC1 10.20.30.10 Domain Controller 1
DC2 10.20.30.11 Domain Controller 2
But, I want to use something like the following to help organize a larger list of various servers:
--------Domain Controllers--------
DC1 10.20.30.10 Domain Controller 1
DC2 10.20.30.11 Domain Controller 2
Any ideas? I am thinking that if I continue to use the count method, this likely won't work as I likely can't ignore lines based on line content.
Thanks!
Big "Thank you!" to the following: