Microsoft KB Archive/303209

= PRB: EnumNetworkDrive Will Not Return Remembered Drives =

Article ID: 303209

Article Last Modified on 3/20/2004

-

APPLIES TO


 * Microsoft Windows Scripting Host 2.5
 * Microsoft Windows Scripting Host 2.5

-



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 &quot;CONNECTED&quot;.



RESOLUTION
The following are two ways to work around this problem:

'Method 1 On Error Resume Next Set WshNetwork = WScript.CreateObject(&quot;WScript.Network&quot;)

Do err.Clear AvailableLetter = GetNextLetter(AvailableLetter) WScript.Echo AvailableLetter WshNetwork.MapNetworkDrive AvailableLetter, &quot;\\Server\Share&quot; Loop Until err.Number = 0 'This could possibly not return so you may 'want to say &quot;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) & &quot;:&quot; = oDrives.Item(i) Then x = x + 1 End If  Next GetNextletter = chr(x) & &quot;:&quot; End Function

'Method 2

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

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



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



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(&quot;WScript.Network&quot;)

Set oDrives = WshNetwork.EnumNetworkDrives

x = 70         'This sample starts at &quot;F&quot; and uses capital letters

Do DriveLetter = chr(x) & &quot;:&quot; 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 &quot;No free drive letters&quot; WScript.Quit End If           Exit For End If   Next Loop Until LetterFound = False WScript.Echo DriveLetter

WshNetwork.MapNetworkDrive DriveLetter, &quot;\\Server\Share&quot;

If err.number <> 0 Then WScript.Echo err.number & &quot;: &quot; & 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.

Keywords: kbprb kbdswmanage2003swept KB303209

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.