PRB: EnumNetworkDrive Will Not Return Remembered Drives (303209)



The information in this article applies to:

  • Microsoft Windows Script Host 1.0
  • Microsoft Windows Script Host 2.0

This article was previously published under Q303209

SYMPTOMS

The EnumNetworkDrive method from Windows Script Host (WSH) only returns connected drives. This can be a problem if a remembered drive exists and EnumNetworkDrives is being used to determine the next available drive letter. If an attempt is made to map to that same drive letter as a remembered drive, an error message is returned:
800704B2 An attempt was made to remember a drive that had previously been remembered.

CAUSE

This is caused by a limitation of the EnumNetworkDrives method. This method only scans for a connection with a status of "CONNECTED".

RESOLUTION

The following are two ways to work around this problem:
'Method 1
On Error Resume Next
Set WshNetwork = WScript.CreateObject("WScript.Network")

Do
   err.Clear
   AvailableLetter = GetNextLetter(AvailableLetter)
   WScript.Echo AvailableLetter 
   WshNetwork.MapNetworkDrive AvailableLetter, "\\Server\Share"
Loop Until err.Number = 0 'This could possibly not return so you may
                          'want to say "Loop Until err.Number <> xxxxxxx
                          'where xxxxxxx is the err number you receive.	
Set WshNetwork = Nothing

Function GetNextLetter(DriveLetter)
   'Start x at the ascii value of the drive letter to start the search
   'unless something is passed in. This sample uses capital letters and
   'starts at F.
   If IsEmpty(DriveLetter) Then
   	x = 70
   Else   	
	x = Asc(drive)
   End If
   
   Set oDrives = WshNetwork.EnumNetworkDrives

   'Step by two since the first item in the collection is the drive letter
   'and the second item is the network mapping 
   For i = 0 to oDrives.Count - 1 Step 2	   
	If chr(x) & ":" = oDrives.Item(i) Then
		x = x + 1
	End If
   Next
   
   GetNextletter = chr(x) & ":"
End Function
				

'Method 2

'This will list all mapped connections
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%comspec% /c net use > somefile.txt"

'This will map to the next available drive letter
WshShell.Run "%comspec% /c net use * \\servername\sharename"
				

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

You can reproduce this problem by mapping a network drive to a remote computer on a separate domain. Create a login script that maps a new network drive based on the EnumNetworkDrives method, such as the following:
On Error Resume Next
Set WshNetwork = WScript.CreateObject("WScript.Network")

Set oDrives = WshNetwork.EnumNetworkDrives

x = 70			'This sample starts at "F" and uses capital letters

Do
	DriveLetter = chr(x) & ":"
	LetterFound = False
	For i = 0 to oDrives.Count - 1 Step 2	   
		If DriveLetter = oDrives.Item(i) Then
			LetterFound = True
			x = x + 1
			If x > 90 Then 
				WScript.Echo "No free drive letters"
				WScript.Quit
			End If
			Exit For
		End If
	Next
Loop Until LetterFound = False
WScript.Echo DriveLetter

WshNetwork.MapNetworkDrive DriveLetter, "\\Server\Share"

If err.number <> 0 Then
	WScript.Echo err.number & ": " & err.description
	err.clear
End If
				
Log off the computer and then log back on. You should be prompted to enter the password to reconnect to the drive. Click cancel and let the login script run. You should see the error as soon as the script tries to map that drive letter.

Modification Type:MinorLast Reviewed:3/20/2004
Keywords:kbDSWManage2003Swept kbprb KB303209