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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mapped drives using VBScript - What's wrong with this code snippet? 3

Status
Not open for further replies.

Callahan

Technical User
Nov 21, 2001
174
GB
Hi all,

Now first off I know as much about VB Scripting as I've learnt this morning. After the sucess I've had so far (up until this point) I'm really going to learn more about it.

Ok, so here's what I want to do. Upon user logon, the .vbs script will unmap all network drives on the client machine, remap them (this is to automate unmapping and mapping of new network shares in the future), then renames each of the drives.
Everything works up until the point of renaming the network drives. If I run the piece of code that renames the drives outside of this code it works so I'm guessing the mistake can only be minor.

Any suggestions grately received.

Option Explicit
Dim strDriveLetter1, strDriveLetter2, RemotePath1, RemotePath2
Dim objNetwork
Dim bForce, bUpdateProfile
Dim mDrive
bForce = "True"
bUpdateProfile = "True"

Set objNetwork = CreateObject("WScript.Network")
strDriveLetter1 = "F:"
strDriveLetter2 = "Y:"
RemotePath1 = "\\fpsl01\home"
RemotePath2 = "\\fpsl01\jbloggs"

' Removes all listed DriveLetters, with bForce, pUpdate Profile
On Error Resume Next
objNetwork.RemoveNetworkDrive strDriveLetter1, bforce, bUpdateProfile
objNetwork.RemoveNetworkDrive strDriveLetter2, bforce, bUpdateProfile

' Maps the listed drives
objNetwork.MapNetworkDrive strDriveLetter1, RemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, RemotePath2

' Section which actually (re)names the Mapped Drives
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter1).Self.Name = "test1"
objShell.NameSpace(strDriveLetter2).Self.Name = "test2"
Wscript.Echo "Success - All done"
Wscript.Quit
 
Set N = CreateObject("WScript.Network")
Set E = N.EnumNetworkDrives
For i = 0 to E.Count - 1 Step 2
Select Case Trim(E.Item(i) & "")
Case "H:"
' Do nothing
Case ""
N.RemoveNetworkDrive E.Item(i+1), True, True
Case Else
N.RemoveNetworkDrive E.Item(i), True, True
End Select
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV,

So for future reference for anyone else trying to implement the same thing here's the full finished code below (this script will disconnect all mapped drives upon user logon to ensure users aren't mapped to something they shouldn't be excluding the H: drive which is being mapped through their AD account profile to their specific username. It will then map all of the listed drives to the listed locations using the specified drive letters and rename each of them to the names you have entered. This includes the H: drive in case the user has renamed it themselves to something different.

The only drawback is that the H: drive does not rename itself on the first run. In my tests you have to run the script twice. I'm not sure why yet but I'm sure it's a minor issue though which has a 'simple' solution.

Hope this post helps someone in the future. Thanks to all that contributed.

Code:
' This script will remove all the mapped drives and then remap
' the ones listed then finally rename each of the mapped drives.
' Do not remove the following line:
' objShell.NameSpace(HomeDrive).Self.Name = "My Home Drive". This
' line will ensure that the name of the user's home drive
' (mapped by AD) remains the same on all user's machines.

Option Explicit
Dim HomeDrive, DriveLetter1, DriveLetter2, DriveLetter1_Path, DriveLetter2_Path
Dim objNetwork, objShell
Dim bForce, bUpdateProfile
Dim N, E, i
bForce = True
bUpdateProfile = True

Set objNetwork = CreateObject("WScript.Network")

' List of drive variables
HomeDrive = "H:"
DriveLetter1 = "X:"
DriveLetter2 = "Y:"

' List of drive paths
DriveLetter1_Path = "\\server1\home"
DriveLetter2_Path = "\\server2\templates"

' Removes all listed DriveLetters, with bForce and pUpdateProfile
' to remove details from the user's profile too.
Set N = CreateObject("WScript.Network")
Set E = N.EnumNetworkDrives
For i = 0 to E.Count - 1 Step 2
  Select Case Trim(E.Item(i) & "")
  Case "H:"
    ' Do nothing
  Case ""
    N.RemoveNetworkDrive E.Item(i+1), True, True
  Case Else
    N.RemoveNetworkDrive E.Item(i), True, True
  End Select
