Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Do you get any errors? What happens when you run the script interactively from a user's session? –  squillman Sep 1 '09 at 19:39
    
I get no errors if I run the file manually, and the drives map. –  aduljr Sep 1 '09 at 19:55

2 Answers 2

up vote 3 down vote accepted

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
share|improve this answer
    
thanks I am trying your suggestions now. –  aduljr Sep 1 '09 at 19:56
    
It works thanks so much! –  aduljr Sep 1 '09 at 21:18

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|improve this answer
    
nice function, perhaps as I grow this script I put your method to the test. Thanks for the help –  aduljr Sep 1 '09 at 21:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.