Solved

Disconnect already existing Mapped Drives using VB Script

Posted on 2006-10-31
12
3,531 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
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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is about my first experience with programming Arduino.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

864 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

25 Experts available now in Live!

Get 1:1 Help Now