+ Post New Thread
Results 1 to 5 of 5
Scripts Thread, Printer Script that uses a machines AD OU in Coding and Web Development; In this topic I asked how to find out what Active directory OU and Machine was in. Using a bit ...
  1. #1
    Arcath's Avatar
    Join Date
    Feb 2009
    Location
    Lancashire
    Posts
    972
    Thank Post
    102
    Thanked 116 Times in 101 Posts
    Rep Power
    74

    Printer Script that uses a machines AD OU

    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:

    Code:
    '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
    Last edited by Arcath; 30th June 2009 at 08:42 PM. Reason: More testing has occoured

  2. #2

    Join Date
    May 2010
    Posts
    6
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Thumbs up Another Printer script...

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

    Code:
    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

  3. #3


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,505
    Thank Post
    227
    Thanked 848 Times in 727 Posts
    Rep Power
    287
    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

    Code:
    ' 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
    Code:
    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

  4. #4
    Arcath's Avatar
    Join Date
    Feb 2009
    Location
    Lancashire
    Posts
    972
    Thank Post
    102
    Thanked 116 Times in 101 Posts
    Rep Power
    74
    Quote Originally Posted by sted View Post
    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

    Code:
    ' 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
    Code:
    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.

  5. #5


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,505
    Thank Post
    227
    Thanked 848 Times in 727 Posts
    Rep Power
    287
    Quote Originally Posted by Arcath View Post
    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

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 13
    Last Post: 20th February 2008, 04:29 PM
  2. Logon script & Printer script question.
    By Galway in forum Windows
    Replies: 3
    Last Post: 29th August 2007, 11:00 AM
  3. Set Default Printer to Machines with Local Printers
    By Heebeejeebee in forum Windows
    Replies: 15
    Last Post: 5th February 2007, 03:00 PM
  4. Replies: 3
    Last Post: 6th December 2006, 11:01 PM
  5. script to copy files to machines
    By browolf in forum Scripts
    Replies: 1
    Last Post: 28th November 2005, 10:28 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •