Solved

Batch file needed

Posted on 2010-09-14
10
442 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now