Solved

Disconnect already existing Mapped Drives using VB Script

Posted on 2006-10-31
12
3,555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
12 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 17846019

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
ID: 17847349
Not to mention shelling out to a series of NET USEs
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 17847351
You also need Set objNetwork =nothing  to free your resources too.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

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

Expert Comment

by:CSecurity
ID: 17847562
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
 

Author Comment

by:4isteam
ID: 17851256
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
ID: 17851299
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
ID: 17853794
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
ID: 17856167
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
ID: 18073734
PAQed with points refunded (250)

Computer101
EE Admin
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Progress
Starting up a Project

624 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