Managing Network Drives

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Network drives remain an important part of the computing infrastructure. Users prefer mapped network drives to Universal Naming Convention (UNC) path names; it is easier to remember that financial records are stored on drive X than to remember that financial records are stored on \\atl-fs-01\departments\accounting\admin\financial_records\2002_archive. Administrators prefer mapped network drives as well; if financial records need to be moved to a new server, it is far easier to remap drive X than to expect users to memorize the new location.

Your scripts can use the methods in this section to manage network drive connections as part of a logon script or in any WSH script that has a need to connect to or disconnect from a network share. In fact, this represents one area where WSH has system administration capability not found in WMI. While both WSH and WMI let you enumerate mapped network drives (WMI by using the Win32_MappedLogicalDisk class), only WSH enables you to create and delete drive mappings.

WshNetwork provides three methods to work with network drive connections: MapNetworkDrive, RemoveNetworkDrive, and EnumNetworkDrives. You can use the three methods to manage network connections as part of a user s logon script or in any WSH script that needs to connect to or disconnect from a network share.

Mapping a Network Drive

You map a local drive letter to a shared folder using the WshNetwork object s MapNetworkDrive method. You can use MapNetworkDrive to connect directly to a shared folder or any child folder beneath a shared folder. MapNetworkDrive has two mandatory and three optional arguments, defined in Table 3.22.

Table 3.22   MapNetworkDrive Arguments

ArgumentTypeRequiredDefaultDescription
LocalNameString
table bullet
NoneThe drive letter, followed by a colon, assigned to the mapped network drive, e.g., "H:".
RemoteNameString
table bullet
NoneThe shared folder s UNC name, e.g., "\\ServerName\ShareName" or "\\ServerName\ShareName\FolderName".
UpdateProfileBoolean FalseBoolean value indicating whether the mapping information is stored in the current user s profile. The value True updates the current user s profile; False does not.
UserNameString NoneMaps the network drive using the credentials of someone other than the current user.
PasswordString NonePassword for the user identified by the UserName argument.

The following example demonstrates how MapNetworkDrive can be used in a user s logon script to connect to two network shares.

Set objNetwork = Wscript.CreateObject("WScript.Network") objNetwork.MapNetworkDrive "G:", "\\atl-fs-01\Sales" objNetwork.MapNetworkDrive "H:", "\\atl-fs-01\Users$\lewjudy" 

Drive G is mapped to the Sales share on the file server named atl-fs-01. Drive H is mapped to the user s home directory located directly beneath the hidden share named Users$ on atl-fs-01.

By default, MapNetworkDrive uses the access token of the current user to validate permissions prior to making the connection; you cannot map a drive to a folder unless you have permission to access that folder. In administrative scripts, it might be necessary to supply an alternate or elevated set of credentials to successfully connect to a share. You can use the optional UserName and Password arguments to supply a set of credentials that are used in lieu of the current user s access token. After the connection is established, the supplied credentials govern access to the network drive for the duration of the connection.

Two common conditions can cause MapNetworkDrive to fail:

  • The user running the script might not have sufficient permissions to connect to the target share.
  • The local drive letter might already be in use.

Left unchecked, both conditions can result in run-time errors. To avoid permissions-related or password-related issues, your best defense is to trap and handle the error using VBScript s On Error Resume Next statement. To handle a local drive letter that is already in use, you can forcibly disconnect the mapped drive or locate and use a different drive letter.

Unmapping a Network Drive

Your scripts can use the RemoveNetworkDrive method to unmap a network drive. If the network drive has a mapping between a local drive letter and the remote UNC path, the method requires the local drive letter as its single parameter.

If the network drive does not have a mapping between a local drive letter and the remote UNC path, the method requires the remote UNC path as its single parameter.

The script in Listing 3.36 unmaps a drive G.

Listing 3.36 Unmapping a Network Drive

1 2 
Set objNetwork = WScript.CreateObject("Wscript.Network") objNetwork.RemoveNetworkDrive "G:"

In addition to the mandatory parameter that specifies the mapped drive to be removed, the MapNetworkDrive method accepts two additional, optional, parameters:

  • A Boolean value that, if set to True, specifies that the method should unmap the drive regardless of whether it is currently in use. If the value is set to True, the user will no longer be able to save data to that drive, even if he or she has already opened a document from there. Instead, the user will have to save the document to an alternate location.
  • A Boolean value that, if set to True, specifies that the method should remove the drive mapping from the profile of the user.

Listing Current Network Drives

Your scripts can use the EnumNetworkDrives method to retrieve a list of the current mapped network drives on a computer.

The EnumNetworkDrives method returns a collection that holds pairs of items: network drive local names and their associated UNC names. The collection is zero-indexed; the even-numbered items in the collection are the local drive names, and the odd-numbered items are the associated UNC paths.

A sample network drive collection is shown in Table 3.23. In this sample collection, drive D (index number 0) maps to \\atl-fs-01\users\kmyer (index number 1).

Table 3.23   Sample Network Drive Collection

Index NumberValue
0D:
1\\atl-fs-01\users\kmyer
2E:
3\\atl-fs-02\accounting
4F:
5\\atl-fs-03\public

A script can retrieve both pieces of information about each mapped network drive by iterating through the collection returned by EnumNetworkDrives and retrieving two items from the collection during each iteration. An example of this is shown in Listing 3.37.

Listing 3.37   Listing Current Network Drives

1 2 3 4 5 
Set objNetwork = WScript.CreateObject("WScript.Network") Set colDrives = objNetwork.EnumNetworkDrives For i = 0 to colDrives.Count-1 Step 2    Wscript.Echo colDrives.Item(i) & vbTab & colDrives.Item (i + 1) Next

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

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