2

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?

aduljr
  • 500
  • 2
  • 5
  • 16

2 Answers2

3

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
GregD
  • 8,713
  • 1
  • 24
  • 36
1

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
Jay Riggs
  • 243
  • 4
  • 14
  • nice function, perhaps as I grow this script I put your method to the test. Thanks for the help – aduljr Sep 01 '09 at 21:19