Jump to content

Recommended Posts

Posted (edited)

In this topic I asked how to find out what Active directory OU and Machine was in.

 

Using a bit of code from the posts there and a Regex function i found i wrote this:

 

'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

 

First off it deletes all network printer connections from a machine (needed this to clean up from the old script) This helps if any kids happen to add any persistent connections (e.g. with a script of thier own).

 

It then creates a connection to Active Directory and gets the computers AD String (Normally CN=COMPUTERNAME,OU=OU1,OU=OU2,DC=DOMAIN,DC=DOMAIN)

 

This string is then passed through a regex function that returns the first OU for that computer, in my case this is where the OU for each room was.

 

It then has a big case statement for each ou (you have to write each case)

 

it would be possible to do away with the case statement and have it add a printer with the same name as the OU assuming that your printers where named like that

 

 

I Have tested this on a couple of domains and it works as i expected it to

Edited by Arcath
More testing has occoured
  • 10 months later...
Posted

Another Script along similar lines - just runs quicker than the one above - and mixes OU with computer name for mapping.

 


Dim wshShell, wshNetwork
Dim strComputerName
Dim computerName

' Create Global Objects
Set wshShell = CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

On Error Resume Next

' Initialize Variables
strComputerName = wshNetwork.ComputerName
computerName = UCase(strComputername)


Function GetDN()
' Use the NameTranslate object to convert the NT name of the computer to
' the Distinguished name required for the LDAP provider. Computer names
' must end with "$". Returns comma delimited string to calling code.

   Dim objTrans, objDomain
   ' Constants for the NameTranslate object.
   Const ADS_NAME_INITTYPE_GC = 3
   Const ADS_NAME_TYPE_NT4 = 3
   Const ADS_NAME_TYPE_1779 = 1

   Set objTrans = CreateObject("NameTranslate")
   Set objDomain = getObject("LDAP://rootDse")
   objTrans.Init ADS_NAME_INITTYPE_GC, ""
   objTrans.Set ADS_NAME_TYPE_NT4, wshNetwork.UserDomain & "\" _
   & strComputerName & "$"
   GetDN = objTrans.Get(ADS_NAME_TYPE_1779)
   'Set DN to upper Case
   GetDN = UCase(GetDN)
End Function

' Delete existing connections to network printers (uncomment to use this)

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

  
'Now define the main Print Server if required - can also use specific names
'such as "\\PS1\Printqueuename" in select statements

Dim printServer
printServer = "PrintServerName"


' Add printer connections dependant upon location (in this case lowest-level OU)
' GetOU is DN from the first "OU=" statement

GetOU = Mid(GetDN,InStr(GetDN,"OU=")+3)

' Now add the printers - NB Case Stements MUST be Unique or more specific lower down the file
' as last match will be actioned - also need to be in UPPERCASE.

Select Case (Left(GetOU, 3))

Case "D&T" 
	WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\D&T A3 Colour"
		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\D&T Laser"
		WshNetwork.SetDefaultPrinter "\\" & printServer & "\D&T Laser"

Case "ART" 
	WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\Art Printer" 
	WshNetwork.SetDefaultPrinter "\\" & printServer & "\Art Printer"	

End Select

	
Select Case (Left(GetOU, 4))

       Case "BIOL" 
	WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\Biology Laser" 
	WshNetwork.SetDefaultPrinter "\\" & printServer & "\Biology Laser"
	
Case "EXAM" 
	WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\Exams Printer" 

End Select 


'Pick up any random computers and allocate them a printer

'Select Case (computerName)  {or Select Case Left(ComputerName,n)

'	Case "RANDOM"

'		WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "RANDOM PRINTER"
'  		WshNetwork.SetDefaultPrinter "\\" & printServer & "\RANDOM PRINTER"
	
'End Select

Posted

why not just write short scripts that copy a vbs to local pc's all user startup folder based on the ou? as long as you copy it with teh same name the script can be only a few lines long

 

so script itself would be something like

 

' Printers.vbs - Jonathan
Option Explicit
Dim objNetwork
on error resume next

Set objNetwork = CreateObject("WScript.Network") 
objNetwork.AddWindowsPrinterConnection "\\server\laser1"
objNetwork.AddWindowsPrinterConnection "\\server\colour1"
objNetwork.AddWindowsPrinterConnection "\\server\hallcolour1"

objNetwork.SetDefaultPrinter "\\server\Laser1"

 

and

copy "\\server\netlogon\suite1.printers.vbs.lnk" "%allusersprofile%start menu\programs\startup\printers.lnk" /y

saved as suite1print.bat and assigned as a script assigned to the suite 1 ou then renaming / moving a pc is handled just by moving it round in ad. just copy and alter for other locations

Posted
why not just write short scripts that copy a vbs to local pc's all user startup folder based on the ou? as long as you copy it with teh same name the script can be only a few lines long

 

so script itself would be something like

 

' Printers.vbs - Jonathan
Option Explicit
Dim objNetwork
on error resume next

Set objNetwork = CreateObject("WScript.Network") 
objNetwork.AddWindowsPrinterConnection "\\server\laser1"
objNetwork.AddWindowsPrinterConnection "\\server\colour1"
objNetwork.AddWindowsPrinterConnection "\\server\hallcolour1"

objNetwork.SetDefaultPrinter "\\server\Laser1"

 

and

copy "\\server\netlogon\suite1.printers.vbs.lnk" "%allusersprofile%start menu\programs\startup\printers.lnk" /y

saved as suite1print.bat and assigned as a script assigned to the suite 1 ou then renaming / moving a pc is handled just by moving it round in ad. just copy and alter for other locations

 

 

This would work but it would take longer to respond to changes, and does exactly what my script does.

 

just to clarify it would be slower to respond to changes as the .bat would be in startup hence would need to PC to reboot before it even looked for changes.

 

also im not a fan of having scripts copy to the local machine, plus "printer.vbs" would then show up in the startup folder.

Posted
This would work but it would take longer to respond to changes, and does exactly what my script does.

 

just to clarify it would be slower to respond to changes as the .bat would be in startup hence would need to PC to reboot before it even looked for changes.

 

also im not a fan of having scripts copy to the local machine, plus "printer.vbs" would then show up in the startup folder.

 

horses for courses i like it as it is simper script but i admit it wont make changes till a reboot but then i generally move pcs when they are turned off lol. as to showing in startup im ok with that incase it fails first time say print server is rebooting etc it can be redone

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...