批处理脚本变量 - 从具有多行变量选项的文本文件的单行中提取

Batch Script Variables - Pull from a single line of a text file with multiple lines of variable options

我正在尝试创建一个批处理文件,用户可以使用该文件将 SCCM 和 RDP 连接到经常连接的 PC。我可以通过为每台 PC 编写脚本轻松地做到这一点,但我想以一种我有一个带有变量的文本文件的方式来做到这一点,就像我对 LOOP 所做的那样,但我不需要 LOOP,我需要能够 select 文本文件的单行。

目前我有一个选项列表,我用选项手动编辑脚本文件,用户 selects 一个数字到 select 名称,但我需要删除所有脚本,以便用户不会不小心修改脚本,只会修改变量。

示例文本文件 -(存储用户为 SCCM 和 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

示例 SCCM 批处理脚本 - (SCCM 命令行界面只允许计算机名称,所以我只能提取名称,但我仍然想要一个用于 SCCM 和 RDP 连接的文本文件)

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

示例 RDP 批处理脚本 -(我使用 cmdkey 存储 user/pass 因为 mstsc 命令行界面只允许名称,除非我先这样做)

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

非常感谢任何帮助。作为免责声明,这仅供对批处理脚本有一定了解的 IT 人员使用,只是试图让我们更容易访问我们经常访问的 PC 以及我们不断远程帮助的那些用户......

这是我之前制作的行选择器,它构建一个包含每行值的数组,并允许通过选择命令滚动和选择值。 :Load 和 :Extract 标签是与您的需求最相关的函数所在的位置。

编辑: 精简的组件仅针对您寻求的输出进行了修改 - 使用更安全的输入形式(选择命令)。请注意,如果选项数量超过 9,则需要使用 Set /P - 尽管应验证输入。

@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

使用 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
    )

原创

@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
::: - 
:::
::: - /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

@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