0

I am trying to run notepad.exe as user "blain". Therefore I would like to use the command runas /u:blain notepad.exe in command prompt, which works as expected and prompts for the user password. I would like to automate it, so, assuming blain's password is "pass", I type echo pass| runas /u:blain notepad.exe.

No matter what I replace pass with, the command always 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.

An interesting thing to note is that the message appears instantly after pressing enter. If I manually enter the password wrong, it takes ~2 seconds to tell me it's wrong (giving the same error message).

1 Answer 1

1

You can't pipe to runas, it's designed to not allow this.

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

You could try doing something similar in VBScript or Powershell though, if that's an option?

A VBScript option would be to send the keys to the console prompt, the password would have to be saved in plain text though:

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}"

A Powershell version, it will keep prompting the user until they get it right (save as .ps1, right click on the file and click Run 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
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

bummer... My use-case kind of hinges on being able to pipe a password. It's an insecure method, but what I want to do is create an empty text file IF the user has entered their correct password
@Blaine I'll see if I can come up with a VB solution, give me a few mins...
Alright. I invited you to a chat if you want me to give you the whole backstory/requierments (a bit too long to post here)
@Blaine Good, I've added another version, which should work much better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.