runright
asked on
Removing a mapped network drive
I think I am loosing it...I going to narrow it down to a specific 3 line piece of code, using a vbscript I want to remove a previously mapped drive.
Dim WshNetwork
Set WshNetwork = Wscript.CreateObject("WScr ipt.Networ k")
WShNetwork.RemoveNetworkDr ive "M:"
After running this code and I look at "My Computer" the drive still shows with a red X. Double click on the drive opens it and removes the red X. Attempting to map a new location to the "M:" drive produces an error saying already mapped...am I missing something sim;e?
Dim WshNetwork
Set WshNetwork = Wscript.CreateObject("WScr
WShNetwork.RemoveNetworkDr
After running this code and I look at "My Computer" the drive still shows with a red X. Double click on the drive opens it and removes the red X. Attempting to map a new location to the "M:" drive produces an error saying already mapped...am I missing something sim;e?
or try this
Option Explicit
Dim objShell, objNetwork, DriveLetter1
DriveLetter1 = "M:"
Set objShell = CreateObject("WScript.Shel l")
Set objNetwork = CreateObject("WScript.Netw ork")
objNetwork.RemoveNetworkDr ive DriveLetter1
objShell.PopUp "Drive " & DriveLetter1 & " disconnected."
Wscript.Quit
Option Explicit
Dim objShell, objNetwork, DriveLetter1
DriveLetter1 = "M:"
Set objShell = CreateObject("WScript.Shel
Set objNetwork = CreateObject("WScript.Netw
objNetwork.RemoveNetworkDr
objShell.PopUp "Drive " & DriveLetter1 & " disconnected."
Wscript.Quit
ASKER
Cannot, do not, want to use a batch file. The logon script (where this code is used) is larger than just mapping drives, it also controls the printers and the default printers. The code you recommended is almost identical to mine with the exception of assigning the drive letter to a variable and the message at the end.. Still, I did copy your suggestion verbatim into a new test script and ran it. It came back with "M: disconnected" and showed in My Computer with a red X. Double clicking on the M Drive still opened the M drive and showed me the files. I ran the script again, it came back again with "M: Disconnected". I then ran the script again, it came back with "The network connection does not exist", although double clicking the M drive in My Computer still opens it.
ASKER
Found a solution that worked....changed the below line to read
WShNetwork.RemoveNetworkDr ive "M:", true, true
This does a more through removal of the drive mapping.
WShNetwork.RemoveNetworkDr
This does a more through removal of the drive mapping.
This will remove all mapped drives...
'****Remove all mapped drives****
For intDrive = 0 To CheckDrive.Count - 1 Step 2
wshNetwork.RemoveNetworkDr ive CheckDrive.Item(intDrive), bforce
Next
'****Remove all mapped drives****
For intDrive = 0 To CheckDrive.Count - 1 Step 2
wshNetwork.RemoveNetworkDr
Next
should be >>>>
Set objNetwork = CreateObject("Wscript.Netw ork")
Set colDrives = objNetwork.EnumNetworkDriv es
For i = 0 to colDrives.Count-1 Step 2
objNetwork.RemoveNetworkDr ive colDrives.Item(i)
Next
Set objNetwork = CreateObject("Wscript.Netw
Set colDrives = objNetwork.EnumNetworkDriv
For i = 0 to colDrives.Count-1 Step 2
objNetwork.RemoveNetworkDr
Next
ASKER
I have been using VB Scripting for a long time now to map the drives and printers for new users and to remove and change printers as printers change for existing users. We just finished setting up a DFS and needed to change the mapping of the drives. This must be the first time that I have had to change/delete the mapping of drives and
WShNetwork.RemoveNetworkDr ive "M:"
DOES NOT WORK, it only disconnects the drive, but leaves it mapped
WShNetwork.RemoveNetworkDr ive "M:", true, true
DOES WORK
By the way, I do use a similar type looping script to first verify if a drive needs to be mapped before deleting it and re-adding it. I found users got confused if they didnt wait for the logon script to finish running before they started opening up drives and found them not available.
WShNetwork.RemoveNetworkDr
DOES NOT WORK, it only disconnects the drive, but leaves it mapped
WShNetwork.RemoveNetworkDr
DOES WORK
By the way, I do use a similar type looping script to first verify if a drive needs to be mapped before deleting it and re-adding it. I found users got confused if they didnt wait for the logon script to finish running before they started opening up drives and found them not available.
DriveM = 0
DriveS = 0
if AllDrives.Count > 0 then
a = 0
do
If AllDrives.Item(a) = "M:" then
a = a + 1
If ucase(AllDrives.Item(a)) = "\\Domain\PUBLIC" then
DriveM = 1
end if
elseIf AllDrives.Item(a) = "S:" then
a = a + 1
If UCase(AllDrives.Item(a)) = "\\Server\CLIENTAPPS" then
DriveS = 1
end if
else
a = a + 1
end if
loop until a >= AllDrives.Count
End if
If DriveM = 0 then
On Error Resume Next
WShNetwork.RemoveNetworkDrive "M:", true, true
WshNetwork.MapNetworkDrive "M:", "\\Domain\Public", True
End if
If DriveS = 0 then
On Error Resume Next
WShNetwork.RemoveNetworkDrive "S:", true, true
WshNetwork.MapNetworkDrive "S:", "\\Server\ClientApps", True
End if
thanks for the post back, shouldve written the script up in something other than notepad and i would have seen the optional arguments
ASKER
I would be interested in a better program than notepad for script editing....trying to find the error on line 74 is a pain in notepad...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
net use m: /del
this is just batch