Solved

Script to Map Network Drives

Posted on 2013-01-24
8
676 Views
Last Modified: 2013-02-11
I'm trying to write a batch file that will automatically map network drives upon restart of a number of workstations running Windows7, Vista, or XP.  

There are lots of postings on this topic in EE and elsewhere.  It appears very straightforward, although I have absolutely no prior experience with this stuff.  See the following as an example:  http://community.spiceworks.com/how_to/show/1487-how-to-create-a-batch-file-for-network-drive-mapping.

I followed the instructions above:
1.  Open notepad.
2.  Typed the following:  net use G:\\servername\D
3.  Saved file as a .bat (NOT a .txt file)

To test it, I simply saved it on my desktop and double-clicked it to run it.  I expected it to create a mapped network drive with letter designation "G".  It doesn't appear that it did anything.

I've tried running it on both Windows 7 and Vista.  I've also tried running it as administrator.  No joy.

Ultimately, I would like this batch file to map multiple drives from all the computers in my office.  

Any help would be greatly appreciated.

thanks,
darin
0
Comment
Question by:DarinOBrien
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 5

Accepted Solution

by:
Harsem earned 350 total points
ID: 38816593
Hello,

that syntax is almost correct - and you also have to make sure that the drive is shared. Windows has a number of default administrative shares, but they are hidden.

To access these administrative shares you can use:

net use G: \\servername\D$
(note the space between  the ":" & "\\")

However I think it would be better if you shared a folder on the server (Servername) and then you can access it via:

net use G: \\servername\Folder

Hope this helps

Jens
0
 
LVL 26

Expert Comment

by:pony10us
ID: 38816658
Microsoft is good at giving help for commands:


The syntax of this command is:

NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
        [/USER:[domainname\]username]
        [/USER:[dotted domain name\]username]
        [/USER:[username@dotted domain name]
        [/SMARTCARD]
        [/SAVECRED]
        [[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]


As for a batch file you are correct to save it as a ".bat"

@echo off
net use g: \\servername\Folder /persistent:yes

Open in new window

0
 

Author Comment

by:DarinOBrien
ID: 38816696
The space between the ":" and the "\\" was the problem!

I'm actually trying to map a drive to a shared folder on the server ("D").  It's not hidden so I assume it's not an administrative share?
 
Can you also please tell me how to make this batch file first delete ALL mapped network drives that might be on the workstation?  Alternatively, it would probably work to delete any existing shares with the same letter designation (e.g. I want to create a new share "G" so I want to delete and pre-existing shares "G").

Lastly, can you tell me where to place this batch file so that it runs automatically everytime the users restart their computers?  Again, working with XP, Vista and Windows7 so maybe they're different locations?

Thanks!
darin
0
 
LVL 2

Assisted Solution

by:beefstu123
beefstu123 earned 50 total points
ID: 38816714
Hi,
here is another way to do it using IP address:

EG:

net use G: \\192.168.0.121\[folder name]

also does the server have a password if it does you have to apply it to your bat file:

net use G: \\192.168.0.121\[folder name] /user:[username] [password]
Note there is a space between the username and the password

EG:

net use G: \\192.168.0.121\folder1 /user:admin1 user1
admin1 is the username and user1 would be the password

and adding another location is just adding another line to your bat file, save and test.
To check if your folders are shared just type the server ip address in to a folder address bar
EG: \\192.168.0.121
and it will show what you have shared on that system
Note: you will have to setup static IP addresses for the server and any other system that are hosting the shared files if you dont' you will have to get the ip address every time the server restarts, to resolve this use Harsem's method and use the system name instead of the ip address.

Once that is done all you have to do is to add it to start up folder and it will run every time you system starts up (put a copy on the desktop as well sometimes the network hasn't been detected when start up tries to run the bat file)

hope it works
beefstu123
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 5

Assisted Solution

by:Harsem
Harsem earned 350 total points
ID: 38816746
Hello,

to remove a share is easy:
net use /d g:

I do not know how to delete all, but that will remove G:

To automatically run that is easy:
(from www.computerhope.com/issues/ch000322.htm )  
Windows 98, XP, NT, 2000, Vista and later users

    Create a shortcut to the batch file.
    Once the shortcut has been created right-click the file and select Cut.
    Click Start, Programs, right-click the Startup folder and click Open
    Once the Startup folder has been opened click Edit and paste the shortcut into the startup. Any shortcuts in the startup folder will automatically start each time Windows starts.

Hope this helps

Jens
0
 
LVL 26

Expert Comment

by:pony10us
ID: 38816869
If the computers are on a Active Directory domain then you could place the commands in the login script or push via GPO.
0
 
LVL 77

Assisted Solution

by:Rob Williams
Rob Williams earned 100 total points
ID: 38816903
A good idea is to disable persistent mappings and delete all existing mappings before creating new mappings to assure there are no conflicts with exiting mappings or ones created by users.  To do so use something similar to:

Net Use /persistent:no
Net Use * /delete
Net Use X: \\ServerName\ShareName1
Net Use Y: \\ServerName\ShareName2
Net Use Z: \\ServerName\ShareName3

You may also wish to review the following article as to how to apply the mappings, if you have a domain.  A more common practice today is group Policy preferences, but that is not 100% effective with XP machines.  The article also discusses other options:
http://blog.lan-tech.ca/2012/03/01/dive-mapping-basics/
0
 

Author Closing Comment

by:DarinOBrien
ID: 38877397
I used harsem solution first shown above:

net use G: \\servername\Folder

In combination with a bunch of these to delete all prior mapped network drives:

net use /delete X:

I avoided using IP addresses for fear that they would change.


Thanks to everyone who participated.  I especially appreciate the link to detailed reference material.

darin
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A common practice in small networks is making file sharing easy which works extremely well when intra-network security is not an issue. In essence, everyone, that is "Everyone", is given access to all of the shared files - often the entire C: drive …
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…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

13 Experts available now in Live!

Get 1:1 Help Now