Map Network Shares and Printers in Active Directory

2011-01-21

When I dug around among a bunch of old code I’ve written, I found a script in VBScript that is made for a Windows Server environment to map network printers and network drives to client computers, depending on the which groups the users belong. This script is meant to run at logon to a Windows domain and connects different directories to a client computer’s hard drive list. It also connects one or more printers to the client. For some groups, a default printer is set.

The script begins by picking up the user’s groups from the information from Active Directory. Then, a few network shares are connected for all types of users to use. After running a few selections to split apart the groups, their respective network drive and printers are connected. It’s been nearly three years since I last tested the script but it should still work fine.

Option Explicit
Dim ObjNetwork, objUser, CurrentUser, arrGroup, strGroup

WScript.Echo "Welcome to HansWestman.se"
Set ObjNetwork = WScript.CreateObject("WScript.Network")
Set objUser = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
arrGroup = CurrentUser.GetEx("memberOf")
strGroup = LCase(Join(arrGroup))

'###### THINGS THAT APPLY TO ALL USERS ############
ObjNetwork.MapNetworkDrive "W:", "\\fileserver.hanswestman.se\WWWPub"
ObjNetwork.MapNetworkDrive "M:", "\\fileserver.hanswestman.se\Temp"
ObjNetwork.AddWindowsPrinterConnection "\\printserver.hanswestman.se\printer1"
ObjNetwork.AddWindowsPrinterConnection "\\printserver.hanswestman.se\printer2"
'#################################################

    '##### THINGS THAT APPLIES TO ALL TEACHERS #####
    If InStr(strGroup, lcase("Teachers")) Then
        ObjNetwork.MapNetworkDrive "U:", "\\fileserver.hanswestman.se\StudentShare"
        ObjNetwork.MapNetworkDrive "T:", "\\fileserver.hanswestman.se\TeacherShare"
        ObjNetwork.MapNetworkDrive "S:", "\\fileserver.hanswestman.se\StaffShare"
        ObjNetwork.AddWindowsPrinterConnection "\\printserver.hanswestman.se\printer3"
        ObjNetwork.SetDefaultPrinter "\\printserver.hanswestman.se\printer2"
    End If

    '##### THINGS THAT APPLY TO ALL ADMINISTRATIVE STAFF #####
    If InStr(strGroup, lcase("Administrative personnel")) Then
        ObjNetwork.MapNetworkDrive "T:", "\\fileserver.hanswestman.se\TeacherShare"
        ObjNetwork.MapNetworkDrive "S:", "\\fileserver.hanswestman.se\StaffShare"
        ObjNetwork.AddWindowsPrinterConnection "\\printserver.hanswestman.se\printer3"
        ObjNetwork.SetDefaultPrinter "\\printserver.hanswestman.se\printer3"
    End If

    '##### THINGS THAT APPLIES TO ALL STUDENTS #####
    If InStr(strGroup, lcase("All students")) Then
        ObjNetwork.MapNetworkDrive "U:", "\\fileserver.hanswestman.se\StudentShare"
        ObjNetwork.SetDefaultPrinter "\\printserver.hanswestman.se\printer1"
    End If

    '##### THINGS RELATING TO THE ACCOUNTING DEPARTMENT #####
    If InStr(strGroup, lcase("Economy")) Then
        ObjNetwork.MapNetworkDrive "K:", "\\fileserver.hanswestman.se\Economy"
    End If

WScript.Echo "Network resources connected!"
WScript.Quit