Map Network Drive and Printers based on Group Membership

By | February 2, 2006

One thing Microsoft leaves out of its Group Policies is the ability to configure mapped network drives and network printers. I guess Microsoft assumes that we all know how to write scripts to do these things. Here is a script that will retreive group membership from Active Directory and map your drives and printers accordingly based on the information in the csv file.

In order for this to work, you will need Windows 2000 Professional or greater and must be attached to a Windows Domain.

Copy and paste the code below into notepad and save it as Logon.vbs

 ———-COPY EVERYTHING BELOW THIS LINE———-
On Error Resume Next

Dim GroupList
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")

GetGroupInfo()

LogonPath = fso.GetParentFolderName(WScript.ScriptFullName)
'**************************************Group Mappings Based on Grouplist.csv*********************************
If fso.FileExists(logonpath&"\Grouplist.csv") Then
   Set grplist = Fso.OpenTextFile(logonpath&"\Grouplist.csv")
   ' make File into an Array
   aGroup = Split(grplist.Readall,vbcrlf)
   For I = 0 to UBound(GroupList) ' Check Every Group Membership the user is in (populated into Grouplist)
      grpname = Grouplist(i)
      For x = 0 to UBound(aGroup) ' Read the entire CSV to make sure all drives are mapped for each Group
         mapline = agroup(x)
         If InStr(LCase(mapline),LCase(grpname)) Then ' If you're in the group
            mapline = Mid(mapline,InStr(mapline,",")+1) ' Remove the GroupName from the line
            Drive = Left(mapline,InStr(mapline,",")-1) ' Extract Drive Letter
            Path = Mid(mapline,InStr(mapline,",")+1) ' Extract the path

            If (fso.DriveExists(drive) <> True) and (Drive<>"!!") Then ' If The Drive is not already mapped
               WshNetwork.MapNetworkDrive drive,path,true ' Map The Drive
               wscript.sleep 1000
            End If

        If Drive = "!!" then
               WSHNetwork.AddWindowsPrinterConnection Path
               wscript.sleep 1000
            end if

         End If
      Next
   Next
End If

Sub GetGroupInfo
Set UserObj = GetObject("WinNT://" & wshNetwork.UserDomain & "/" & WshNetwork.UserName)
Set Groups = UserObj.groups

For Each Group In Groups
GroupCount = GroupCount + 1
Next

ReDim GroupList(GroupCount -1)
i = 0
For Each Group In Groups
GroupList(i) = Group.Name
i = i + 1
Next
End Sub
———-COPY EVERYTHING ABOVE THIS LINE———-

 Then make yourself a GroupList.csv file and follow this format:

———-CSV FILE CONTENTS———-
Group Name,x:,\\Servername\share_name
Group Name,!!,\\Print_Server\Printer
———-CSV FILE CONTENTS———-

You can add as many lines as necessary to get this done but it is important that there are no spaces above the top line and no spaced below the bottom line, the script will error out.

Once you have your files created, move them to the logon servers(GC) netlogon share and make sure the grouplist.csv has read rights to everyone and the logon.vbs has execute rights to everyone.

Under your Group Policy editor, create a group policy for the logon script and edit the configuration. Navigate to User Configuration-> Windows Settings-> Scripts -> Logon
Add the script from the netlogon folder.

This script was found a while ago and the original author is unknown.

EDIT: Modified on 4/17/06, added "On Error Resume Next" on line 1 to stop error message for end user in the event of an error.
CREDIT: Chey Harden

 EDIT: Modified on 6/12/2007, added "true" to the command that maps the network drive to make the connection persistent.
Credit: LazyNetworkAdmin

This information is provided "AS IS" with no warranties expressed or implied.

One thought on “Map Network Drive and Printers based on Group Membership

  1. Pingback: Map Network Drive based on group membership – Server 2008 – LazyNetworkAdmin

Leave a Reply

Your email address will not be published. Required fields are marked *