Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to get a batch file to automatically enter the password into the "runas" program, but I can't get it to work. Here's my batch file so far:

echo password | runas /user:testuser c:/path/to/my/program.exe

However, I get an error saying "unknown user name or bad password". I have also tried:

runas /user:testuser c:/path/to/my/program.exe < c:/path/to/file/containing/password.txt

This comes back with the same error. Does anyone know a way to do this? What I'm trying to do eventually is get the batch file to read a username and password from a config file, and then start "program.exe" as that user.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
552 views
Welcome To Ask or Share your Answers For Others

1 Answer

The Batch-JScript hybrid script below do what you want. Save it with .bat extension.

@if (@CodeSection == @Batch) @then
@echo off
start "" runas /user:testuser c:/path/to/my/program.exe
CScript //nologo //E:JScript "%~F0"
goto :EOF
@end
WScript.CreateObject("WScript.Shell").SendKeys("password{ENTER}");

For further details, see this post


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...