NOTE: Editing is disabled for now.

Printer Addition Based on Location

From Wiki

Jump to: navigation, search

Contents

Prerequisites

This script requires that you name your client machines based upon their location. In the example below, the machine-names start with the room number followed by a hyphen and a computer number.

The script is written in VBScript and must be run as a login script - this can be easily done via GPO.

Noteworthy sections

Deleting Printers from Previous Sessions

The first part of note is the deletion of any existing printers from previous sessions. This is simply an enumeration of printers followed by a deletion of any printers that are not conencted locally.

' Delete existing connections to network printers
For i = 0 to oPrinters.Count - 1 Step 2
            On Error Resume Next
	    if Left(oPrinters.Item(i), 3) <> "lpt" And Left(oPrinters.Item(i), 3) <> "usb" then
             	WshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true
            else WScript.Echo "No network printers found"
            end if
Next

Dealing with Citrix/Terminal Services Sessions

The script ordinarily uses the VBScript ComputerName call to decide what the client is called. In a Terminal Services environment, this returns the name of the terminal server - which is pretty useless in this instance.

So that we get the 'correct' client name we have to expand the variable %CLIENTNAME% and assign it to our local variable that we check against. My terminal servers are all named to begin with 'svrts' for easy identification. If the computer name variable starts with this it must be a terminal server therefore.

' Citrix specific section

if (Left(computerName, 5) = "svrts") then
 
	computerName = LCase(WshShell.ExpandEnvironmentStrings("%CLIENTNAME%"))
 
end if

Selecting the Print Server

By using a variable for the print server, we can easily move the printers around (as long as their names are the same on the new server).

' Variable to select print server

Dim printServer
 
printServer = "MyFirstPrintServer"

Checking Where the Computer Is & Assigning the Printers

I will be looking at the first 4 characters of the computername variable since this varies according to location for me. To check a different part of the name, simply change what is evaluated for the case statement.

Select Case (Left(computerName, 4))
 
	Case "rm01"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\RM01-MONO"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\RM01-COLOUR"
 
		WshNetwork.SetDefaultPrinter "\\" & printServer & "\RM01-MONO"
 
	Case "rm02"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\RM02-MONO"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\RM02-COLOUR"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\RM02-A3"
 
		WshNetwork.SetDefaultPrinter "\\" & printServer & "\RM02-MONO"
 
End Select

Random Computers

Some computers do not belong in a room or are random... these can be added as follows.

Select Case (computerName)
 
	Case "random-computer-1"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\printer1"
 
		WshNetwork.SetDefaultPrinter "\\" & printServer & "\printer1"
 
	Case "random-computer-2"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\printer2"
 
		WshNetwork.SetDefaultPrinter "\\" & printServer & "\printer2"
End Select

The Script in Full

' Script to add network printers to workstations and thin clients

 
' Author: Ric Charlton

 
 
' Declare variables and enumerate existing printer connections

On Error Resume Next
 
 
 
Set WshShell = WScript.CreateObject("WScript.Shell")
 
Set WshNetwork = WScript.CreateObject("WScript.Network")
 
Set oPrinters = WshNetwork.EnumPrinterConnections
 
 
 
Dim computerName
 
computerName = LCase(WshNetwork.ComputerName)
 
 
 
' Delete existing connections to network printers

For i = 0 to oPrinters.Count - 1 Step 2
 
            On Error Resume Next
 
	    if Left(oPrinters.Item(i), 3) <> "lpt" And Left(oPrinters.Item(i), 3) <> "usb" then
 
             	WshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true
 
            else WScript.Echo "No network printers found"
 
            end if
 
Next
 
 
 
' Citrix specific section

if (Left(computerName, 5) = "svrts") then
 
	computerName = LCase(WshShell.ExpandEnvironmentStrings("%CLIENTNAME%"))
 
end if
 
 
 
' Add printer connections dependant upon location

' Variable to select print server

Dim printServer
 
printServer = "MyFirstPrintServer"
 
 
 
Select Case (Left(computerName, 4))
 
	Case "rm01"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\RM01-MONO"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\RM01-COLOUR"
 
		WshNetwork.SetDefaultPrinter "\\" & printServer & "\RM01-MONO"
 
	Case "rm02"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\RM02-MONO"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\RM02-COLOUR"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\RM02-A3"
 
		WshNetwork.SetDefaultPrinter "\\" & printServer & "\RM02-MONO"
 
End Select
 
 
 
Select Case (computerName)
 
	Case "random1"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\printer1"
 
		WshNetwork.SetDefaultPrinter "\\" & printServer & "\printer1"
 
	Case "random2"
 
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\printer2"
 
		WshNetwork.SetDefaultPrinter "\\" & printServer & "\printer2"
 
End Select

Inspecting the Computer Name

There are other methods that can be used to inspect the computername. For instance a 'split' function can be used so that you look before or after a certain character (e.g. a '-').

The split function should replace the start of the select statement and reads as follows:

Dim x
 
x = split(computerName,"-") 
 
Select Case x(0)
    Case "rm01"
     ...

Changing the number in select statement, chooses which part of the computer name is inspected. Using RM-01-COMPUTER as the example, x(0) returns RM, x(1) returns 01 and x(2) returns COMPUTER.