FreeNAS: Automate Drive Mappings for Windows Users

This is the third in a series about FreeNAS, the free network attached storage application which allows you to create an inexpensive but highly capable network file server for backups, iTunes, and general file sharing. Our application is a server for student data. We want to give each student a secure folder in which to store files that they create and use when working in our student computer labs.  The two previous postings are:

Creating a FreeNAS server for student data

Adding students and creating folders 

Note that the first link picks up at the point that the FreeNAS server software has been installed on to server hardware with a minimal configuration. The FreeNAS web site has links to several tutorials as well as the official setup guide.

By the way, FreeNAS installs really nicely within a virtual machine so you can easily test it out. I’ve got it running in Parallels on my MacBook, with software RAID 5 providing redundant disk storage.

Mapping a drive to a student folder

Once I set up the student’s folder and account on the FreeNAS server, I wanted to be able to give them the opportunity to access it from any workstation in our student lab.  The cleanest way I could think of was to create an icon on the desktop which runs a script. The script does the following:
1. Asks for the student login name
2. Asks for the student’s password
3. Maps the H: drive to the student’s folder on the FreeNAS server.

Student folders are named exactly the same as the student login, and they all appear under a shared folder called “StudentData”.  The full path is /mnt/StudentData/.  So, when student Myron Kapoodle logs in with his user name mkapoodle, the script takes him to: 

/mnt/StudentData/mkapoodle

Thus, when the student accesses drive H:, they find themselves in their own folder. They can’t select a folder “above” their own, and they can’t access anyone else’s folder, even if they can see it when browsing around the network neighborhood.

The Script

' VBScript to map a network drive.
' Heavily borrowed from ....
' Guy Thomas http://computerperformance.co.uk/
' Larry Keyes www.techfornonprofits.com
' ------------------------------------------------------'
Option Explicit
Dim strDriveLetter, strRemotePath, strUser, strPassword
Dim objNetwork, objShell, objFSO
Dim CheckDrive, AlreadyConnected, intDrive
strUser=""
strPassword=""

' This section gets the name and password
strUser=InputBox("Enter your User Name")
strPassword=InputBox("Enter your Password")

' The section sets the variables.
strDriveLetter = "H:"
strRemotePath = "\\freenas\StudentData\" & strUser

' This sections creates two objects:
' objShell and objNetwork and counts the drives
Set objShell = WScript.CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set CheckDrive = objNetwork.EnumNetworkDrives()

If objFSO.DriveExists(strDriveLetter) Then
objShell.Popup "The H: Drive is already mapped"
objNetwork.RemoveNetworkDrive strDriveLetter
strRemotePath = "\\freenas\StudentData\" & strUser
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath , false, strUser, strPassword
Else
strRemotePath = "\\freenas\StudentData\" & strUser
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath , false, strUser, strPassword
End if

'Section which actually (re)names the Mapped Drive to eliminate naming problem.
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter & "\").Self.Name = strUser
Wscript.Echo "Check : "& strDriveLetter & " for " & strUser
WScript.Quit

There is some extra stuff in there that attempts to fix an issue that appeared in Windows 7, where if the drive mapping is reused, it shows up with the name of the previous user.

Our student workstations have a single “student” local account.  Every student logs in to that account when they use the workstation. There are no individual user profiles. In some cases I have the student account log in automatically, and I’ll probably do this on all machines that use the FreeNAS network so that a student doesn’t have to log in twice…once to the desktop and once with their own user name and password on the FreeNAS server.

This script should be installed on each Windows workstation, with a desktop icon to appear on the desktop of the student account.

Two other observations and questions:

1. Obviously you can simply map a drive from the command line using Start->Run->CMD, and then at the prompt  type MAP H: /freeNAS/StudentData/mkapoodle.

2. I searched all over for a more elegant way to have a screen that came up that would ask for the name and password and then make the call to create the drive mapping. First I looked at C#, then, because Visual Basic has a “shell” command, I switched to VB. However that required a full-blown Windows installation of the .exe file, as well as a batch file which was called by the VB program. I finally decided I could live with two windows popping up; one asking for the name and another for the password.

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 )

Facebook photo

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

Connecting to %s