【问题标题】:Cannot get my Batch file to accept user input and set a prompted variable无法让我的批处理文件接受用户输入并设置提示变量
【发布时间】:2016-11-22 06:49:47
【问题描述】:

我已经阅读了几篇关于此的帖子,并在此花费了大约一个小时,改变了很多东西,并取得了很大的进步。

我创建了一个使用 PSEXEC 的批处理文件,因此我可以应用组策略更新并在网络中进行各种客户端维护

在我开始添加 if 语句以使用户更容易选择他们想要使用的域之前,该文件一直运行良好。

我似乎无法获取 if 语句来捕获输入然后设置变量。

我的 PSEXEC 输出是尝试连接到远程计算机或服务器后用户名/密码不正确。它启动 PSEXEC 服务,然后连接,然后是 PsExec could not start cmd on computer123The user name or password is incorrect.

这是脚本:

    @echo off &setlocal
    :: BatchGotAdmin
    :-------------------------------------
    REM  --> Check for permissions
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

    REM --> If error flag set, we do not have admin.
    if '%errorlevel%' NEQ '0' (
        echo Requesting administrative privileges...
        goto UACPrompt
    ) else ( goto gotAdmin )

    :UACPrompt
        echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
        set params = %*:"=""
        echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

        "%temp%\getadmin.vbs"
        del "%temp%\getadmin.vbs"
        exit /B

    :gotAdmin
        pushd "%CD%"
        CD /D "%~dp0"
    :--------------------------------------
    cls
    :start
    :::
    :::           *** Log into a remote computer to execute a command ***
    :::
    :::                (psexec DEVICE -u USERNAME -p PASSWORD cmd)
    :::
    :::                  PSTOOLS must be installed in the %path%
    :::

    :::
    for /f "delims=: tokens=*" %%A in ('findstr /b ::: "%~f0"') do @echo(%%A

    set /p computerName="Enter computer/server name: "
    echo "Enter the domain of your admin username:"
    echo =========================
    echo Enter '1' for domain1
    echo Enter '2' for domain2
    echo Enter '3' for domain3
    echo =========================

    set /p domainName=
    if %domainName% == 1 (
      set domainName = "domain1"
      goto continue
    )
    if %domainName% == 2 (
      set domainName = "domain2"
      goto continue
    )
    if %domainName% == 3 (
      set domainName = "domain3"
      goto continue
    ) else (
      goto choice
    )

    :continue
    set /p userName="Enter your Admin username: "
    powershell -Command $pword = read-host "Enter password " -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p password=<.tmp.txt & del .tmp.txt
    psexec \\%computerName% -u %domainName%\%userName% -p %password% cmd
    :choice
    set choice=
    set /p choice="Do you want to restart this file? Press 'y' for Yes or 'n' for No then Press ENTER: "
    if not '%choice%'=='' set choice=%choice:~0,1%
    if '%choice%'=='y' goto start

username or password incorrect error 似乎意味着 PSEXEC 命令没有domainName 的正确变量字符串

如果我在123 周围加上"",则输入输入后文件将退出。

【问题讨论】:

    标签: batch-file cmd uac psexec


    【解决方案1】:

    空格在字符串set 语句中很重要。

      set domainName = "domain1"
    

    domainnameSpace 设置为 Space"domain1"

    删除空格。更可靠的set

      set "domainName=domain1"
    

    域名设置为domain1

    choice 是一个批处理关键字,是标签或变量名的较差选择。

    【讨论】:

    • 感谢您的意见。我删除了上面的空格,PSEXEC 连接到计算机,然后在尝试启动 cmd 后失败。我不确定为什么会发生这种情况。我还将continuechoice 更改为nextstepresetfile。我什至尝试将if 语句中的"%domainName%" 更改为"!domainName!",但这没有任何区别。
    • 经过大量测试后,我在尝试连接到计算机后再次提示输入密码。当我输入与以前相同的密码时,它无法run PASSWORD on COMPUTER,所以有什么想法吗?
    • 我知道这不是 psexec 命令,因为我在批处理文件之外运行它并且运行良好。
    • 您是否删除了set params = %*:"="" 中的空格?由于您使用的是%params%,因此将使用您发布的批次将其替换为 nothing。我建议暂时echoing psexec 命令以确保正在传输正确或预期的与手动相同的数据。
    • 我的密码末尾有一个特殊字符。我回显了该命令,但它没有收到。
    猜你喜欢
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多