As a network administrator, we often use vbscripts to map network shares. Our end users are constantly wanting new shares with specific drive letters in many different locations. So as administrators, it is our job to script a drive mapping solution for those users.
In this article, I'm going to talk about drive mapping for end users. First, we have to specify a drive letter (i.e. T: ) when mapping. This is typically supplied by the user(s) requesting the drive mapping. Next we need the server and share (i.e. \\myserver\sharename). Finally, we write the code to map the share to that drive letter and pop it into their script. We also use other logic to determine who maps what drive, but we'll save those techniques for another article.
First, let's look at the basics of drive mapping. In order to map drives, we use the Wscript.Network object. From here, we can access all kinds of things like enumerating all the current network drive mappings, disconnecting drives, and mapping drives. There are several other methods available using the link above.
Set WshNet = WScript.CreateObject("WScript.Network")
Now that we have our object, we can disconnect drives using the RemoveNetworkDrive method and we can map drives using the MapNetworkDrive method:
WshNet.MapNetworkDrive "X:", "\\myserver\sharename" 'Connect drive
WshNet.RemoveNetworkDrive "X:", "\\myserver\sharename" 'Disconnect drive
Easy huh? But what if a user already has a different share mapped to drive X:?
As administrators, we have the power to force users to map certain shares to certain drive mappings. Often times, we enforce this by disconnecting anything on the requested drive mapping before we map it. This ensures we have the correct mapping on the drive letter. This seems like overkill in my opinion. Why waste time disconnecting and reconnecting a drive if we don't need to?
To get around this, we can use the EnumNetworkDrives method. This returns an array of drive letters and server shares. We can then iterate through the list and compare to see if they exist. Let's add this capability and create a nice function we can reuse:
Function MapDrive(strDrive,strPath)
'Input: strDrive = Drive letter - ex. "x:"
'Input: strPath = Path to server/share - ex. "\\server\share"
'Output: bln = True or False
Err.clear
MapDrive = False
Set WshNet = WScript.CreateObject("WScript.Network")
'Step 1: Get the current drives
Set oDrives = WshNet.EnumNetworkDrives
If Err.Number <> 0 Then
'Code here for error logging
Err.Clear
MapDrive = False
Exit Function
End If
'Step 2: Compare drive letters to the one requested
blnFound = False
For i = 0 To oDrives.Count - 1 Step 2
If UCase(strDrive) = UCase(oDrives.Item(i)) Then
WScript.Echo "Drive letter " & strDrive & " mapped, checking connection"
blnFound = True
'Drive letter was found. Now see if the network share on it is the same as requested
If UCase(strPath) = UCase(oDrives.Item(i+1)) Then
'Correct mapping on the drive
MapDrive = True
Else
'Wrong mapping on drive. Disconnect and remap
WshNet.RemoveNetworkDrive strDrive, true, True 'Disconnect drive
If Err.Number <> 0 Then
'Code here for error logging
Err.clear
MapDrive = False
Exit Function
End If
WshNet.MapNetworkDrive strDrive, strPath 'Connect drive
If Err.Number <> 0 Then
'Code here for error logging
Err.clear
MapDrive = False
Exit Function
End If
MapDrive = True
End If
End If
Next'Drive in the list
'Ok. If blnFound is still false, the drive letter isn't being used. So let's map it.
If Not blnFound Then
WshNet.MapNetworkDrive strDrive, strPath
If Err.Number <> 0 Then
'Code here for error logging
Err.clear
MapDrive = False
Exit Function
End If
MapDrive = True
End If
Set WshNet = Nothing
Set oDrives = Nothing
End Function
Looks long huh? In summary, the function enumerates all the network drive mappings first. Then it looks through the list for the requested drive letter to see if it's in use. If so, it checks the share mapping on that drive letter. If it's not correct, it disconnects and connects it proper. If the drive letter is not in use, it then maps the share to the requested drive letter.
To use:
MapDrive("X:",\\myserver\sharename)
And because it is a function that returns True or False, we can use the results as well:
If MapDrive("X:","\\myserver\sharename") Then
Wscript.Echo "Drive mapped successfully"
Else
Wscript.Echo "Drive not mapped successfully"
End If
There you have it. A better drive mapping solution. It's not perfect by no means, but it's better than just disconnecting wildly. I plan on adding more logic to search for shares mapped on other letters to ensure we don't get double mappings and to migrate people to the correct ones in times were we need to change the drive letter (and they will, end users are fickle).
Till next time!
-Corey
Have a burning VBScript question to be answered? Send it in and you may just be featured in our next article... and get your question answered. :)