How to automate mapping network drives via script – VBS Logon Script – MapNetworkDrive

How to automate mapping network drives via script

My coworkers were constantly managing their clients mapped drives so I wrote a script to automate setting them up.  I wrote it both in VB6 and VBScript for those of you interested in seeing it written both late and early bound.  Here it is:

==== VBScript ====
(to use this put it in a file with a .vbs extension and then double click on it)

Main

Sub Main()
    Dim sNetworkString
    sNetworkString = "I:,\\somenetworklocation\somefolder;
J:,\\someothernetworklocation\asubfolder;M:,\\onemorenetworklocation\something"
    Dim sNetworkArray
    sNetworkArray = Split(sNetworkString, ";")
    Dim sNetworkLine

    Dim objNetwork
    Set objNetwork = CreateObject("WScript.Network") 

    'To remove a network drive: call objNetwork.RemoveNetworkDrive("I:",true,true)
    Dim item
    For Each item In sNetworkArray
        sNetworkLine = Split(item, ",")
        On Error Resume Next 'This is here to ignore the drive errors if they 
        'already exist
        objNetwork.MapNetworkDrive sNetworkLine(0), sNetworkLine(1)
        On Error GoTo 0
    Next
End Sub

==== VB6 ====
(to use this put it in a VB6 forms or console app, add a reference to
‘”Windows Script Host Object Model” and compile)

Private Sub Form_Load()
    Dim sNetworkString As String
    sNetworkString = "I:,\\somenetworklocation\somefolder;
J:,\\someothernetworklocation\asubfolder;M:,\\onemorenetworklocation\something"
    Dim sNetworkArray() As String
    sNetworkArray = Split(sNetworkString, ";")
    Dim sNetworkLine() As String

    Dim objNetwork As WshNetwork
    Set objNetwork = CreateObject("WScript.Network") ' WshNetwork

    'To remove a network drive: call objNetwork.RemoveNetworkDrive("I:",true,true)
    Dim item As Variant
    For Each item In sNetworkArray
        sNetworkLine = Split(item, ",")
        On Error Resume Next 'This is here to ignore the drive errors if they already exist
        objNetwork.MapNetworkDrive sNetworkLine(0), sNetworkLine(1)
        On Error GoTo 0
    Next item
End Sub

If you want to make this smarter you could play with objNetwork.EnumNetworkDrives().  By using this collection you could bypass my messy method of ignoring already mapped drives by ignoring the error they throw.

How it works:
You may of noticed above there is a variable declared like so:
sNetworkString = “I:,\\somenetworklocation\somefolder;
J:,\\someothernetworklocation\asubfolder;M:,\\onemorenetworklocation\something”
This variable is key to this function working. If you want to map more / less / different paths all you need to do is add your own to this string. It is delimited by comma and semi colon, so this maps the X drive:
sNetworkString = “X:,\\mycomputer\afolder”
and this maps X and Y:
sNetworkString = “X:,\\mycomputer\afolder;Y:,\\anothercomputer\folder1\folder2”
Note that there is no semi-colon (;) at the end of the string. This is intentionally omitted because of the way Split() works.

If you use this script in your company I would love to hear about it! Feel free to post your comments here, I always enjoy hearing about other people’s experiences.

Advertisements
  1. #1 by Mike Martinez on July 13, 2012 - 3:39 pm

    I needed to map a network drive to all the computers in different branches. I got a hold of this script and modified it to meet our need. I emailed and attached the script to the email. Told them to run it. IT WORKED!!!!!!!! It saved me hours of work.

    Thank You. Mike M.

    • #2 by Anthony on August 2, 2012 - 10:39 am

      You’re welcome, I’m glad it’s getting some use!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: