Solved

Batch file needed

Posted on 2010-09-14
10
450 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
[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 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