If I map a driveletter to a network-share, the share can be enumerated
using EnumNetworkDrives of WScript.Network - and likewise un-mapped
using RemoveNetworkDrive (or re-mapped using MapNetworkDrive).

But if the network share is removed before the mapped driveletter is
un-mapped, the mapping is broken and appears in Windows Explorer as a
driveletter with a red cross - and if you try to access the driveletter
you get the errormessage "The network name cannot be found".

Now if you use EnumNetworkDrives the broken driveletter mapping doesn't
appear as a network drive - and you can't use RemoveNetworkDrive to
remove the connection, this results in the error
"WSHNetwork.RemoveNetworkDrive: This network connection does not
exist". If MapNetworkDrive is attempted on the same driveletter you get
the error "WSHNetwork.MapNetworkDrive: An attemp made to remember a
device that had previously been remembered".

So you're stuck with a broken connection that can't be viewed / removed
/ unmapped using EnumNetworkDrives or RemoveNetworkDrive - only through
Windows Explorer - Rightclick on driveletter - Disconnect can the
connection be removed (or using Net Use <driveletter>: /Delete)

So my question is: Does anyone know of any other way using VBScript,
WMI etc. to remove a broken networkshare?

Any help will be highly appreciated!

Thanks,
Nicolaj

Re: EnumNetworkDrives and RemoveNetworkDrive doesn't work on broken network connection / share - any other ways? by drlandau

drlandau
Tue Feb 21 03:37:28 CST 2006

Hmm, odd enough the EnumNetworkDrives 'sometimes' seem to work, but
this may be because the connection isn't always being reported as
broken immediately...(?) Anyway the RemoveNetworkDrive function seems
to work if you set both bForce and bUpdateProfile to TRUE - but as
EnumNetworkDrives obviously can't be trusted to show all drives if some
are broken, the following code won't work:

<pre>
' Return true if the specified driveletter is currently mapped to a
network drive or otherwise in use
Function DriveletterInUse(strDriveLetter)
Dim objWshNetwork, objDrives, intCounter, blnDriveletterInUse, objFSO
Set objWshNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDrives = objWshNetwork.EnumNetworkDrives
blnDriveletterInUse = False
For intCounter = 0 to objDrives.Count - 1 Step 2
If UCase(Left(objDrives.Item(intCounter), 1)) =
UCase(Left(strDriveLetter, 1)) Then blnDriveletterInUse = True
Next
If objFSO.DriveExists(strDriveLetter) Then blnDriveletterInUse = True
DriveletterInUse = blnDriveletterInUse
End Function
</pre>

Instead I've written a function that uses WMI to check if the
driveletter is in use, as WMI (Win32_NetworkConnection) returns the
connection information even though the connection is broken
(driveletter and networkpath is returned along with the state of the
connection, error, disconnected, degraded etc.). Use this function to
check if a driveletter is in use - and then use the RemoveDriverLetter
function with both bForce and bUpdateProfile set to TRUE, to delete the
connection. There's not much errorchecking, so if needed incorporate
this yourself:

<pre>
Function DriveletterInUse(strComputer, strDriveLetter)
' Declare variables
Dim objFSO, objWMIService, colNetworkConnections
' Define constants
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
' Create objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\CIMV2")
Set colNetworkConnections = objWMIService.ExecQuery("SELECT * FROM
Win32_NetworkConnection", "WQL", _

wbemFlagReturnImmediately + wbemFlagForwardOnly)
' Assume driveletter is not in use by default
blnDriveletterInUse = False
' Loop through all network connections
For Each objNetworkConnection In colNetworkConnections
' Check network connections
If UCase(Left(objNetworkConnection.LocalName, 1)) =
UCase(Left(strDriveLetter, 1)) Then blnDriveletterInUse = True
Next
' Use FileSystemObject to include checking of local driveletters
If objFSO.DriveExists(strDriveLetter) Then blnDriveletterInUse = True
' Return boolean value
DriveletterInUse = blnDriveletterInUse
' Cleanup
Set objFSO = Nothing
Set objWMIService = Nothing
Set colNetworkConnections = Nothing
End Function
</pre>

Sorry for any bugs!

Best regards,
Nicolaj


Re: EnumNetworkDrives and RemoveNetworkDrive doesn't work on broken network connection / share - any other ways? by drlandau

drlandau
Tue Mar 14 05:06:41 CST 2006

VERY WEIRD!!!

When running the function on a Windows 2000 Server everythings runs
fine - but when running it on a Windows Server 2003, the script takes
20 seconds to finish the "For Each objNetworkConnection In
colNetworkConnections" statement, seems that it's the WMI thats slow.

Does anyone have any idea why this is happening? It's happening on all
the WIndows Server 2003 servers I've tested on. Takes no time on a
Windows 2000 box...

---

' Return true if the specified driveletter is currently mapped to a
network drive or otherwise in use
Function DriveletterInUse(strComputer, strDriveLetter)
' Declare variables
Dim objFSO, objWMIService, colNetworkConnections
' Define constants
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
' Create objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\CIMV2")
Set colNetworkConnections = objWMIService.ExecQuery("SELECT LocalName
FROM Win32_NetworkConnection", "WQL", _

wbemFlagReturnImmediately + wbemFlagForwardOnly)
' Assume driveletter is not in use by default
blnDriveletterInUse = False
' Loop through all network connections
For Each objNetworkConnection In colNetworkConnections
' Check network connections
If UCase(Left(objNetworkConnection.LocalName, 1)) =
UCase(Left(strDriveLetter, 1)) Then blnDriveletterInUse = True
Next
' Use FileSystemObject to include checking of local driveletters
If objFSO.DriveExists(strDriveLetter) Then blnDriveletterInUse = True
' Return boolean value
DriveletterInUse = blnDriveletterInUse
' Cleanup
Set objFSO = Nothing
Set objWMIService = Nothing
Set colNetworkConnections = Nothing
End Function