2016-11-22 84 views
0

我已经阅读了几篇关于此的文章,并花了大约一个小时的时间,改变了很多东西,并取得了很大进展。无法让我的批处理文件接受用户输入并设置提示变量

我创建了一个使用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

如果是我,但""围绕1,23,文件在输入输入后退出。

回答

2

空格在字符串set声明中至关重要。

set domainName = "domain1" 

将设置域名空间空间 “域1”

删除空格。更可靠的set

set "domainName=domain1" 

设置域名域1

choice是一个批处理关键字和不良呃,选择一个标签或变量名。

+0

感谢您的意见。我删除了上面的空格,并且PSEXEC连接到计算机,然后在尝试启动'cmd'后失败。我不确定为什么会发生这种情况。我还将'continue'和'choice'更改为'nextstep'和'resetfile'。我甚至尝试将'if'语句中的''%domainName%''改为'“!domainName!”',但这没有什么区别。 –

+0

经过大量测试后,我现在在尝试连接到计算机后再次提示输入密码。当我输入与以前相同的密码时,它无法在计算机上运行密码,那么有什么想法? –

+0

我知道这不是psexec命令,因为我在批处理文件之外运行它,并且运行良好。 –

相关问题