[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Modify Script to map to drives but if already mapped disconnect.

I have this script I found on the net and it maps the drives great but if a drive isn't already mapped it give the error "this network connection does not exist".  I would like the script to be able to check if the drive is mapped and if it is disconnect it and remap it using the new file path. But I also want it to be able to map the drive if the mapping does not exit. Here is my 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")

'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
Any help would be appreciated. Thanks
0
m_m_cooper
Asked:
m_m_cooper
  • 4
  • 3
1 Solution
 
Cherukuri30Commented:
Try this script

Option Explicit
Dim strDriveLetter, strRemotePath
Dim objNetwork, objShell
Dim CheckDrive, AlreadyConnected, intDrive
' The section sets the variables.
strDriveLetter = "W:"
strRemotePath = "\\<computer name>\<share name>"

' This sections creates two objects:
' objShell and objNetwork and counts the drives
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()

' This section deals with a For ... Next loop
' See how it compares the enumerated drive letters
' with strDriveLetter
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter _
Then AlreadyConnected =True
Next

' This section uses the If = then, else logic
' This tests to see if the Drive is already mapped.
' If yes then disconnects
If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath

' The first message box
objShell.PopUp "Drive " & strDriveLetter & _
"Disconnected, then connected successfully."
Else
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
objShell.PopUp "Drive " & strDriveLetter & _
" connected successfully." End if
WScript.Quit

in the above script change the strRemotePath variable value according to your setup

Reference:

http://computerperformance.co.uk/Logon/Logon_HomeDirLoop.htm#Example_1_-_MapNetworkDrive_-_5_Arguments_




0
 
m_m_cooperAuthor Commented:
How do I do this for two mapped drives?
0
 
Cherukuri30Commented:
Make sure you give drive letters in Capital Letters and also remove the popup message which might be annoying for the users




Option Explicit
Dim strDriveLetter1, strDriveLetter2, strRemotePath1, strRemotePath2
Dim objNetwork, objShell
Dim CheckDrive, AlreadyConnected, intDrive
' The section sets the variables.
strDriveLetter1 = "W:"
strDriveLetter2 = "X:"
strRemotePath1 = "\\<computer name>\<share name>"
strRemotePath2 = "\\<computer name>\<share name>"

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()


On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter1 _
Then AlreadyConnected =True
Next
If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter1
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1

objShell.PopUp "Drive " & strDriveLetter1 & _
"Disconnected, then connected successfully."
Else
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objShell.PopUp "Drive " & strDriveLetter1 & _
" connected successfully." End if

On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter2 _
Then AlreadyConnected =True
Next
If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter2
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2

objShell.PopUp "Drive " & strDriveLetter2 & _
"Disconnected, then connected successfully."
Else
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2
objShell.PopUp "Drive " & strDriveLetter2 & _
" connected successfully." End if




WScript.Quit
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
m_m_cooperAuthor Commented:
There are still two issues with this script. If a drive is already mapped to it does not map it to a different location. Also I do not need the messages. It will only confuse my users.
0
 
Cherukuri30Commented:
The below script will not display the popup messages.

Regarding mapping to another location, I did not understand your logic, Why do you want to map to another location when it is allready existing. And what do you mean my another location? you mean another drive letter or another shared resource? I guess this script is good enough for your requirements




Option Explicit
Dim strDriveLetter1, strDriveLetter2, strRemotePath1, strRemotePath2
Dim objNetwork, objShell
Dim CheckDrive, AlreadyConnected, intDrive
' The section sets the variables.
strDriveLetter1 = "W:"
strDriveLetter2 = "X:"
strRemotePath1 = "\\<computer name>\<share name>"
strRemotePath2 = "\\<computer name>\<share name>"

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()


On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter1 _
Then AlreadyConnected =True
Next
If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter1
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
Else
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
End if

On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter2 _
Then AlreadyConnected =True
Next
If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter2
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2
Else
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2
End if

WScript.Quit
0
 
m_m_cooperAuthor Commented:
The only Issue I am having with the script is if the F  or N drive is already mapped to a different location then the script does not map it to the new location. For example if the F drive is mapped to \\server1\share1 and I want it to remap to \\server2\share2 the script sees F connected and leaves it alone instead of remapping it.
0
 
Cherukuri30Commented:
This issue you have to plan properly and carefully when your implementing the logon scripts not to have any conflicts while mapping. I guess all your drives will be mapped through logon scripts, am i right? If so plan carefully and assign drive letters for each shared resource to the users.

If every mapping is done through logon script and assigned unique drive letters then you need not worry of any kind of conflict because all the mapped drives will be automatically disconnected when you logg off/shutdown the system and will be mapped back automatically when user logs on
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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