Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript help - shared drive mapping

Status
Not open for further replies.

BbkKnight

Technical User
Aug 29, 2013
3
GB
Hi,

Thanks in advance for any response and assistance with this,
I am very new to scripting and have a vbscript (below) to map shared drives, it works but some users are reporting error "the local device name has a remembered connection to another network resource"

I understand it has something to do with including a RemoveNetworkDrive command and deleting registry entries for the network drives from under HKCU\Network\driveletter.

How can I incorporate remove network drives and delete the registry keys before executing the rest of the script?

' ------ SCRIPT CONFIGURATION ------
strDrive = "S:"
strPath = "\\share\drive"
boolPersistent = False ' True = Persistent ; False = Not Persistent
strDrive2 = "G:"
strPath2 = "\\share\drive"
boolPersistent2 = False ' True = Persistent ; False = Not Persistent
strDrive3 = "N:"
strPath3 = "\\share\drive"
boolPersistent3 = False ' True = Persistent ; False = Not Persistent
strDrive4 = "O:"
strPath4 = "\\share\drive"
boolPersistent4 = False ' True = Persistent ; False = Not Persistent
strDrive5 = "P:"
strPath5 = "\\share\drive"
boolPersistent5 = False ' True = Persistent ; False = Not Persistent
' ------ END CONFIGURATION ---------
objNetwork.MapNetworkDrive strDrive, strPath, boolPersistent
objNetwork.MapNetworkDrive strDrive2, strPath2, boolPersistent2
objNetwork.MapNetworkDrive strDrive3, strPath3, boolPersistent3
objNetwork.MapNetworkDrive strDrive4, strPath4, boolPersistent4
objNetwork.MapNetworkDrive strDrive5, strPath5, boolPersistent5
 
Something like this :
Code:
...
' ------ END CONFIGURATION ---------
objNetwork.RemoveNetworkDrive strDrive, True, True
objNetwork.MapNetworkDrive strDrive, strPath, boolPersistent
objNetwork.RemoveNetworkDrive strDrive2, True, True
objNetwork.MapNetworkDrive strDrive2, strPath2, boolPersistent2
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Here's some code to look at. We had a situation where we wanted drives to be persistent as long as they met certain criteria but as soon as the criteria changed, we needed to change the mapping to a different persistent path. We first used some code to fetch the persistent network drives that were assigned to that individual and then we defined what the path should be and then compared what was already mapped to what it was supposed to be. If there was a difference we then unmapped the drive and remapped it, otherwise we left the persistent mapping alone as it was already correctly mapped. Here's some sample code (untested) that I threw together for you.

Set NetworkDrives = WshNetwork.EnumNetworkDrives
For DrvIDX = 0 To NetworkDrives.Count - 1
If InStr(NetworkDrives.Item(DrvIdx),":") Then
SharedDrive = NetworkDrives.Item(DrvIdx)
Select Case SharedDrive
Case "G:"
GDrive = SharedDrive & " = " & NetworkDrives.Item(DrvIDx + 1)
Case "N:"
NDrive = SharedDrive & " = " & NetworkDrives.Item(DrvIDx + 1)
Case "O:"
ODrive = SharedDrive & " = " & NetworkDrives.Item(DrvIDX + 1)
Case "P:"
PDrive = SharedDrive & " = " & NetworkDrives.Item(DrvIDX + 1)
Case "S:"
SDrive = SharedDrive & " = " & NetworkDrives.Item(DrvIDX + 1)
Case Else
SharedDrive = ""
End Select
Else
SharedDrive = ""
End If
Next
GDrivePath = "share\path"
NDrivePath = "share\path"
ODrivePath = "share\path"
PDrivePath = "share\path"
SDrivePath = "share\path"
If Ucase(GDrivePath) <> UCase(Mid(GDrive,6)) Then
If InStr(GDrive,"\\") Then
WshNetwork.RemoveNetworkDrive "G:",bForce,bUpdateProfile
End If
WshNetwork.MapNetworkDrive "G:",GDrivePath,bUpdateProfile
End If
If Ucase(NDrivePath) <> UCase(Mid(NDrive,6)) Then
If InStr(NDrive,"\\") Then
WshNetwork.RemoveNetworkDrive "N:",bForce,bUpdateProfile
End If
WshNetwork.MapNetworkDrive "N:",NDrivePath,bUpdateProfile
End If
If Ucase(ODrivePath) <> UCase(Mid(ODrive,6)) Then
If InStr(ODrive,"\\") Then
WshNetwork.RemoveNetworkDrive "O:",bForce,bUpdateProfile
End If
WshNetwork.MapNetworkDrive "OG:",ODrivePath,bUpdateProfile
End If
If Ucase(PDrivePath) <> UCase(Mid(PDrive,6)) Then
If InStr(PDrive,"\\") Then
WshNetwork.RemoveNetworkDrive "P:",bForce,bUpdateProfile
End If
WshNetwork.MapNetworkDrive "P:",PDrivePath,bUpdateProfile
End If
If Ucase(SDrivePath) <> UCase(Mid(SDrive,6)) Then
If InStr(SDrive,"\\") Then
WshNetwork.RemoveNetworkDrive "S:",bForce,bUpdateProfile
End If
WshNetwork.MapNetworkDrive "S:",SDrivePath,bUpdateProfile
End If
 
Many thanks both, sorry was on holiday.
I will try these

much appreciated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top