• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3568
  • Last Modified:

Disconnect already existing Mapped Drives using VB Script

I have a script to Disconnect Mapped drives. The object is to Disconnect all Mapped Drives and reconnect with a script. The current script that follows is disconnecting drives that were mapped with a script, but not the drives that were mapped manually.

*****Script****

 Set objNetwork = WScript.CreateObject("WScript.Network")

Set colDrives = objNetwork.EnumNetworkDrives

For i = 0 to colDrives.Count-1 Step 2

objNetwork.RemoveNetworkDrive  colDrives.Item(i)

Next
0
4isteam
Asked:
4isteam
  • 4
  • 2
  • 2
  • +2
1 Solution
 
cookreCommented:

Add the extra options that say to disconnect even in use and to remove persistent mappings:

objNetwork.RemoveNetworkDrive  colDrives.Item(i)  ,true .true


One presumes you're doing this under an ID with rights to change other user's settings.
0
 
cookreCommented:
Not to mention shelling out to a series of NET USEs
0
 
taycuong76Commented:
You also need Set objNetwork =nothing  to free your resources too.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CSecurityCommented:
Dim WshNetwork, ShareName
Set WshNetwork = WScript.CreateObject("WScript.Network")
ShareName = "C$"
WshNetwork.RemoveNetworkDrive ShareName, true, true
0
 
CSecurityCommented:
Set objNetwork = WScript.CreateObject("WScript.Network")

Set colDrives = objNetwork.EnumNetworkDrives

For i = 0 to colDrives.Count-1 Step 2

objNetwork.RemoveNetworkDrive  colDrives.Item(i), true, true

Next

Set objNetwork = Nothing
0
 
4isteamAuthor Commented:
Thank You CSecurity. I used your script and it worked for me. I hope its not too much to ask but can you look at this script for me and tell me how I can stop the Mapped Drive to open with Explorer. I would like for it to be mapped witch its working but not to open. Ive done a some research and played some with it but its not working for me. If I omit it, then it creates five drives using the Letters provided in the script. I beleive it has to do with the comment towards the end "Call Showexplorer"  I appreciate your help thank you.



 
' FirstLetter.vbs Windows Logon Script
' VBScript to map the first available drive letter
' Author Guy Thomas http://computerperformance.co.uk/ 
' Version 3.7 - September 2005
' ------------------------------------------------------'

'Dim strDriveLetter, strRemotePath
Dim objShell 'objNetwork,
Dim CheckDrive, DriveExists, intDrive
Dim strAlpha, strExtract, intAlpha, intCount

' The section sets the variables
' N.B. Change strRemotePath
strRemotePath = "\\001317bo4enmila\PS_ODorders$"
strDriveLetter = "P:"
strAlpha = "PJMRU"
intAlpha = 0
intCount = 0
err.number= vbEmpty

' This sections creates two objects:
' objShell and objNetwork and then counts the drives
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()

' This section operates the For ... Next loop
' See how it compares the enumerated drive letters
' With strDriveLetter
On Error Resume Next
DriveExists = False
' Sets the Outer loop to check for 5 letters in strAlpha
For intCount = 1 To 5
   DriveExists = False
   
   ' CheckDrive compares each Enumerated network drive
   ' with the proposed drive letter held by strDriveLetter
      For intDrive = 0 To CheckDrive.Count - 1 Step 2
        If CheckDrive.Item(intDrive) = strDriveLetter _
        Then DriveExists = True
      Next
  intAlpha = intAlpha + 1

  '  Logic section if strDriveLetter does not = DriveExist
  ' Then go ahead and map the drive

'  Wscript.Echo strDriveLetter & " exists: " & DriveExists
  If DriveExists = False Then objNetwork.MapNetworkDrive _
  strDriveLetter, strRemotePath
  call ShowExplorer ' Extra code to take you to the mapped drive
  ' Appends a colon to drive letter. 1 means number of letters
  strDriveLetter = Mid(strAlpha, intAlpha,1) & ":"
 
  ' If the DriveExists, then it is necessary to
  ' reset the variable from true --> false for next test loop
  If DriveExists = True Then DriveExists = False

Next

'WScript.Echo "Out of drive letters. Last letter " & strDriveLetter

WScript.Quit(1)

Sub ShowExplorer()
If DriveExists = False Then objShell.run _
("Explorer" & " " & strDriveLetter & "\" )
If DriveExists = False Then WScript.Quit(0)
End Sub
0
 
CSecurityCommented:
That's a bit not possible! Because windows opens it up! It is default settings of windows... When you map a network drive by windows, you will get new explorer too... It is in windows...
0
 
4isteamAuthor Commented:
Actually we were able to do it. Just comment out Call ShowExplorer and Delete the following lines. Thanks anyways.

If DriveExists = False Then objShell.run _
("Explorer" & " " & strDriveLetter & "\" )

Following is how it should look


Next

'WScript.Echo "Out of drive letters. Last letter " & strDriveLetter

WScript.Quit(1)

Sub ShowExplorer()
 
If DriveExists = False Then WScript.Quit(0)
End Sub
0
 
CSecurityCommented:
Ah yes! Sorry... I did not checked it... Really sorry! You can remove the sub and calling line:

objShell.run _
("Explorer" & " " & strDriveLetter & "\" )
Remove this and the ShowExplorer sub!

Again sorry :p
0
 
Computer101Commented:
PAQed with points refunded (250)

Computer101
EE Admin
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now