How do you auto-input text into a password field? (Batch) - batch-file

Part of my program's job is to create a new user account, elevate it to admin, and then run programs AS that user.
To do this, I would use the following code:
runas /user:hostname\AddedUserProfile /savecred program.exe
This works, but there is a problem. To use the runas command, the user you are running the program through, must have a password. Upon running the command, it asks you for that user's password. My program is supposed to run behind the scenes, and without the user being able to see it (Don't worry. I already have code that invisibly starts programs, so that is NOT the issue here). I can't find a way to automatically enter text into the password entry field (and yes, I know the password doesn't show visibly). I've tried having the program echo the password, and I've even tried to have it rerout the output of an echo command, into the input of the password field.
echo password | runas /user:hostname\AddedUserProfile /savecred program.exe
Even that doesn't work! it just gives me an error:
RUNAS ERROR: Unable to run - taskmgr.exe
-2147467259: Unspecified error
I really am at my wit's end on this one. If there is a way to do this other than runas, I'd LOVE to know! I'd prefer that it was using runas, though.
Edit:
It seems that the error is due to an incorrect password entry. I can't figure out why it would be entering it incorrectly, however. If I do not use the /savecred, then I get an error message that actually says that the password is incorrect.

Auto input password by using a pipe method like this:
echo PasswordHere | runas /user:hostname\AddedUserProfile /savecred program.exe
That mean, it will type "PasswordHere" when runas.exe need user input. I'm not tested it yet, so you have to try it out. I always use this "pipe" way to access my FTP server.

Related

How to disable input in batch script?

I have a batch script (mine) that launches another batch script (theirs). Their batch script queries user input as part of the process, and I do not have access to modify it.
I need to suppress their batch script from querying input, meaning that when their batch script outputs: Press enter for step 2..., I want the user to be unable to interact with the script using their keyboard, hence the script should look like it's frozen.
How do I call their script from my script in such a way that the user is unable to interact with the input requests of their script?
Actually summing up the comments to make this not an unanswered question. You can try using the | (pipe) operator:
echo Haha! You will not be able to press any key!! | their.bat
Which will redirect STDOUT (Standard Out) of command echo Haha! You will not be able to press any key!! (Haha! You will not be able to press any key!!) to STDIN (Standard Input) of command theirs.bat.
Or, even read from nul:
their.bat < nul
I recommend reading https://ss64.com/nt/syntax-redirection.html and What does "&&" in this batch file? (second answer is better here.).

Passing command-line arguments/parameters to AutoIt executables

My AutoIt script:
WinWaitActive("User Authentication","","10")
If WinExists("User Authentication") Then
; Enter a username.
Send("prabu{TAB}")
Send("{TAB}")
;Enter a Password.
Send("Password")
Send("{TAB}")
Send("{SPACE}")
;Press Authenticate button.
Send("{TAB} {ENTER}")
EndIf
It waits for an authentication popup window to gain focus,
enters a username and password,
then presses the "Authenticate" button.
I "compiled" it to an .exe file and execute it from Selenium using:
Runtime.getRuntime().exec("C:\\Users\\Prabu\\Documents\\ds.exe");
But I want it to enter a different username and password every time. I intend to provide these to the script using command-line arguments (parameters if you will).
Is it possible to pass arguments/parameters to AutoIt scripts? If so, how should this be done and how do I access arguments/parameters provided to my script?
changes in AutoIt Script
$username = $CmdLIne[1]
$password=$CmdLine[2]
Send($username)
Send($password)
in java
String command="C:\\Users\\Prabu\\Documents\\ds.exe \"username1\" \"password1\"";
Runtime.getRuntime().exec(command);
reference
https://www.autoitscript.com/autoit3/docs/intro/running.htm#CommandLine
http://seleniumocean.blogspot.in/2014/11/its-time-for-autoit-parameterizing.html

Why doesn't this batch file work? DSADD bad syntax?

Really stumped here and I'm sure the answer is simple. Simply put, I'm creating a bunch of AD accounts based on input from a spreadsheet. I've been able to concatenate the fields into a DSADD command that I run each time a new user is onboarded. For example:
#echo off
dsadd user "CN=Bilbo Baggins,ou=Hobbiton,ou=Shire,ou=MiddleEarth,dc=lotr,dc=com" -fn Bilbo -ln Baggins -display "Bilbo Baggins" -upn bbaggins#lotr.com -pwd damnring! -email bbaggins#lotr.com -samid bbagins -mustchpwd no -disabled no
When I paste that into a cmd line and hit enter all is well, the AD account is created. I have hundreds of these - so I thought I would get fancy and put each of these commands on their own line and call it, oh, I don't know "add_all_users.bat". However when I call that from a command line or PS instance it errors out with "The syntax is incorrect".
What am I missing here?

Net use Command : Windows CE

I'm trying to send network credentials using Net Use command.
This is what I have:
#echo off
net use \\<serverName>\<shareFolder>\ passw0rd /USER:domain.com\UserName
PAUSE
This automatically inserts the username and Domain but for some reason not the password!!
I have tried it like this as well:
#echo off
net use \\<serverName>\<shareFolder>\ /USER:domain.com\UserName passw0rd
PAUSE
I have checked the paths I'm using and they definitely work. When i copy them out and paste in RUN they work.
Same goes for the username and passwords.
Everything in google search is purple cause I've clicked on all the links :/
Ok, I thought I had it...
I tried it like this:
#echo off
net use \\<serverName>\<shareFolder>\ <mapName> /USER:domain.com\UserName passw0rd
PAUSE
And it worked, but only because i entered the password before i tried it like this and then it remembered the password.
So I'm still looking for a way.
Please help.
Does the password contain special characters like % / ~ or similar characters with special meaning in batch files?
Yes, then enclose the password in double quotes.
Use also double quotes around UNC path and test if that makes a difference.
What I miss in all your net use commands is the device name usually specified left of UNC path to shared folder to map the shared folder to a drive letter. Therefore it could be that the password is interpreted as device name.
But according to your question you want to pass only the user credentials and password to be able to access the shared folder using UNC path. However, I suggest to test if it makes a difference really mapping the shared folder to a drive letter. I don't know if the password string is interpreted as device name if no device name specified and can't test it by myself at the moment.
#echo off
net use <MapDriveName> \\<serverName>\<ShareFolder>\ /PASSWORD:<Passw0rd> /USER:<UserName>
PAUSE
This one works for me. Finally :)
Hope this can help someone else as well.
net use Y: \server_name OR ipaddress\shared /user:domainname\%username%

BAT script - how to automatically insert additional arguments?

I want to write a simple bat script but I cannot deal with one thing.
I run a command and it gives few messages and it's waiting for my login. I want to write a script which will do it.
For example, I run a command:
myCommand.bat
and it gives:
msg1...
msg2...
...
type your login:_
Now, I have to put my login.
How can I do it automatically?
BAT script is the best way for me :)
You probably can solve this by either
placing your username and/or password directly in myCommand.bat
placing your username and/or password in a file "myPassword.txt" and using redirection: <myPassowrd.txt myCommand.bat
But, you probably should not do either of those for security reasons.
Addendum:
If your batch script is calling an executable that is prompting for the login credentials, you may have 2 options
The executable may have a command line option(s) that allows you to specify username
and/or password. You would have to research your executable to find out what they are.
If the executable is not expecting any other input, you might be able to pipe the username and/or password into the program. For example, if the program asks for username and then asks for password, something like this might work:
(echo yourName&echo yourPassword)|yourProgram.exe
Security concerns still apply

Resources