Windows 2003 - How to map a drive to a network share persistently

2.5k Views Asked by At

I have a VB script which access a network share via drive-mapping (script below), and then copy files across for processing. As you see, the script checks if the drive already being mapped before it attempts to map it again. I scheduled the script in the Windows Scheduled Tasks and make it run every minute. The scheduled task is configured to be running as a domain user.

My problem is the drive being mapped does not persist: each time the script runs, it has to authenticate and map the drive again. It seems the drive mapping is lost once the script finishes.

I found that if I login to the server using the domain user account same as the scheduled task, map the drive manually (use 'NET USE') and keep the login session running, then the script would skip mapping the drive as expected.

I don't like the idea keeping a live login session just to keep the drive mapped. Is there any way to keep a network share persistently as a local drive, regardless login user's session.

The reason I don't want to map drive each time is because I want to avoid frequent Windows authentication (every minute). We found Windows authentication LSASS.exe leaks a small amount of memory each time we run the "MapNetworkDrive" vbs statement. Of course this memory leak issue can become a different thread discussion. For the moment, if I can have a persistently mapped drive (to a network share), I'd be very happy.

[drive mapping vb script, fyi]

Function MapNetDrive (sDriveLetter, sUNC)
Dim TDriveExists: TDriveExists = False
Dim WshNetwork: Set WshNetwork = WScript.CreateObject("WScript.Network")
Dim oDrives: Set oDrives = WshNetwork.EnumNetworkDrives
'loop through all the currently mapped drives to see if T: exists
Dim i
For i = 0 to oDrives.Count - 1 Step 2
    If oDrives.Item(i) = sDriveLetter Then 
        TDriveExists = True 
    End If
Next
'If not, map the T: drive
If TDriveExists <> True Then 
    WshNetwork.MapNetworkDrive sDriveLetter, sUNC, True, 'userName', 'password'
End If

'clean up
Set oDrives =  Nothing
Set WshNetwork = Nothing

MapNetDrive = True
End Function
1

There are 1 best solutions below

3
On
Sub MapNetDrive(sDriveLetter, sUNC)
  Set oShell = CreateObject("WScript.Shell")
  oShell.Run "%comspec% /c NET USE /PERSISTENT:YES " & sDriveLetter & _
              ": """ & sUNC & """", 0, True
  Set oShell = Nothing
End Sub