+ Reply to Thread
Results 1 to 6 of 6

Thread: Location Aware Printing (VBS Script)

  Share/Bookmark
  1. #1

    Reputation Reputation Reputation Reputation
    Fatmas's Avatar
    Join Date
    Jul 2009
    Location
    UK
    Posts
    254
    Thank Post
    40
    Thanked 37 Times in 24 Posts
    Rep Power
    15

    Default Location Aware Printing (VBS Script)

    I have been tasked to write some VB scripts for the kids taking the ICT functional skills exam in January that will do the following:

    When the user logs on it will assign the correct printers to that machine and set the default printer accordingly.

    Delete any installed printers that are for a different room.


    This needs to work for laptops (we do not know which rooms these will be in beforehand so the user will have to select which room they are in.)


    For example:

    If they log onto a laptop in room 107 and select that room it will add the correct printer and delete any previously assigned printers..


    I found one on here but am not sure how useful it will be:

    Printing by location

    Could anyone point me in the right direction or suggest a script I could modify? (Lazy I know but if the resources are already available why not)


    I'll continue looking on the interwebs in the mean time.

    Thanks guys

  2. #2

    Reputation Reputation
    Gerry's Avatar
    Join Date
    Jun 2007
    Location
    North Wales
    Posts
    323
    Thank Post
    38
    Thanked 24 Times in 22 Posts
    Rep Power
    13

    Default

    Here's a skeleton of the vbs script we use with Group Policy:
    Code:
    on Error Resume Next
    Set wshNetwork = CreateObject("WScript.Network")
    
    Printer1="\\server_name\printer_share_name"
    
    'Deletes all network printers
    Set clPrinters = WshNetwork.EnumPrinterConnections
    On Error Resume Next
    For i = 0 to clPrinters.Count - 1 Step 2
    wshNetwork.RemovePrinterConnection clPrinters.Item(i+1), true
    Next
    
    'Sets up printers
    WshNetwork.AddWindowsPrinterConnection Printer1
    WshNetwork.SetDefaultPrinter Printer1
    
    For the odd PC that needs to print to the same printer but can't be a member of the same group, we just put a shortcut to the script (stored on a server) into the "All Users->Programs->Startup" folder. This script should only delete and add network printers. Any local printers should not be affected. Script will also work with printers shared on PC's.

    For the laptops, the simplest option is probably to put a link on the All Users desktop to each VBS script. There are other, fancier selection options such as using DOS, VBS InputBox function or creating an HTA app with buttons.

    If you wish to use the machine name to decide which printers to connect to, then the script you found should give you pointers on how to go about it with VBS.

  3. 2 Thanks to Gerry:

    dbrown (20-01-2010), Fatmas (07-12-2009)

  4. #3

    Reputation Reputation Reputation Reputation
    Fatmas's Avatar
    Join Date
    Jul 2009
    Location
    UK
    Posts
    254
    Thank Post
    40
    Thanked 37 Times in 24 Posts
    Rep Power
    15

    Default

    Cheers for that, I've started making the scripts for each room (six in total) but we just need a way for the user (a year 11 student) to select the correct room. Putting all the scripts on there might be an option but I'll investigate a more elegant solution as well.


    Thanks for your help

  5. #4

    Reputation

    Join Date
    Jul 2008
    Posts
    2
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Environmental Variables are your friends here!
    We use a (part of the server 2003 admin tools?) free utility called setx.exe via a batch file assigned to the startup scripts for a particular OU, which in turn relates to a particular room those computers are in.

    For instance i have an OU called 'B2' which is also the name of a classroom with those pcs in. In the startup scripts i have a batch file that runs 'C:\Scripts\SETX PRINT_TIME B2 -m' where PRINT_TIME is the variable name, B2 the value and the '-m' so that it is set as a machine variable.

    Once the desktop has ran this, it will stick for the rest of its OS install. Then when the user logs on a VBS runs that can query the environmental variables for thise and its value, then use a SELECT statement to run the correct printer connection line.

    Example;

    Code:
    Dim WshShell
    Dim WshNetwork
    Dim strPrintVar
    
    on error resume next
    
    Set WshShell = WScript.CreateObject("WScript.Shell") 
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    
    Set strPrintVar = WshShell.Environment("SYSTEM")
    
    	Select Case strPrintVar("PRINT_TIME")
                       Case "B2"				WshNetwork.AddWindowsPrinterConnection "\\DATA\B2-3-COLOUR"
    							WshNetwork.SetDefaultPrinter "\\DATA\B2-3-COLOUR"
    
    		   Case "B3"				WshNetwork.AddWindowsPrinterConnection "\\DATA\B2-B3 Laser Printer"
    							WshNetwork.SetDefaultPrinter "\\DATA\B2-B3 Laser Printer"	
            End Select
    
    We also have a script that deletes all network based printers off the users account as they log off but thats a straight rip from The Scripting Guy website if you search for it

    You can add as many printers as need - we have about 30 of them and it only takes 1-2 seconds for the script to run on a modest desktop.

  6. #5

    Reputation

    Join Date
    Nov 2009
    Posts
    79
    Thank Post
    3
    Thanked 4 Times in 4 Posts
    Rep Power
    2

    Default

    room =
    obj.regread("HKLM\SYSTEM\CurrentControlSet\Control \ComputerName\ComputerName\room")
    End If
    ' Sets / collects fixed setings
    printserver = "\\wbcc-print"


    ' Enumerates printer connections and deletes them
    Set oPrinters = WshNetwork.EnumPrinterConnections
    For Counter = 0 to oPrinters.Count - 1
    IF mid(oPrinters.Item(Counter + 1), 1, 2) = "\\" Then
    PrinterPath = oPrinters.Item(Counter + 1)
    WshNetwork.RemovePrinterConnection PrinterPath, True, True
    End If
    Next

    'Match's rooms to printers

    If room = "ROOM 7" Then
    PrinterPath1 = printserver & "\suite7biz190f"
    PrinterDriver1 = "KONICA MINOLTA 190f PCL6"
    Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
    Wshnetwork.setdefaultprinter printserver & "\suite7biz190f"
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    ElseIf room = "ROOM 8" Then
    PrinterPath1 = printserver & "\suite8biz190f"
    PrinterDriver1 = "KONICA MINOLTA 190f PCL6"
    Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
    Wshnetwork.setdefaultprinter printserver & "\suite8biz190f"
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    ElseIf room = "ROOM 9" Then
    PrinterPath1 = printserver & "\suite9biz190f"
    PrinterDriver1 = "KONICA MINOLTA 190f PCL6"
    Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
    Wshnetwork.setdefaultprinter printserver & "\suite9biz190f"
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    ElseIf room = "ROOMSB6" Then
    PrinterPath1 = printserver & "\suite6thformbiz190f"
    PrinterDriver1 = "KONICA MINOLTA 190f PCL6"
    Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
    Wshnetwork.setdefaultprinter printserver & "\suite6thformbiz190f"
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    ElseIf room = "ROOMSM6" Then
    PrinterPath1 = printserver & "\suite6thformbiz190f"
    PrinterDriver1 = "KONICA MINOLTA 190f PCL6"
    Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
    Wshnetwork.setdefaultprinter printserver & "\suite6thformbiz190f"
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    ElseIf room = "ROOMSR6" Then
    PrinterPath1 = printserver & "\suite6thformbiz190f"
    PrinterDriver1 = "KONICA MINOLTA 190f PCL6"
    Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
    Wshnetwork.setdefaultprinter printserver & "\suite6thformbiz190f"
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    ElseIf room = "ROOMSF6" Then
    PrinterPath1 = printserver & "\suite6thformbiz190f"
    PrinterDriver1 = "KONICA MINOLTA 190f PCL6"
    Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
    Wshnetwork.setdefaultprinter printserver & "\suite6thformbiz190f"
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    ElseIf room = "ROOM 10" Then
    PrinterPath1 = printserver & "\suite10biz190f"
    PrinterDriver1 = "KONICA MINOLTA 190f PCL6"
    Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    Wshnetwork.setdefaultprinter printserver & "\suite10biz190f"
    ElseIf room = "ROOM D5" Then
    PrinterPath1 = printserver & "\mfdkmc253design"
    Wshnetwork.addwindowsprinterconnection Printerpath1
    Wshnetwork.setdefaultprinter printserver & "\mfdkmc253design"
    ElseIf room = "Design04" Then
    PrinterPath1 = printserver & "\mfdkmc253design"
    Wshnetwork.addwindowsprinterconnection Printerpath1
    Wshnetwork.setdefaultprinter printserver & "\mfdkmc253design"
    ElseIf room = "LAB 8" Then
    PrinterPath1 = printserver & "\suitelab8biz190f"
    PrinterDriver1 = "HP LaserJet 1200 Series PCL 6"
    Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
    Wshnetwork.setdefaultprinter server & "\suitelab8biz190f"
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    ElseIf room = "LIBRARY" Then
    PrinterPath1 = printserver & "\mfdkm250library"
    Wshnetwork.addwindowsprinterconnection Printerpath1
    Wshnetwork.setdefaultprinter printserver & "\mfdkm250library"
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    ElseIf room = "staff" Then
    PrinterPath1 = printserver & "\KONICA MONO PULL QUEUE"
    Wshnetwork.addwindowsprinterconnection Printerpath1
    Wshnetwork.setdefaultprinter printserver & "\KONICA MONO PULL QUEUE"
    PrinterPath2 = printserver & "\KONICA COLOUR PULL"
    Wshnetwork.addwindowsprinterconnection Printerpath2
    Else
    End If


    Our print script reads the registry for a key which is set to the room and then adds the printers dependent on the room the user logs into.

  7. #6

    Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation Reputation

    Join Date
    Aug 2005
    Location
    London
    Posts
    2,810
    Blog Entries
    2
    Thank Post
    74
    Thanked 443 Times in 389 Posts
    Rep Power
    98

    Default

    @danlewis3 - there's nothing wrong with your script but this kind of thing has a tendency to grow (new rooms are added with printers etc) so my (personal!) preference is to do this kind of thing with a select statement:
    Code:
    select case room
      case "room7"
        PrinterPath1 = printserver & "\suite7biz190f"
        PrinterPath2 = printserver & "\KONICA COLOUR PULL"
      case "Design04"
        PrinterPath1 = printserver & "\mfdkmc253design"
      case else
        printerpath1 = printserver & "\mainprinter"
    end select
    
    if printerpath1<>"" then
      Wshnetwork.addwindowsprinterconnection Printerpath1
    end if
    if printerpath2<>"" then
      Wshnetwork.addwindowsprinterconnection Printerpath1
    end if
    
    I just find it easier to read multiple case statements than lots of if/elseif.
    The other thing I've done is move the actual addprinter bit to the end - it saves typing (making errors less likely) but also means that you can see more of the script on screen which just makes it easier to see what's going on.

    I've also put in a "case else" - this deals with any room that doesn't have a specific printer. You might want this to print to some central printer (library??) or you could even have a dummy printer set up (some software won't do print preview without a print driver; like this you can have a printer driver everywhere even if there's no physical printer available)

    I've left out the printer driver bit - I can't remember ever using it and looking at this MSDN page suggests that it's only needed for Windows 9x clients.

    Finally (and I think this has been mentioned before) if you name your machines based on the room they're in then you can just use that instead of having to set a registry key. This is easy if your room names are all the same format (H215, B319 etc) but less easy when the rooms are all different lengths! What you could do is have names like room7_01, room7_02, design04_01 etc. This code will then get the room:
    Code:
    set oNet=wscript.network
    sComputer=oNet.Computername
    iUnderscorePos=instr(sComputer,"_")
    if iUnderscorePos<>0 then
      sRoom=left(sComputer,iUnderscorePos-1)
    end if
    
    If the computer name doesn't have an underscore then the script won't get a room name and the printer will be set using the "case else" bit above.

    Hope this of use to someone - when you're writing scripts there are always different ways to do things; none is necessarily right or wrong but they might be better or worse for your particular setup!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. VBS Script
    By Lipjam in forum Windows
    Replies: 4
    Last Post: 02-12-2009, 07:54 PM
  2. Strange MSAccess printing problem with .vbs
    By RabbieBurns in forum Office Software
    Replies: 1
    Last Post: 31-01-2009, 07:36 PM
  3. Printing by location
    By Chuckster in forum Scripts
    Replies: 2
    Last Post: 04-09-2008, 11:50 PM
  4. Sub within a sub - VBS Script
    By FN-GM in forum Scripts
    Replies: 5
    Last Post: 18-05-2008, 06:30 PM
  5. Trace a VBS Script
    By chalkwellstu in forum Scripts
    Replies: 3
    Last Post: 25-01-2008, 11:34 AM

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