One of these wonderful days, I came across a need to map a network drive before I run a script. It’s a hassle to manually create the network drive mapping every time. This is because I was using different credentials to map the network drive.
Then I put my mask, and yelled “This is the job for…scripting man!!!”.
Here is my VBScript to map the network drive with different credentials without user interaction. Hope it is useful for somebody else. Make sure you type the correct server URL, drive letter and credentials in this script before you run.
‘*******************************************************************************
‘ Script Name: MapNetwork.vbs
‘ Purpose: Map network drive with different credentials
‘ Usage: Change the values for SDrive, sNetworkLocation, sUser, sPassword
‘ variables and run the script. Script returns nothing, but it will
‘ map the network drive to the specified drive letter.
‘
‘ Author: Anand Venkatachalapathy
‘ http://anandpv.spaces.live.com
‘********************************************************************************
Dim oNetwork
Dim sDrive, sNetworkLocation, sUser, sPassword
sDrive = "W:"
sNetworkLocation = "\MyServerData"
sUser = "MyDomainMyUserName"
sPassword = "MyPassword"
Set oNetwork = CreateObject("wscript.network")
oNetwork.MapNetworkDrive sDrive,sNetworkLocation,False,sUser,sPassword
Set oNetwork = Nothing
‘******************* End of Script *************
Cool!
This can also be accomplished via a bat file by using the following syntax:if not exist Y: net use Y: "\\MyServer\Data" /user:MyDomain\MyUserName MyPassword