Dealing with Disconnected Drives

Sorry this is a long Q...

I'm having problems trying to deal with two legacy apps that both have to use a drive mapping to the same drive letter but on different servers (don't ask!!).  These applications have not needed to be on the same PCs until now.  To make matters worse, the drive mappings are configured locally on the machine - either statically mapped in the user's profile or by a NET USE batch file in the All Users' Startup folder.

To try and make them coexist I've written two chunks of VBScript (one for each app) to act as the user's launch points for them.  These work by disconnecting the existing drive if it's the wrong one and then creating the new mapping and launching the app.
This works a treat on a new PC but on a PC where the user profile already contained a drive mapping it runs into problems:

In this case, on logging in, the drive letter shows up in Explorer as a Disconnected Drive and crucially, is invisible to the Win32_LogicalDisk WMI class that I was using to check for an existing drive mapping -  that means the script fails.
On a single user PC I can manually disconnect the drive in their profile before setting up these scripts etc but on a multi-user PC, after removing their old local mapping batch file, I suppose I would have to use a NET USE /D in the All Users' profile startup folder instead to ensure each user would be OK before they try to run either scripts.  I really don't want to add that extra layer of complexity to what is already a messy fudge!

How can I check for and remove these static mappings as part of my VBScript?  It appears that the static mapping is visible under Win32_NetworkConnection but this does not provide a Disconnect Method.  Am I going to have to use the WSH Shell object to run a net use /d if the NetworkConnection is found?
Any better ideas?

Thanks for reading!
LVL 16
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You don't have to do either.

All you need to do is delete the mapped drive (does it matter if it's the correct one or not?  That's an added layer of complexity.) and map the correct one.

Use this sub in your script, it does just that.  Example:

MapDrive "Q:", "\\server\share"
Sub MapDrive(letter, path)
 On Error Resume Next
 objNetwork.MapNetworkDrive letter, path
 If Err.Number <> 0 Then
  objNetwork.RemoveNetworkDrive letter, True, True
  objNetwork.MapNetworkDrive letter, path 
 End If
End Sub

Open in new window

Oh, one other thing - you'll need to define the objNetwork object as follows (somewhere up above)

Set objNetwork = CreateObject("Wscript.Network")

cantorisAuthor Commented:
Thanks for that.
But unfortunately, the disconnected drive is also invisible to the WSH Network object's .EnumNetworkDrives method, so presumably .RemoveNetworkDrive will not see it either?
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Why presume?  Try it and then let us know....

That code runs in my login scripts and maps dozens of drives for hundreds of users flawlessly...
cantorisAuthor Commented:
I'll experiment with this tomorrow, thanks.
The problem is only occurring when the script is trying to map drives in the presence of a drive locally-mapped-via-explorer.  If it had all been scripted all along then I'm sure all would be fine.
Hopefully that does it for you..

If not, I'd look at the run method instead of shell..  run allows you to do hidden windows instead of popping a cmd window.

Err - Exec maybe.
cantorisAuthor Commented:
I mapped the drive statically in a profile, logged off and back on:
When I tried to use the .RemoveNetworkDrive method to remove this network connection that .EnumNetworkDrives could not see, then there was the error "The network connection does not exist".  It remained visible in Win32_NetworkConnection afterwards and in Explorer as well.
When I then did a  net use i: /d   it disappeared from Win32_NetworkConnection but was still visible in Explorer (unless I logged off and back on).  If I tried to open the still-visible connection I got a message that it "refers to a location that is unavailable".  I was though able to remap that drive letter to a different server without any problems using net use or the GUI and could then browse into it correctly in Explorer although it remained incorrectly labelled in My Computer with the old UNC path!
Any ideas of the best way to deal with this?  I may end up having to use objWshShell.Run "net use i: /d" and live with the Explorer visual anomaly (the users call an app out of it by desktop shortcut rather than browse into it) but it might confuse anyone who comes to work on it!
Ok, so the removenetworkdrive didn't work.  What happens if you just try to map a drive on top of the "invisible" one?

Also, how many workstations are we talking about?  It might behoove you to send your techs out there and unmap them from the profiles.  Or to push a script that calls net use /d to remove them, and then going forward to map them all using VBS.  If you want to manage using a unified method (all batch, all VBS, all kix, whatever), you need to prep the environment for it.  Unfortunately, it sounds like that could use a bit of help in your environment.

Here, users aren't even allowed to map drives via explorer (it's disabled by GPO).  They have access to where they need to have access to, and we'll map them using VBS login scripts for them.  Works quite well..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cantorisAuthor Commented:
If I had control over such things, it would all have been done properly through VBScript logon scripts long ago...  ;-)
It's not likely to be many PCs with this issue but the difficulty with things within specific profiles like this is that users often move from PC to PC so you don't stand much chance of getting everyone to log in in turn to ensure the drive is disconnected in every profile.
If you try and map a different UNC over the disconnected drive you get the same error as if it were still connected, namely, "The local device name has a remembered connection to another network resource".
It looks increasingly as though I'll have to code in a check of Win32_NetworkConnection's .ConnectionState property.  ie if .LocalName is "I:" and .ConnectionState is "Disconnected" then use objWshShell.Run "net use i: /d" etc.
An added complication that could be affecting things is that the PCs also have mapped drives delivered via Novell login scripts (although these are not conflicting with the drive letter these two apps need to share).
Novell?  Ewwwwwwwwwwwwwwwwwww....

Yeah, I'm fresh out of ideas.  Post up what you finally end up using though, I'd be interested to see how you solve it.

cantorisAuthor Commented:
This is part of my solution.  Thanks for your help with this weird problem.
' This is part of a much bigger drive checking sub
strWQL = "SELECT DeviceID, DriveType FROM Win32_LogicalDisk WHERE DeviceID='" & strDriveLetter & "'"
Set colDisks = objSWbemServices.ExecQuery(strWQL,, wbemFlagReturnWhenComplete)
If colDisks.Count = 0 Then
	Call subCheckConnections	Exit Sub
End If
' More logic goes in here looking at which UNC is mapped and trying to disconnect it if appropriate (and also checking for physical drives on that letter)
' Here's part of the subCheckConnections
strWQL = "SELECT ConnectionState, LocalName, RemotePath FROM Win32_NetworkConnection WHERE LocalName='" & UCase(strDriveLetter) & "'"
Set colConns = objSWbemServices.ExecQuery(strWQL,, wbemFlagReturnWhenComplete)
If colConns.Count = 0 Then Exit Sub
For Each objConn In colConns
	strState = objConn.ConnectionState
	StrPath = objConn.RemotePath
If LCase(strState) = "connected" Then
	strMsg = "An unprecedented error has occurred - a disconnected network drive is not a LogicalDisk." & vbCRLF
	Call subEpicFail(strMsg)
	' I suspect this will never happen given what the script does earlier on but I coded it in for testing on my guinea pig user's computer
End If
StrCmd = "net use " & strDriveLetter & " /d"
intError = objWshShell.Run(strCmd, Wsh_HiddenWindow, Wsh_WaitOnReturn)

Open in new window

cantorisAuthor Commented:
As you'll see in my final post, the resultant script ends up a bit complex but it does appear to work!  Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.