Distributed printer allocation script

Scenario:

You have been told that you need to relocate your users from a one-story building to a four-story building.  On the first floor will be one group that shares a printer and their manager.  The second floor will have two groups (each with their own printer) and a manager.  The third floor will be another group with their own printer, part of the accounting group (with their own printer) and the accounting manager.  Floor four will be the rest of accounting.  Each manager will have their own printer.

Solution:

Since the changes are purely physical (location), not a re-org, you will be maintaining your old AD OU structure, while adding a set of groups that are location-based and used purely for printer assignments, as follows:

  • First-Floor
  • Second-Floor
  • Third-Floor
  • Fourth-Floor

For the sake of this scenario, the existing groups we will also rely on are rather generically named (hey, get more creative!):

  • Second-Floor-Sub-Group ‘ Actually named something more descriptive, I hope!
  • Managers ‘ A security group for accessing certain folders, perhaps?  Or e-mail enabled?  Bad!
  • Accounting

I use this structure simply to demonstrate a few basic options for assigning printers in the logon script. This script is a modified version of one that I used for a scenario very similar to this.


' Variable Declarations

Dim WSHNetwork
Dim strUserName    ' Current user
Dim strUserDomain  ' Current User's domain name
Dim ObjPrinterList ' Dictionary of user printer mappings
Dim objGroupList
Dim objUser
Dim strOldServer
Dim strNewServer
Dim strCurrentPrinter
Dim strPrinterToDelete
Dim strDefaultPrinter

strOldServer = "old-print-2k"
strNewServer = "\\new-ps-2k12\"
'
' Wait until the user is really logged in...
'
Set WSHNetwork = WScript.CreateObject("WScript.Network")
strUserName = ""
While strUserName = ""
    WScript.Sleep 100 ' 1/10 th of a second
    strUserName = WSHNetwork.UserName
Wend

' Remove printer mappings that are no longer needed
Set ObjPrinterList = WshNetwork.EnumPrinterConnections
For i = 1 to ObjPrinterList.Count Step 2
    strCurrentPrinter = ObjPrinterList.Item(i)
    wscript.echo strCurrentPrinter
    ' check to see if the printer is on the old server, if so then delete it
    If (InStr(1, strCurrentPrinter, strOldServer, 1) > 0) Then
        strPrinterToDelete = strCurrentPrinter
        WshNetwork.RemovePrinterConnection strPrinterToDelete, true, true
    End If
Next

' Get the users group memberships
Set objGroupList = CreateObject("Scripting.Dictionary")
objGroupList.CompareMode = vbTextCompare
Set objUser = GetObject("WinNT://" _
    & WSHNetwork.UserDomain & "/" _
    & strUserName & ",user")
For Each objGroup In objUser.Groups
    objGroupList.Add objGroup.Name, " , "
Next

' and load the printers based on the users
' group memberships
If (CBool(objGroupList.Exists("First-Floor"))) Then
    ' This floor is just one group with a manager
    WshNetwork.AddWindowsPrinterConnection strNewServer & "1st-Floor-Printer"
    If (CBool(objGroupList.Exists("Managers"))) Then
        WshNetwork.AddWindowsPrinterConnection strNewServer & "1st-Floor-Mgr-Printer"
        WshNetwork.SetDefaultPrinter strNewServer & "1st-Floor-Mgr-Printer"
    Else
        WshNetwork.SetDefaultPrinter strNewServer & "1st-Floor-Printer"
    End If
End If
If (CBool(objGroupList.Exists("Second-Floor"))) Then
    ' This floor has a main group, a specialized group, and one manager
    strDefaultPrinter = "2nd-Floor-Printer"
    WshNetwork.AddWindowsPrinterConnection strNewServer & "2nd-Floor-Printer"
    If (CBool(objGroupList.Exists("Second-Floor-Sub-Group"))) Then
        WshNetwork.AddWindowsPrinterConnection strNewServer & "2nd-Floor-Printer-2"
        strDefaultPrinter = "2nd-Floor-Printer-2"
    End If
    If (CBool(objGroupList.Exists("Managers"))) Then
        WshNetwork.AddWindowsPrinterConnection strNewServer & "2nd-Floor-Mgr-Printer"
        strDefaultPrinter = "2nd-Floor-Mgr-Printer"
    End If
    WshNetwork.SetDefaultPrinter strNewServer & strDefaultPrinter
End If
If (CBool(objGroupList.Exists("Third-Floor"))) Then
    ' This floor has two groups, each with their own printer and manager
    ' The "Accounting" group is split between floors 3 and 4
    If (CBool(objGroupList.Exists("Accounting"))) Then
        WshNetwork.AddWindowsPrinterConnection strNewServer & "3rd-Floor-Acct-Printer"
        strDefaultPrinter = "3rd-Floor-Acct-Printer"
        If (CBool(objGroupList.Exists("Managers"))) Then
            WshNetwork.AddWindowsPrinterConnection strNewServer & "3rd-Floor-Acct-Mgr-Printer"
            strDefaultPrinter = "3rd-Floor-Acct-Mgr-Printer"
        End If
    Else
        WshNetwork.AddWindowsPrinterConnection strNewServer & "3rd-Floor-GroupA-Printer"
        strDefaultPrinter = "3rd-Floor-GroupA-Printer"
        If (CBool(objGroupList.Exists("Managers"))) Then
            WshNetwork.AddWindowsPrinterConnection strNewServer & "3rd-Floor-GroupA-Mgr-Printer"
            strDefaultPrinter = "3rd-Floor-GroupA-Mgr-Printer"
        End If
    End If
    WshNetwork.SetDefaultPrinter strNewServer & strDefaultPrinter
End If
If (CBool(objGroupList.Exists("Fourth-Floor"))) Then
    ' This floor is just the "Accounting" group overflow
    WshNetwork.AddWindowsPrinterConnection strNewServer & "4th-Floor-Acct-Printer"
    WshNetwork.SetDefaultPrinter strNewServer & "4th-Floor-Acct-Printer"
End If