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

On Error Resume Next

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


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
End If

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

For Each Group In Groups
GroupCount = GroupCount + 1

ReDim GroupList(GroupCount -1)
i = 0
For Each Group In Groups
GroupList(i) = Group.Name
i = i + 1
End Sub

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

Group Name,x:,\\Servername\share_name
Group Name,!!,\\Print_Server\Printer

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.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.