-1

I'm trying to create a batch file that the user can use to connect with SCCM and RDP to PCs that are constantly connected to. I can do this easily with writing out the script for each PC but I would like to do this in a way that I have a text file with variables as I would do with a LOOP but I don't need a LOOP, I need to be able to select a single line of the text file.

Currently I have a list of options and I manually edit the script file with the options and the user selects a number to select the name but I need to remove all script so that the user doesn't accidentally modify the script, only the variables.

Sample text file - (Store user entered variables for both SCCM and RDP)

Variable1    Variable2     Variable3     Variable4
Description1 ComputerName1 ComputerUser1 ComputerPassword1
Description2 ComputerName2 ComputerUser2 ComputerPassword2
Description3 ComputerName3 ComputerUser3 ComputerPassword3
Description4 ComputerName4 ComputerUser4 ComputerPassword4
Description5 ComputerName5 ComputerUser5 ComputerPassword5
Description6 ComputerName6 ComputerUser6 ComputerPassword6

Sample SCCM Batch script - (SCCM command line interface only allows computer name so I'd only be able to pull the name but I still want a single text file for both SCCM and RDP connections)

Variable1's listed from text file -

Description1
Description2
Description3
Description4
Description5
Description6

Make a selection: ((User enters Variable1))

sccm\CmRcViewer.exe "<variable2>" <-------from a specific line a user selects

Sample RDP Batch script - (I use the cmdkey to store the user/pass since mstsc command line interface only allows the name unless I do this first)

Variable1's listed from text file -

Description1
Description2
Description3
Description4
Description5
Description6

Make a selection: ((User enters Variable1))

cmdkey /generic:"<variable2>" /user:"<variable3>" /pass:"<variable4>" <-------from a specific line a user selects
mstsc /v:"<variable2>" /f <-------from a specific line a user selects

Any assistance is greatly appreciated. As a disclaimer, this is only being used by those in IT who has a decent knowledge of batch scripts, just trying to make it a tad easier to get to our PCs we are constantly accessing and those users we are constantly helping remotely...

2 Answers2

1

This is a line selector I've made previously that builds an Array containing the value of each Line and allows scrolling and selection of the value via the choice command. The :Load and :Extract labels are where the functions most relevant to your needs reside.

EDIT: The stripped down components modified for just the output you seek - Using the safer form of input (Choice Command). Note that if the number of options exceeds 9, Set /P will need to be used - though input should be validated.

@Echo Off & Setlocal EnableDelayedExpansion
    Set "lines=0"
    Set "Options=E"
    For /f "Skip=1 Tokens=* Delims=" %%A in (%~dp0\Choices.txt) do (
        Set /A lines+=1
        Set "line[!lines!]=%%A"
        Set "Options=!Options!!lines!"
    )
:extract
    Cls
    For /F "Tokens=1,2 Delims==" %%A in ('Set line[') Do (
        Set "Option=%%~A"
        Set "Option=!Option:line=!"
        Echo !Option! %%B
    )
    For /F "Delims=" %%C in ('Choice /N /C %Options%') Do (
        If "%%C" == "E" (Endlocal & Exit /B 0)
        For /F "Tokens=2,3,4 Delims= " %%G in ("!line[%%C]!") Do cmdkey /generic:"%%~G" /user:"%%~H" /pass:"%%~I" & mstsc /v:"%%~G" /f
    )
Goto :extract

An example of validating input if using Set /P

    Set /P "INPUT=Selection: "
    For /F "Delims=" %%C in ("!Input!") Do (
        echo.%%C| findstr /R "[^0-9]" >nul 2>nul && Goto :extract
        IF "%%C" == "0" (Endlocal & Exit /B) Else IF "%%C" GTR "%lines%" Goto :extract
        For /F "Tokens=2,3,4 Delims= " %%G in ("!line[%%C]!") Do cmdkey /generic:"%%~G" /user:"%%~H" /pass:"%%~I" & mstsc /v:"%%~G" /f
    )

Original

@Echo off & CD "%~dp0"
TITLE %~n0 & Setlocal

%= Remark =%

    (Set LF=^


%= NewLine variable to split set /p input line =%)

::: / Creates variable /AE = Ascii-27 escape code.
::: - http://www.dostips.com/forum/viewtopic.php?t=1733
::: - https://stackoverflow.com/a/34923514/12343998
:::
::: - /AE can be used  with and without DelayedExpansion.
    Setlocal
    For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
        Endlocal
        Set "/AE=%%a"
    )
