Recipe3.12.Mapping a Network Drive


Recipe 3.12. Mapping a Network Drive

Problem

You want to map a drive to a folder on a remote computer.

Solution

Using a graphical user interface

  1. Open Windows Explorer.

  2. From the menu, select Tools

    Beside Drive, select the drive letter you want to assign.

  3. Beside Folder, enter the UNC path to the network share you want to map.

  4. If you want the drive to be persistent, make sure the box beside Reconnect at logon is checked.

  5. By default, your current credentials will be used to access the network share, if you want to use alternate credentials, click the different username link. Enter a user name and password and click OK.

  6. Click Finish.

Using a command-line interface

The following command maps a drive to a network share point:

> net use <Drive> <Share>

The following example maps \\rtp01\myshare to the N: drive using your current credentials:

> net use N: \\rtp01\myshare

The following example maps a persistent drive using alternate credentials:

> net use N: \\rtp01\c$ /user:amer\rallen /savecred /persistent:yes

The following command lists all network connections including mapped drives:

> net use

The following command deletes the N: network drive:

> net use N: /delete

Using VBScript
' This code creates a mapped drive to a network path. ' ------ SCRIPT CONFIGURATION ------ strDrive = "<Drive>" ' e.g., N: strPath = "<Path>"   ' e.g., \\rtp01\c$\temp strUser = "<User>"   ' e.g., AMER\rallen strPassword = "<Password>" boolPersistent = True  ' True = Persistent ; False = Not Persistent ' ------ END CONFIGURATION --------- set objNetwork = WScript.CreateObject("WScript.Network") objNetwork.MapNetworkDrive strDrive, strPath, boolPersistent, _                            strUser, strPassword WScript.Echo "Successfully mapped drive"

Discussion

Mapping a drive to a folder on a remote server is primarily done for convenience. There is nothing you can do with a mapped drive that you can't also do with a UNC path (e.g., \\rtp01\myshare). However, some applications may not support accessing files via a UNC path, so you might need to use a mapped drive instead. A mapped drive is more convenient if you access a remote server frequently from a tool such as Windows Explorer. Instead of typing a long UNC path, you can simple type the drive letter and access the folder much quicker. And if you need to access the remote folder using alternate credentials, creating a mapped drive can save you even more time because you can store the credentials with the mapped drive so that when your computer starts up, the drive is automatically mapped using the alternate credentials. But be warned, this approach is also a great way to create account lockouts following password changes.

You can't use the cd command within a CMD session to change directories into a UNC path. You can, however, use the pushd command, which creates a temporary drive for the UNC path (much as if you were mapping a drive) and cd's into that drive. After you end the CMD session, the drive is dismounted and the previously assigned drive letter becomes available again.


See Also

MS KB 149861 (How Authentication Works for Net Use Command) and MS KB 308582 (How to connect and disconnect a network drive in Windows XP)



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net