无法让我的批处理文件接受用户输入并设置提示变量

时间:2016-11-22 06:49:47

标签: batch-file cmd uac psexec

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

我创建了一个使用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的正确变量字符串

如果我在""12周围3,则在输入输入后文件退出。

1 个答案:

答案 0 :(得分:2)

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

  set domainName = "domain1"

domainname Space 设置为 Space “domain1”

删除空格。更可靠的set

  set "domainName=domain1"

域名设置为 domain1

choice是一个批处理关键字,对于标签或变量名称来说是一个糟糕的选择。