::: \

%= create blank batch file to store and Selectively retieve variables with =%

    If not Exist "inputURLlog.log" (Break>inputURLlog.log & Goto :main)
    For %%A in ( "%/AE%[36m[%/AE%[34mN%/AE%[36m]ew" , "[%/AE%[34mL%/AE%[36m]oad%/AE%[0m")Do Echo.%%~A
    Choice /N /C nl >nul
    If errorlevel 2 (Goto :load)    
    Goto :Main

:Input <VarName>
    Set "variable=%~1"
    Setlocal EnableDelayedExpansion

:Validate
%= allow input of variable, test input, store for reuse. =%

    Echo(%/AE%[35m
    Set /P "input=!variable!!LF!%/AE%[36m{> %/AE%[33m"
    IF "!input!"=="" (
        Echo(%/AE%[31m!variable! required.
        Goto :Validate
    )
    If "!input!"=="" (Echo(%/AE%[31m!variable! required. & Goto :Validate)
    IF /I "!input!"=="load" (Goto :load)
%= Url Validation - Optional =%
    (ping !input!)>nul || (Echo Invalid url & Endlocal & Goto :main)
    ECHO(!input!>>inputURLlog.log
    Endlocal
    Exit /B

:main
    Call :Input Url
    Goto :main

:load
    Setlocal EnableDelayedExpansion

    Set lines=0
    For /f "Tokens=* Delims=" %%A in (inputURLlog.log) do (
        Set /A lines+=1
        Set "line[!lines!]=%%A"
    )
REM Set "#=%lines%" & REM start from newest entry
    Set "#=1" & REM start from oldest entry
    Echo %#%
:extract

    For %%A in (!#!) do (
    Cls
    Title Option %%A of !lines!
    Echo(Url: !line[%%A]!
    For %%B in ("%/AE%[36m[%/AE%[34mS%/AE%[36m]elect" "[%/AE%[34mN%/AE%[36m]ext" "[%/AE%[34mL%/AE%[36m]ast%/AE%[0m")Do Echo.%%~B
    Choice /N /C LNS /M ""
    If "!errorlevel!"=="3" (Set "Url=!line[%%A]!" & Goto :Selected)
    If "!errorlevel!"=="2" (IF not !#! GEQ !lines! (Set /A #+=1) Else (Set "#=1"))
    If "!errorlevel!"=="1" (IF not !#! LEQ 1 (Set /A #-=1) Else (Set "#=%lines%"))
    )
    Goto :extract

%= Demonstrate that variable has been reloaded =%
:Selected
Echo( Selected Url = !Url!
Pause >nul

T3RR0R
  • 2,747
  • 3
  • 10
  • 25
  • I did try using your script and modifying it to what I needed but I just couldn't figure it out... I did finally get an answer on another forum that worked well for what I needed. I've posted it as an answer here. Thank you for trying though. – Leo Ashcraft May 21 '20 at 19:23
  • @LeoAshcraft I've updated the answer to include the modifications that would reflect your desired menu output, and exampled options for taking input either by Choice (For smaller option ranges) or by Set /P with appropriate validation to prevent script failure on user input error – T3RR0R May 22 '20 at 10:09
0
@echo off
setlocal enabledelayedexpansion
set x=0

for /f "skip=1 tokens=1-4" %%i in (%~dp0\Choices.txt) do (
  call set /a x+=1
  call set item.%%x%%.1=%%i
  call set item.%%x%%.2=%%j
  call set item.%%x%%.3=%%k
  call set item.%%x%%.4=%%l
)

for /l %%i in (1,1,%x%) do (
  call echo %%i. !item.%%i.1!
)

echo. & set /p sel=Enter Selection:

cmdkey /generic:"!item.%sel%.2!>" /user:"!item.%sel%.3!" /pass:"!item.%sel%.4!"
mstsc /v:"!item.%sel%.2!" /f