+ Post New Thread
Results 1 to 9 of 9
Scripts Thread, Hacked together printer script in Coding and Web Development; Used Maniac's and (at least one others) scripts to make my own. Removes all network printers (leaves local ones) and ...
  1. #1

    Join Date
    Mar 2007
    Location
    Central Scotland
    Posts
    829
    Thank Post
    12
    Thanked 8 Times in 8 Posts
    Rep Power
    19

    Hacked together printer script

    Used Maniac's and (at least one others) scripts to make my own.

    Removes all network printers (leaves local ones) and uses Maniacs script to add printers according to the container OU of the computer.

    I've got it working as a logon script for users, anyone know if it's possible to use it as a startup script for computer accounts? I can't seem to get it to run that way! Also if anyone is bored feel free to tidy it up - I'm no scripting genius like many on here

    One question though, would it be possible to make it completely skip the whole thing (deletion of printers and adding new ones) if the computer isn't in one of the UOs listed?

    =================
    on error resume next
    'Set variables.

    Set shell= WScript.CreateObject("WScript.Shell")
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    dim Counter, PrinterPath

    'Remove All Network Printers.

    Set oPrinters = WshNetwork.EnumPrinterConnections
    FOR Counter = 0 to oPrinters.Count - 1
    IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN
    PrinterPath = oPrinters.Item(Counter)
    WshNetwork.RemovePrinterConnection PrinterPath, True, True
    End If
    Next
    'Set even more variables.
    Set objWshNetwork = CreateObject("WScript.Network")
    Set objAdsSystemInfo = CreateObject("adsysteminfo")
    Set objComputerName = GetObject("LDAP://" & objAdsSystemInfo.ComputerName)
    Set objOU = GetObject(objComputerName.Parent)
    strOU = replace(objOU.Name,"OU=","")

    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
    'Add printers depending on AD OU containing computer.
    Next

    Select Case strOU
    Case "IT1"
    objWshNetwork.AddWindowsPrinterConnection "\\print\Dell1700 IT1"
    objWshNetwork.SetDefaultPrinter "\\PRINT\Dell1700 IT1"
    objWshNetwork.AddWindowsPrinterConnection "\\print\HP2500C IT1"

    Case "IT2"
    objWshNetwork.AddWindowsPrinterConnection "\\print\Dell 1700n IT2"
    objWshNetwork.SetDefaultPrinter "\\PRINT\Dell 1700n IT2"


    End Select
    =================

    I trawled through a lot of the scripts on this forum to put this together so none of it is my own work!

    The reasons I wanted it to delete the printers before creating new ones is it'll remove any problems of moving computers around between containers etc.

  2. #2

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,258
    Thank Post
    671
    Thanked 1,644 Times in 1,466 Posts
    Rep Power
    424

    Re: Hacked together printer script

    Printers added like this are a per user thing.

    Ben

  3. #3

    Join Date
    Mar 2007
    Location
    Central Scotland
    Posts
    829
    Thank Post
    12
    Thanked 8 Times in 8 Posts
    Rep Power
    19

    Re: Hacked together printer script

    Ah that'd explain why I couldn't get it working at machine startup

  4. #4
    contink's Avatar
    Join Date
    Jul 2006
    Location
    South Yorkshire
    Posts
    3,791
    Thank Post
    303
    Thanked 327 Times in 233 Posts
    Rep Power
    118

    Re: Hacked together printer script

    Also worth noting that the current script will only work if you have a single OU layer.

    ie: ou=foo, cn=foos, dc=my, dc=local

    ... this little function (thanks AJbritton) helps in other instances.
    Code:
    ' Check if user is member of an Organisational Unit
    Function UserInOU(sOU)
    	' Returns true if the user is a member of the specified OU (or descendant)
    	Dim oAdSys, sUserDN
    	Set oAdSys = CreateObject("ADSystemInfo")
    	sUserDN = UCase(oAdSys.UserName)
    	UserInOU = (InStr(1, sUserDN, UCase(sOU)) > 0)
    End function

  5. #5

    Join Date
    Mar 2007
    Location
    Central Scotland
    Posts
    829
    Thank Post
    12
    Thanked 8 Times in 8 Posts
    Rep Power
    19

    Re: Hacked together printer script

    we only have a single layer at present but yeah that's pretty useful Contink

  6. #6

    Join Date
    Mar 2007
    Location
    Central Scotland
    Posts
    829
    Thank Post
    12
    Thanked 8 Times in 8 Posts
    Rep Power
    19

    Re: Hacked together printer script

    Hi all,

    I've changed the script quite a bit this morning as it wasn't quite doing what I wanted it to. No doubt this can be done in a neater way as I'm very much a VB script newbie!

    Essentially the script checked the UO the workstation is in, if it matches one of the list then it deletes all network printers and adds in specified printers and makes one default.

    I wanted it to delete all printers as I have a few laptops that are on loan between departments and I can simply drag them into the different UO as the departments use them, the staff get very annoyed if their print outs appear in the other departments printers so I simply stop this happening by removing the bloomin printers!

    It works for me so far, still have to test it a little with students more restrictive accounts though.


    ==================
    on error resume next
    'Set variables.

    Set shell= WScript.CreateObject("WScript.Shell")
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    dim Counter, PrinterPath
    Set objWshNetwork = CreateObject("WScript.Network")
    Set objAdsSystemInfo = CreateObject("adsysteminfo")
    Set objComputerName = GetObject("LDAP://" & objAdsSystemInfo.ComputerName)
    Set objOU = GetObject(objComputerName.Parent)
    strOU = replace(objOU.Name,"OU=","")

    Dim computerName
    computerName = LCase(WshNetwork.ComputerName)

    'Add printers depending on AD OU containing computer.

    if strOU="IT1" then
    Set oPrinters = WshNetwork.EnumPrinterConnections
    FOR Counter = 0 to oPrinters.Count - 1
    IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN
    PrinterPath = oPrinters.Item(Counter)
    WshNetwork.RemovePrinterConnection PrinterPath, True, True
    End If
    objWshNetwork.AddWindowsPrinterConnection "\\print\Dell1700 IT1"
    objWshNetwork.SetDefaultPrinter "\\PRINT\Dell1700 IT1"
    objWshNetwork.AddWindowsPrinterConnection "\\print\HP2500C IT1"
    Next
    elseif strOU="IT2" then
    Set oPrinters = WshNetwork.EnumPrinterConnections
    FOR Counter = 0 to oPrinters.Count - 1
    IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN
    PrinterPath = oPrinters.Item(Counter)
    WshNetwork.RemovePrinterConnection PrinterPath, True, True
    End If
    objWshNetwork.AddWindowsPrinterConnection "\\print\Dell 1700n IT2"
    objWshNetwork.SetDefaultPrinter "\\PRINT\Dell 1700n IT2"
    Next
    elseif strOU="Mathematics" then
    Set oPrinters = WshNetwork.EnumPrinterConnections
    FOR Counter = 0 to oPrinters.Count - 1
    IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN
    PrinterPath = oPrinters.Item(Counter)
    WshNetwork.RemovePrinterConnection PrinterPath, True, True
    End If
    objWshNetwork.AddWindowsPrinterConnection "\\print\HP LJ2100 Maths"
    objWshNetwork.SetDefaultPrinter "\\PRINT\HP LJ2100 Maths"
    Next
    else
    'No printers added or deleted
    End if
    ==============

  7. #7
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    42

    Re: Hacked together printer script

    I would probably clean it up abit:

    Code:
    on error resume next
    'Set variables.
    
    Set shell= WScript.CreateObject("WScript.Shell")
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    dim Counter, PrinterPath
    Set objWshNetwork = CreateObject("WScript.Network")
    Set objAdsSystemInfo = CreateObject("adsysteminfo")
    Set objComputerName = GetObject("LDAP://" & objAdsSystemInfo.ComputerName)
    Set objOU = GetObject(objComputerName.Parent)
    strOU = replace(objOU.Name,"OU=","")
    Set oPrinters = WshNetwork.EnumPrinterConnections
    
    Dim computerName
    computerName = LCase(WshNetwork.ComputerName)
    
    'Add printers depending on AD OU containing computer.
    
    Select Case strOU
    	Case "IT1"
            	removePrinters
    		addPrinter "\\print\Dell1700 IT1",True
    		addPrinter "\\print\HP2500C IT1",False
    	Case "IT2"
    		removePrinters
    		addPrinter "\\print\Dell 1700n IT2",True
    	Case "Mathematics"
    		removePrinters
    		addPrinter "\\print\HP LJ2100 Maths",True
    
    end select
    
    function removePrinters()
    
    	FOR Counter = 0 to oPrinters.Count - 1
    		IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN
    			PrinterPath = oPrinters.Item(Counter)
    			WshNetwork.RemovePrinterConnection PrinterPath, True, True
    		end if
    	next
    
    end function
    
    function addPrinter(strConnectString,isDefault)
    
    		if strConnectString <> "" then
    				strResult = WshNetwork.AddWindowsPrinterConnection(strConnectString)
    			if isDefault then
    				WshNetwork.SetDefaultPrinter strConnectString
    			end if
    		end if
    end function
    somehting like this should work.. i think

  8. #8

    Join Date
    Mar 2007
    Location
    Central Scotland
    Posts
    829
    Thank Post
    12
    Thanked 8 Times in 8 Posts
    Rep Power
    19

    Re: Hacked together printer script

    Cheers Apeo,

    looks much more tidy. I've edited it and added in all of our printers. works like a dream

    rayer: Apeo

  9. #9
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    42

    Re: Hacked together printer script

    Glad to help

SHARE:
+ Post New Thread

Similar Threads

  1. A Printer Script
    By Ric_ in forum Scripts
    Replies: 164
    Last Post: 1st October 2010, 10:11 AM
  2. logon script for printer
    By ozzy in forum Scripts
    Replies: 6
    Last Post: 22nd November 2007, 03:03 PM
  3. Printer Script
    By faza in forum Scripts
    Replies: 9
    Last Post: 14th September 2007, 08:30 AM
  4. Logon script & Printer script question.
    By Galway in forum Windows
    Replies: 3
    Last Post: 29th August 2007, 11:00 AM
  5. Script to delete printer
    By tosca925 in forum Scripts
    Replies: 2
    Last Post: 31st August 2005, 07:45 PM

Thread Information

Users Browsing this Thread

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

Posting Permissions

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