Solved

Disconnect already existing Mapped Drives using VB Script

Posted on 2006-10-31
12
3,547 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

751 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