【问题标题】:enter username password when prompted in windows batchwindows批处理提示时输入用户名密码
【发布时间】:2020-09-10 14:20:57
【问题描述】:

我需要在 Windows 中为一个实用程序编写批处理文件。但是每次我从命令提示符运行时,该实用程序都会询问用户名和密码。我想在命令提示符中提示时传递用户名和密码。现在命令运行后,用户名和密码的问题出现了 qs 2 个单独的问题,例如: 输入姓名: 输入密码:

这成为一个问题,因为仅将 %1 和 %2 作为参数传递并没有帮助,因为 uname 和 pwd 作为单独的 qs 来。请帮我在 windows 中写一个批处理...我对 windows prog 完全陌生...

【问题讨论】:

  • 如果您有源代码,更好的选择是修改实用程序。

标签: batch-file echo


【解决方案1】:

您是否检查过该实用程序:

  • 没有用于输入用户名/密码的命令行参数?
  • 没有其他输入用户名密码的机制,如using Environment variables

我假设您检查了所有这些,除了手动输入用户名和密码之外别无选择。

在这种情况下,您应该查看AutoHotkey
它用于创建可以自动化 UI 输入的脚本,尤其是运行需要键盘和鼠标控制的应用程序。

还有一个名为SendKeys.net 的小实用程序用于批处理文件。
不过还没有测试过。

【讨论】:

  • 感谢 Renaud 的回复...实际上这是第三方实用程序,我们无法控制它。通过命令后,它总是要求我们必须手动输入 uname 和 pwd。现在,我需要编写一个批处理来自动化整个过程。我尝试了所有其他方法,但无法...提示时在 Windows 批处理中处理输入 uname 和 pwd 的方式??
【解决方案2】:

雷诺是对的,命令行参数将是最好的解决方案。查看您的实用程序手册或尝试实用程序.exe /?

以下解决方案可能有效(也可能无效) 将您的答案写入文件(例如“credentials.txt”)(确保它只包含两行)

myusername
mypassword

那就试试

myutility < credentials.txt

出于安全原因,如果编写了该实用程序,这将不起作用。

【讨论】:

    【解决方案3】:

    此任务可以通过 Windows 中的 VBS 完成。您可以使用 SendKeys() 来模拟键盘操作。它的工作方式类似于 Linux 中的命令“expect”。 BAT 在这里帮不了你。

    【讨论】:

      【解决方案4】:

      您可以调用这个批处理-HTA-VBScript 混合文件(GUI 登录提示):

      <!-- :
      
        :: LoginGUI.bat | Wasif Hasan | Sep 2020
        :: This is a Batch-HTA-VBScript hybrid that will show a GUI window to login
        :: Save it as a .bat file and run it in your other batch files
        :: and returns the username and password as a result (to STDOUT) (in "username;password" format)
        :: If you enter no password in the dialog it will show an error message box and persist 
        :: Arguments: "BatchFilePath" "prompt (To Show beside the password box)" BoolAllowBlankUsername BoolAllowBlankPassword
        :: Default prompt is "password"
      
        @echo off & setlocal EnableDelayedExpansion
        if "%*"=="" (
        for /f "tokens=* delims=" %%a in ('echo "Password:" False False ^| mshta.exe "%~f0"') do set "pass=%%a"
        ) else (
        for /f "tokens=* delims=" %%a in ('echo %* ^| mshta.exe "%~f0"') do set "pass=%%a"
        )
        echo !pass! & exit /b 0
      
      -->
      
      <!DOCTYPE html>
      <html>
      <head>
      <title>Password box</title>
      <hta:application
        applicationName="Password box"
        border="thin"
        maximizeButton="no"
        minimizeButton="no"
        showinTaskbar="no"
        scroll="no"
        singleInstance="yes"
        contextMenu="no"
        selection="no"
      />
      <script type="text/vbscript">
        Sub Window_OnLoad()
          Dim fso2, strPrompt, dblQuote
          window.resizeTo 320,180
          Set fso2 = CreateObject("Scripting.FileSystemObject").GetStandardStream(0)
          strPrompt = fso2.ReadLine
          dblQuote = Chr(34)
          strPromptX = Split(strPrompt," ")
          strPrompt = strPromptX(0)
          strPrompt = Replace(strPrompt, dblQuote, "")
          Document.All.prompt.innerHTML = strPrompt
          document.All.username.Focus
        End Sub
        Sub Validate() 
          Dim fso, GetPassword, GetUsername
          GetPassword = Document.All.Password.Value
          GetUsername = Document.All.username.Value
          If GetPassword = "" Then 
            If Not allowBlankPassword = True Then
              MsgBox "Please enter a password!",48,"Password box"
            End If
          ElseIf GetUsername = "" Then
            If Not allowBlankUsername = True Then
              MsgBox "Please enter a username!",48,"Password box"
            End If 
          Else
            Set fso = CreateObject("Scripting.FileSystemObject").GetStandardStream(1)
            window.Close(fso.write(GetUsername & ";" & GetPassword))
          End If
        End Sub
      </script>
      </head>
      <body style="background-color:lightblue;font-family:Tahoma;">
      <div align="center">
      <span id="username_l">Username:</span>
      <input type="text" size="20" id="username" /><br /><br />
      <span id="prompt">Password:</span>
      <input type="password" size="20" id="Password" />
      <p><input type="button" value="Login" onClick="Validate()" /></p>
      </div>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-10
        • 2016-01-30
        • 2014-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-11
        相关资源
        最近更新 更多