Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch file needed

Posted on 2010-09-14
10
Medium Priority
?
452 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
LVL 6

Accepted Solution

by:
ajb2222 earned 2000 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 14
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 14
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 14
ID: 33684553
lol I'm honestly a little surprised you went with the solution you accepted..
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

824 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