2017-04-04 153 views
0

我试图以用户“blain”的身份运行notepad.exe。因此,我想在命令提示符中使用命令runas /u:blain notepad.exe,该命令按预期工作并提示输入用户密码。我想自动化它,所以,假设痘痘的密码是“通过”,我输入echo pass| runas /u:blain notepad.exeecho cmd |命令不能正常工作

不管我更换同传,命令总是返回:

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秒才能告诉我它是错误的(给出相同的错误信息)。

回答

1

您不能管道到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,右键单击该文件,然后单击运行与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 
    } 
} 
+0

无赖...我的用例类型的铰链能够管道密码。这是一个不安全的方法,但我想要做的是创建一个空的文本文件,如果用户输入了正确的密码 – Blaine

+0

@Blaine我会看看我能不能提出一个VB解决方案,给我几分钟... –

+0

好的。我邀请你参加一个聊天,如果你想让我给你整个背景/要求(有点太长了,可以在这里发布) – Blaine