logon.vbs script is not mapping network drives

6,549

Solution 1

Try:

wshNetwork.MapNetworkDrive "S:","\\server\shared", True
wshNetwork.MapNetworkDrive "U:","\\server\" & wshNetwork.UserName, True

I also add a routine to remove all the shares just to avoid "device is already in use" errors that I was getting, prior to mapping the drives.

wshNetwork.RemoveNetworkDrive "S:", True, True
wshNetwork.RemoveNetworkDrive "U:", True, True

wscript.sleep 300

Solution 2

Here's a function I've been using:

Function MapDrive(ByVal strDrive, ByVal strShare)
    ' Function to map network share to a drive letter.
    ' If the drive letter specified is already in use, the function
    ' attempts to remove the network connection.
    ' objFSO is the File System Object, with global scope.
    ' objNetwork is the Network object, with global scope.
    ' Returns True if drive mapped, False otherwise.

    Dim objDrive
    On Error Resume Next
    If (objFSO.DriveExists(strDrive) = True) Then
        Set objDrive = objFSO.GetDrive(strDrive)
        If (Err.Number <> 0) Then
            On Error GoTo 0
            MapDrive = False
            Exit Function
        End If
        If (objDrive.DriveType = 3) Then
            objNetwork.RemoveNetworkDrive strDrive, True, True
        Else
            MapDrive = False
            Exit Function
        End If
        Set objDrive = Nothing
    End If
    objNetwork.MapNetworkDrive strDrive, strShare
    If (Err.Number = 0) Then
        MapDrive = True
    Else
        Err.Clear
        MapDrive = False
    End If
    On Error GoTo 0
End Function

Usage example:

If (MapDrive("Z:", "\\yourserver\yourshare") = False) Then
    ' Do something because there was an error.        
End If
Share:
6,549
aduljr
Author by

aduljr

Updated on September 17, 2022

Comments

  • aduljr
    aduljr over 1 year

    I am new to creating a vbs script to map network drives in windows. For some reason, the script runs, but does not map any network drives when a user logs on to the domain. Here is the script I am using. It is pretty simple and straight forward.

    Option Explicit
    Dim wshNetwork 
    
    Set wshNetwork = CreateObject("WScript.Network")
    
    wshNetwork.MapNetworkDrive "S:","\\server\shared"
    wshNetwork.MapNetworkDrive "U:","\\server\" & wshNetwork.UserName
    WScript.Quit
    

    What am I doing wrong?

    • squillman
      squillman over 14 years
      Do you get any errors? What happens when you run the script interactively from a user's session?
    • aduljr
      aduljr over 14 years
      I get no errors if I run the file manually, and the drives map.
  • aduljr
    aduljr over 14 years
    nice function, perhaps as I grow this script I put your method to the test. Thanks for the help