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

modify VB Script to disconnect already mapped drives then remap

I found this script on the net and it works great except for one issue. If the host already has a drive connected it gives back an error and doesn't map the drive. What I would like is the script to disconnect the F: and N: then map them to the new location. Below is the script.

Option Explicit
Dim objNetwork, strRemotePath1, strRemotePath2
Dim strDriveLetter1, strDriveLetter2

strDriveLetter1 = "F:"
strDriveLetter2 = "N:"
strRemotePath1 = "\\vcna-server-06\root"
strRemotePath2 = "\\vcna-server-04\projects"

Set objNetwork = CreateObject("WScript.Network")

' Section which maps two drives, F: and N:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2

Wscript.Quit

Any help would be great. Thanks.
0
m_m_cooper
Asked:
m_m_cooper
  • 3
1 Solution
 
MazaraatCommented:
Here ya go =)


Option Explicit
Dim objNetwork, strRemotePath1, strRemotePath2
Dim strDriveLetter1, strDriveLetter2

strDriveLetter1 = "F:"
strDriveLetter2 = "N:"
strRemotePath1 = "\\vcna-server-06\root"
strRemotePath2 = "\\vcna-server-04\projects"

Set objNetwork = CreateObject("WScript.Network")

'Remove original Drive
objNetwork.RemoveNetworkDrive "F:"
objNetwork.RemoveNetworkDrive "N:"

' Section which maps two drives, F: and N:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2

Wscript.Quit
0
 
MazaraatCommented:
RemoveNetworkDrive Method
Description
The RemoveNetworkDrive method is used to disconnect a network drive mapping from the local system.

object.RemoveNetworkDrive Device [, Force] [, UpdateProfile]


example:
'VBScript Example
Set WshNetwork = CreateObject("WScript.Network")

WshNetwork.RemoveNetworkDrive "S:", True, True
or
WshNetwork.RemoveNetworkDrive "\\server\share", True, True
0
 
MazaraatCommented:
Thanks, glad to help =)
0
 
m_m_cooperAuthor Commented:
Thanks for the help.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now