回显命令|命令无法正常工作

echo cmd| command not working properly

我正在尝试 运行 notepad.exe 作为用户 "blain"。因此,我想在命令提示符下使用命令 runas /u:blain notepad.exe,它按预期工作并提示输入用户密码。我想自动化它,所以,假设 blain 的密码是 "pass",我输入 echo pass| runas /u:blain notepad.exe

无论我用什么替换 pass,命令总是 returns:

C:\Users\blain>echo pass| runas /u:blain notepad.exe
Enter the password for blain:
Attempting to start notepad.exe as user "BLAINE-WIN-10\blain" ...
RUNAS ERROR: Unable to run - notepad.exe
1326: The user name or password is incorrect.

需要注意的一件有趣的事情是,在按回车键后,该消息会立即出现。如果我手动输入密码错误,大约需要 2 秒告诉我密码错误(给出相同的错误消息)。

您不能通过管道传输到 runas,它的设计不允许这样做。

https://blogs.msdn.microsoft.com/oldnewthing/20041129-00/?p=37183

不过,您可以尝试在 VBScript 或 Powershell 中做类似的事情,如果可以的话?

VBScript 选项是将密钥发送到控制台提示,但密码必须以纯文本格式保存:

Set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.Run "cmd /c runas /user:domain\user program.exe" 
WScript.Sleep 500 
WshShell.SendKeys "password" 
WshShell.SendKeys "{ENTER}"

一个Powershell版本,它会不断提示用户直到他们正确(另存为.ps1,右键单击该文件并单击运行 with powershell):

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ds=New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine)
for(;;)
{
    $cred=Get-Credential
    if($ds.ValidateCredentials("$($env:computername)$($cred.UserName)",$cred.GetNetworkCredential().Password))
    {
        #Success, start your program
        start program.exe -Credential $cred
        exit
    }
}