Mapping Windows Network Drive using Visual Basic Script
VBScript can be used to connect network drives to Windows shares on servers or NAS devices. In situations where batch processing is not the right choice, or group policy is not the application you want, a Visual Basic Script can meet the requirement, for example, to apply login scripts to VPN Remote Clients who are not members of the AD domain.
Microsoft VBScript contains object classes, methods (functions / procedures) and value structures. Our
MapNetworkDrive object is used as a method here. The method or verb in turn manipulates values.
Any object can be used, the object
objNetwork is defined here, script developers like to stick to patterns and their variables. A prefix of
str indicates a string value, while the
obj prefix indicates an object. After WScript has created our objNetwork, it can be edited using the MapNetworkDrive method.
VBScript for Network drive Mapping
The following VBScript
netdrive.vbs as an example, assign the network mapping to drive Z: for a specific user; the login can be performed as a different user than the one with whom you have authenticated yourself on the client.
Option Explicit Dim objNetwork, strDriveLetter, strRemotePath, strUser, strPassword, strProfile, WshShell ' Set credentials & network share to variables. strDriveLetter = "Z:" strRemotePath = "\\server\share" strUser = "domain\username" strPassword = "topsecret" strProfile = "false" ' Create a network object (objNetwork) do apply MapNetworkDrive Z: Set objNetwork = WScript.CreateObject("WScript.Network") objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _ strProfile, strUser, strPassword ' Open message box, enable remove the apostrophe at the beginning. ' WScript.Echo "Map Network Drive " & strDriveLetter MsgBox " Explorer launch Network Drive " & strDriveLetter, vbInformation, "Network Drive Mapping" ' Explorer will open the mapped network drive. Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "explorer.exe /e," & strDriveLetter, 1, false WScript.Quit
The corresponding variable assigned line 5 – 9 in which the placeholder is defined between quotation marks.
Assign VBScript values to variables
strDriveLetterchoose a desired network drive letter.
strRemotePathadd the UNC path to the network share.
strUseradd the user name. If this is a member of an AD domain, the domain prefix must be given, using like
strPasswordadd the users password.
Run VBScript WSH from batch
If a batch is already used, for example
netlogon.bat, then from the batch file the VBScript
netdrive.vbs is executed as follows:
start /wait "" cscript //Nologo netdrive.vbs
If you do not want to output Visual Basic Console Screen during execution, use option
/B to start the application without opening a new window.
start /B /wait "" cscript //Nologo netdrive.vbs
After successfully logging in, the network drive for the Windows network share is created and then opened in Explorer.