Solved

Disconnect already existing Mapped Drives using VB Script

Posted on 2006-10-31
12
3,527 Views
Last Modified: 2008-01-09
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
Comment
Question by:4isteam
  • 4
  • 2
  • 2
  • +2
12 Comments
 
LVL 22

Expert Comment

by:cookre
Comment Utility

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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Not to mention shelling out to a series of NET USEs
0
 
LVL 3

Expert Comment

by:taycuong76
Comment Utility
You also need Set objNetwork =nothing  to free your resources too.
0
 
LVL 17

Expert Comment

by:CSecurity
Comment Utility
Dim WshNetwork, ShareName
Set WshNetwork = WScript.CreateObject("WScript.Network")
ShareName = "C$"
WshNetwork.RemoveNetworkDrive ShareName, true, true
0
 
LVL 17

Expert Comment

by:CSecurity
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:4isteam
Comment Utility
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
 
LVL 17

Expert Comment

by:CSecurity
Comment Utility
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
 

Author Comment

by:4isteam
Comment Utility
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
 
LVL 17

Expert Comment

by:CSecurity
Comment Utility
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
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
Comment Utility
PAQed with points refunded (250)

Computer101
EE Admin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Using YubiKey with REST API application 2 77
parentbit challenge 3 51
strCopies  challenge 17 73
Internet Connection Util 8 68
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now