• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1905
  • Last Modified:

Get User Input in a bat file

Hi All,

I'm trying to write a bat file with the use of the "Net Use" command like this:
net use k: \\mypcname\c$ password /User:Operator1

I don't want to hard code the password in the bat file. How can I ask the user to input this password while running this bat file. Please advise. Thanks.
0
cplau
Asked:
cplau
  • 3
  • 2
  • 2
  • +1
1 Solution
 
sirbountyCommented:
set /p pw=Enter your password:
0
 
fhmcCommented:
how about

mapk.bat
::capture user input and store it in an environment variable
set /p netusepw=Please Enter Your Password:
::execute net user command and pass environment variable
net use k: \\mypcname\c$ %netusepw% /user:Operator1
::clear previously stored data in environment variable
set netusepw=asdfasdf
:end
0
 
fhmcCommented:
argh...  sirbounty beat me to it.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
sirbountyCommented:
just use
setlocal
at the head of your batch file - that way you won't have to clear the password, regardless of the variable you use for it...
0
 
Steve KnightIT ConsultancyCommented:
Can't try it right now but... if you just leave the password off the net use line does it not prompt the user for the password anyway if it doesn't match their current one?

Steve
0
 
oBdACommented:
The correct way is actually to use an asterisk instead of leaving out the password in the "net use".
That way,
- the password will not be displayed when it's entered
- Windows won't try first to authenticate with the current user's password, causing possible lockouts and auditing events.

net use k: \\mypcname\c$ * /User:Operator1
0
 
Steve KnightIT ConsultancyCommented:
Thanks oBdA, thats what I was thinking of... haven't done it in ages as of course 99.9% of the time there is domain authentication involved or the users have the same user/passwords on the different systems.

Either way it's a better solution IMO than having a prompt where the user has to type their password into a visible prompt and store it in an environment variable, might aswell just put the password in the batch file if so!

Steve
0
 
fhmcCommented:
I agree, dragon-it's approach is far superior to the env. variable approach (I didn't realize "net use" could natively prompt for credentials.)  noted.

that said, I don't agree the security concerns associated to defining and almost immediately clearing a password in an environmnet variable is anything close to the concerns associated w/ storing a password in clear text in a .bat file...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now