Blog.Arcath.Net | Random rantings and code

Jun/09

30

Printer Script that uses a computers Active Directory OU

Well this is a special post:

  • Its the first post involving something i did for work
  • Its the first post where the code is written in VBS
  • Its the first Windows based post

This is a printer script i wrote to replace a script some else had written for one of the schools i work in, his script was a big, HOOOOGE case statement that ran of host name, e.g.

pc1 -> printer 1
pc2 -> printer 1 & 2
pc3 -> printer 1 & 2

This to the end user works exactly the same as the new script, but from my point of view the new script is allot better.

When the user logs in the script looks at the host name and then asks Active Directory where that computer is in its orginsational Structure. This means that if i move a PC in active directory the printers it gets change. You can tell how much better that is already, instead of having to rewite its case the new script just changes it for me.

So then its time to post the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'Printer Script based on AD Group
'Written by Adam Laycock (Arcath)
'July 2009

'Variables

Set WshNetwork = WScript.CreateObject("WScript.Network")
 
Set oPrinters = WshNetwork.EnumPrinterConnections
 
printServer = "SERVERNAME" 'Set this to the machine that is your Print Server

DefaultColourPrinter = "\PRINTERNAME" 'The Main Colour Printer

DefaultBWPrinter = "\PRINTERNAME" 'The Main Black and White Printer

 
'Functions

'Get First Match
'Returns the First REGEX Match for the pattern you supply
'from http://www.somacon.com/p138.php
Function GetFirstMatch(PatternToMatch, StringToSearch)
	Dim regEx, CurrentMatch, CurrentMatches
 
	Set regEx = New RegExp
	regEx.Pattern = PatternToMatch
	regEx.IgnoreCase = True
	regEx.Global = True
	regEx.MultiLine = True
	Set CurrentMatches = regEx.Execute(StringToSearch)
 
	GetFirstMatch = ""
	If CurrentMatches.Count >= 1 Then
		Set CurrentMatch = CurrentMatches(0)
		If CurrentMatch.SubMatches.Count >= 1 Then
			GetFirstMatch = CurrentMatch.SubMatches(0)
		End If
	End If
	Set regEx = Nothing
End Function
 
 
'Remove all the Network Printers from the Machine
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
 
'Connect to AD
Set objSysInfo = CreateObject("ADSystemInfo")
 
 
'Get LDAP entry to current computer object.
strComputerDN = objSysInfo.ComputerName
Set objComputer = GetObject("LDAP://" & strComputerDN)
 
strOU = GetFirstMatch("OU=(.*?),OU=", strComputerDN)
 
 
Select Case (LCase(strOU))
	'For Each OU make a Case
	'note that the OU Name is made to be all lower case
	case "room1"
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & DefaultColourPrinter
 		WshNetwork.SetDefaultPrinter "\\" & printServer & DefaultColourPrinter
	case "room2"
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & DefaultBWPrinter
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & DefaultColourPrinter
 		WshNetwork.SetDefaultPrinter "\\" & printServer & DefaultBWPrinter
End Select

and now an explination

First off it declares all its variables and creates some objects, can you tell i wrote this to be used in more than 1 place?

It then declares 1 function. This comes from http://www.somacon.com/p138.php and is basically a find the first hit to a regex string funtion. I had never done any regex in VBS and thier function does exactly what i need.

Then comes some actual printer management. It starts by removing all the connections to network printers that the machine has. This way the machine only gets the printers that you want it to (other than any connected by a cable to it).

This is followed by making a connection to active directory to get the AD String for the machine. An example of this is “CN=pc1,OU=Room1,OU=Site,DC=Arcath,DC=NET” This is then passed into the function i mentioned earlier and the first OU is returned, in this case “Room1″.

It then has a case statement where you place a case for each OU, in each case simply place the code for a simple printer script.

RSS Feed

1 Comment for Printer Script that uses a computers Active Directory OU

problems with my printer Script, can you cast your eye over it? | November 17, 2009 at 12:14 pm

[...] I use a script that picks up the OU of the computer, so when i move a machine in AD (say from year4 classroom to year 5 classroom) it will change the printer for me. my script is better explained here: Printer Script that uses a computers Active Directory OU – Blog.Arcath.Net [...]

Leave a comment!

« HsDB Class for Ruby

Ruby on Rails with Apache using mod_rails »

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

Theme Design by devolux.org