Recipe 3.12. Mapping a Network DriveProblemYou want to map a drive to a folder on a remote computer. SolutionUsing a graphical user interface
Using a command-line interfaceThe 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" DiscussionMapping 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.
See AlsoMS KB 149861 (How Authentication Works for Net Use Command) and MS KB 308582 (How to connect and disconnect a network drive in Windows XP) |