Solved

Batch file needed

Posted on 2010-09-14
10
447 Views
Last Modified: 2012-05-10
I need a batch file that will
1. Prompt for a username
2. Map to that user's network drive

For example:

Prompt
Enter your username:  msmith

Maps to path
//server/share/msmith


Thanks!



0
Comment
Question by:esauseesau
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 10

Expert Comment

by:jorlando66
ID: 33672983
Why wouldnt you just do this in the initial login script or winthin user manager?  
0
 

Author Comment

by:esauseesau
ID: 33673075
This will be done on laptops that are available for checkout.  Our plan is to create a one generic account that all users will log on to.  This way we won't have to deal with password changes, new users and getting them logged on to our network every time they use the laptop.  They will then use this batch file to access their individual network drive.
0
 
LVL 5

Expert Comment

by:richy92
ID: 33673136
This will work, although it would be better done from user manager or a login script :)

@echo off
set INPUT=
set /P INPUT=Enter Username%
net use p: \\server\share\%input%


That will map the drive to P: you can change this if you want
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 6

Accepted Solution

by:
ajb2222 earned 500 total points
ID: 33673221
You will also need to promt for a password.  

echo "Enter Username"
set /p UserName=

echo "Password"
set /p PassWord=

net use \\server\share\%UserName% %PassWord% /user:domain\%UserName%
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33673768
You will also want to wrap your net use in quotes, and you will also want to deleted existing drive mappings.

Also you do not need to pre-define the values of the varriables (as this will not really do anything) and you WILL want to set them to fake values afterward (to keep others from finding the values later)

Also you should NOT use %UserName% in the script because this is a system managed variable.

Finally you should test for bad credentials as best as possible and handle them.

ECHO OFF
::- Set Default Values here:
SET "Creds=NOMATCH"
SET "HomeDrive=\\YourServer\YourShare$\Your Folder"

::- UnMap All Existing Drives here:
NET USE * /D /Y

:SetCreds
CLS
IF /I "%Creds%"=="BAD" ECHO The Credentials you entered "%UNAME%" and "%PWord%" Are not valid please try again.&ECHO.
SET /P UName=Please Enter Your User Name (Domain\UserName): 
SET /P PWord=Please Enter Your Password: 

::- Attempt Drive Mapping here:
NET USE H: "\\%HomeDrive%\%Uname%" /User:"%UName%" %PWord% 2>&1 | Find /I "Logon failure: unknown user name or bad password." > nul && SET "Creds=BAD"&GOTO :SetCreds

::- Cleanup the environment here:
SET "Creds=NOMATCH"
SET "HomeDrive=NOMATCH"
SET "UName=NOMATCH"
SET "PWord=NOMATCH"
ECHO Home Drive Mapped Sucessfully!

::- Continue on with your script here.
::-  NOTE: If you need to use the above UN and PW again in items below you will want to move the entries where I set them back to "NOMATCH" to the end of the scrript.

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33673846
It would perhaps be simplest / better to use the NET USE to do the prompting for credentials since then input is hidden:

i.e.

@echo off
setlocal
set /P mapuser=Enter username:
if not "%mapuser%"=="" (
  echo Please enter your password when prompted by the following command:
  net use H: \\server\%mapuser% /user:domain\%mapuser%
)

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33673861
but I like the more belt and braces approach of QCubed, wasn't there when I started typing.
0
 
LVL 10

Expert Comment

by:jorlando66
ID: 33674239
I suspect you will have problems with this approach, but good luck.
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33674505
Lol, thanks very much Dragon, I haven't heard the term "Belt and Braces" before, but I'll take is as a complement ^^

(I assume that Belt & Braces probably means that the item in question is built with additional attention to handling possible failure points? Eg compensating for inherent deficiencies)
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33684553
lol I'm honestly a little surprised you went with the solution you accepted..
0

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question