Mapping a Drive with VBScript

So I recently wrote a very long and complex login script to replace the login scripts we currently have here and figured I would share certain parts of it here incase anyone else stumbles upon it and learns something.

Define these variables for both commands

Set objNetwork = CreateObject("Wscript.Network")
Set objFileSys = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")


This function was written to map and F drive to a specific server and share.
Sub MapFDrive()

'Checks to see if they have the drive mapped, if so then it deletes it.
If objFileSys.DriveExists("F:") = True Then
objNetwork.RemoveNetworkDrive "F:", True, True
End If

'Maps the Drive.
objNetwork.MapNetworkDrive "F:", "\\Server\Share"
'Gives the drive a name so it's not just <share name> on <server> (Drive:).
objShell.nameSpace("F:\").Self.name = "Sharename can be whatever you want."

End Sub


And this one, which is a little more complex, allows for dynamic mapping via the script
Sub MapDrive(strFileServerPush, strDriveLetter, strMapPath, strDriveName)

'Checks to see if they have the drive mapped, if so then it deletes it.
If objFileSys.DriveExists(strDriveLetter) = True Then
objNetwork.RemoveNetworkDrive strDriveLetter, True, True
End If

'Constructs the path to map the drive.
strPath = "\\" & strFileServerPush & "\" & strMapPath
'Maps the Drive according to the path above.
objNetwork.MapNetworkDrive strDriveLetter, strPath
'Gives the drive a name so it's not just <share name> on <server> (Drive:).
objShell.nameSpace(strDriveLetter & "\").Self.name = strDriveName

End Sub


The above function is called like so:

MapDrive “<Server Name>”, “<Drive Letter>”, “<Share Name>”, “<Whatever you want to name it>”


 
 
 

One Response to “Mapping a Drive with VBScript”

  1. TimothyReed
    17. September 2008 at 10:14

    Alternatively to vb login scripts you can use abilities of desktop management systems.

    In our company we map drives as well as map printers, deploy software and patches, configure outlook profiles and many more with scriptlogic’s solution called desktop authority for desktop management.

    Hope this was useful!

Leave a Reply