BAT script - how to automatically insert additional arguments? - winforms

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

Related

How to write a full path in a batch file having a username with spaces?

Essentially I am referencing a path in my batch file that includes the username.
Here is what I mean:
C:/users/%username%/
Using %username% does seem to work when refrecning the user currently logged in to the machine, but fails if the username has a space. For example the username jlows works no problem, but j lowes does not and shows an error saying user "j" cannot be found.
What can be added to the path to account for this situation?
Always quote paths in batch files. This way, it helps you in order to avoid misbehaviours like this one. In most of the cases, system considers, e.g. the path random which doesn't exist because you have entered random test.
So, replacing / with \ as this is the default Windows separator, will give you:
"C:\Users\%username%"
However, there is a shorter version, userprofile, which stands for C:\Users\%username%, exactly what you have. Use it like:
"%UserProfile%"

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

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.

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

Writing secure batch files

I have a simple batch file:
#echo off
set username=user
set password=pass
::do some things with the username and password
the user could just call the batch file from cmd, and type set after my batch file has run it's course and see the username and password.
Is there any way to fix this?
(excluding bat to exe converters)
Prompt for password then save it
This script here to use the password associated with a batch file from a hidden stream attached to the file, prompt for the password if not present and save it. If you know how to work out what is going on in the batch you could do the same and get the password to work with, but to a casual observer it is not at all obvious how it works.
Password entry obscured from batch file
Script to prompt for and enter password in hidden manner.
Password hidden using ADS
Script to store password in an extra data stream with the batch file that you can't see but is there and can be read by the batch file itself.

Giiving inputs to CMD exe by daynamically

I have an application ,i want to run on using task scheduler .
now i want to set up execution through command line arguments on my local system.
i created set up but ask password i need to enter that password dynamically i.e from file are as arguments etc.
Please suggest me any solutions to skip password option or to enter password dynamically.
Does this answer your question? It explains how to call a batch file with command line parameters.
How do I pass command line parameters to a batch file?

Resources