Next

' Maps the listed drives
objNetwork.MapNetworkDrive DriveLetter1, DriveLetter1_Path
objNetwork.MapNetworkDrive DriveLetter2, DriveLetter2_Path

' Section which actually (re)names the mapped drives.
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(HomeDrive).Self.Name = "My Home Drive"
objShell.NameSpace(DriveLetter1).Self.Name = "testabc"
objShell.NameSpace(DriveLetter2).Self.Name = "test123"
Wscript.Quit
 
'the below function is in a class
'blnEnumDrives and enumDrives have global scope

Public Function MapDrive(strDrvShare, blnPersistent, blnDisconnect)
On Error Resume Next
Dim blnDriveConnected
Dim blnAlreadyMapped
Dim strDrv
Dim strDrvLong
Dim strShare


blnAlreadyMapped = False
strDrv = Left(strDrvShare, 1)
strShare = Right(strDrvShare, Len(strDrvShare) - 2)
strDrvLong = strDrv & ":"
blnDriveConnected = FSO.DriveExists(strDrvLong)

If blnEnumDrives = False Then
Set enumDrives = WshNetwork.EnumNetworkDrives
blnEnumDrives = True
End If

If IsObject(enumDrives) Then
For i = 0 to enumDrives.Count -1
If enumDrives.Item(i) <> "" Then
If LCase(enumDrives.Item(i)) = LCase(strDrvLong) Then
'msgbox "found our drive already in use " & strDrvLong
If LCase(enumDrives.Item(i + 1)) = LCase(strShare) Then
'msgbox "found the share as well" & strShare
blnAlreadyMapped = True
End If
End If
End If
Next
End If

If blnAlreadyMapped = False Then
If FSO.FolderExists(strShare) Then
'msgbox "found that share " & strShare
Err.Clear
If blnDriveConnected = False Then
If blnDisconnect = "True" Then
'shouldnt need to disconnect here but just incase there is a persistant connection
'WshNetwork.RemoveNetworkDrive strDrvLong, True, True
WshNetwork.MapNetWorkDrive strDrvLong, strShare, blnPersistent
If Err.Number = 0 Then
MapDrive = 0
blnEnumDrives = False
Else
'Msgbox Err.Number
'shouldnt need to disconnect here but just incase there is a persistant, remembered connection
WshNetwork.RemoveNetworkDrive strDrvLong, True, True
WshNetwork.MapNetWorkDrive strDrvLong, strShare, blnPersistent
'msgbox "failed to map drive " & strDrvLong & strShare & " blPers " & blnPersistent
If Err.Number = 0 Then
MapDrive = 0
blnEnumDrives = False
Else
MapDrive = 4
End If
End If

Else
WshNetwork.MapNetWorkDrive strDrvLong, strShare, blnPersistent
If Err.Number = 0 Then
MapDrive = 0
blnEnumDrives = False
Else
'msgbox "failed to map drive " & strDrvLong & strShare & " blPers " & blnPersistent
MapDrive = 4
End If
End If
ElseIf blnDriveConnected = True And blnDisconnect = "False" Then
'msgbox "cannot map drive " & strDrvLong & " already in use and disconnect set to false."
MapDrive = 3
ElseIf blnDriveConnected = True And blnDisconnect = "True" Then
WshNetwork.RemoveNetworkDrive strDrvLong, True, True
WshNetwork.MapNetWorkDrive strDrvLong, strShare, blnPersistent
blnEnumDrives = False
If Err.Number = 0 Then
MapDrive = 0
Else
'msgbox "failed to map drive " & strDrvLong & strShare & " blPers " & blnPersistent
MapDrive = 2
End If
End If
Else
'msgbox "didnt find that share " & strShare & " " & strDrvLong
MapDrive = 1
End If
Else
MapDrive = 8 'already got it connected, might want to return 0
End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top