Solved

Batch file needed

Posted on 2010-09-14
10
448 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

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

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

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…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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