+ Post New Thread
Results 1 to 14 of 14
How do you do....it? Thread, Location Aware Printing (VBS Script) in Technical; I have been tasked to write some VB scripts for the kids taking the ICT functional skills exam in January ...
  1. #1
    Fatmas's Avatar
    Join Date
    Jul 2009
    Location
    UK
    Posts
    770
    Thank Post
    119
    Thanked 124 Times in 74 Posts
    Blog Entries
    2
    Rep Power
    107

    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
    Gerry's Avatar
    Join Date
    Jun 2007
    Location
    North Wales
    Posts
    431
    Thank Post
    60
    Thanked 38 Times in 35 Posts
    Rep Power
    25
    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 (20th January 2010), Fatmas (7th December 2009)

  4. #3
    Fatmas's Avatar
    Join Date
    Jul 2009
    Location
    UK
    Posts
    770
    Thank Post
    119
    Thanked 124 Times in 74 Posts
    Blog Entries
    2
    Rep Power
    107
    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

    Join Date
    Jul 2008
    Posts
    2
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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

    Join Date
    Nov 2009
    Posts
    79
    Thank Post
    3
    Thanked 4 Times in 4 Posts
    Rep Power
    11
    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

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,159
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    125
    @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!

  8. #7

    Join Date
    Jan 2011
    Posts
    4
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Ok, we need something similar

    Currently we have print servers in each site (4 sites total) s03,s05,s08,s02

    Computer's are named the following method as an example

    S03-w103pc01
    s03-wscipc03
    s08-w204pc03
    s02-c22pc01

    We need a method to add printers by computer name. Preferably a method to add multiple printers to multiple computers

    for example
    if I have computer name s03-w103, or s03-w104 or s03-w104 add
    Printer A
    Printer B
    Printer C
    Set default printer A

    Basically a script that runs at logon or startup that looks for the computer name and adds printer based on name. Preferablyatlogin so it runs every time consistently

    All printers are already installed on print servers.

    Domain is server 2003 R2

    Would also like this script to run in background so user's cannot "X' it out and get no printers

    Any help is much appreciated

  9. #8
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    5,007
    Thank Post
    124
    Thanked 286 Times in 263 Posts
    Rep Power
    109

  10. #9

    Join Date
    Jan 2011
    Posts
    4
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    I've looked at the wiki. Not sure how I would use as exampled. Seeing as we have multiple print servers? Would I be able to do the following?

    Case "s08-w103" and "s08-w104"

    WshNetwork.AddWindowsPrinterConnection "\s08-print01\s08-library"

    WshNetwork.AddWindowsPrinterConnection "\s08-print01\s08-room103"

    WshNetwork.SetDefaultPrinter "\s08-print01\s08-room103"

    Case "s03-wsci" and "s03-c22" and "s03-c23" and "s03-c25"

    WshNetwork.AddWindowsPrinterConnection "\s03-print01\s03-library"

    WshNetwork.AddWindowsPrinterConnection "\s03-print01\s03-room103"

    WshNetwork.SetDefaultPrinter "\s03-print01\s03-room103"

    ??
    Last edited by cgresq192; 19th January 2011 at 01:06 AM.

  11. #10
    Gerry's Avatar
    Join Date
    Jun 2007
    Location
    North Wales
    Posts
    431
    Thank Post
    60
    Thanked 38 Times in 35 Posts
    Rep Power
    25
    I don't see why not. We don't use location aware scripts, but our PC's are grouped in the AD, and group policy runs a specific printer script for that group.

  12. #11

    Join Date
    Jan 2011
    Posts
    4
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    What is the best way to accomplish your method? Can you post an example script and how it is assigned in group policy?

    Thanks for any help!

    Quote Originally Posted by Gerry View Post
    I don't see why not. We don't use location aware scripts, but our PC's are grouped in the AD, and group policy runs a specific printer script for that group.

  13. #12
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    5,007
    Thank Post
    124
    Thanked 286 Times in 263 Posts
    Rep Power
    109
    You need to put your computer accounts into OUs and use a logon script that assigns the printers according to the OU they are in or if you naming convention is good and relates to the location you can take the first part of the name and use that to identify the room/area in your case I am guessing:
    Code:
    Case "s08-w103" and "s08-w104"
    You could identify these by getting the name and taking the first 3 eg
    Code:
    s08
    or even more specific (if your naming convention works this way)
    Code:
    s08-w1
    And using case statements with this.
    The OU method would be my preferred approach though.

  14. #13

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,159
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    125
    If you go with the case statement, you need to get the syntax right - eg your example becomes:
    Code:
    Case "s08-w103", "s08-w104"
    
    WshNetwork.AddWindowsPrinterConnection "\\s08-print01\s08-library"
    
    WshNetwork.AddWindowsPrinterConnection "\\s08-print01\s08-room103"
    
    WshNetwork.SetDefaultPrinter "\\s08-print01\s08-room103"
    
    Case "s03-wsci" , "s03-c22" ,"s03-c23" , "s03-c25"
    
    WshNetwork.AddWindowsPrinterConnection "\\s03-print01\s03-library"
    
    WshNetwork.AddWindowsPrinterConnection "\\s03-print01\s03-room103"
    
    WshNetwork.SetDefaultPrinter "\\s03-print01\s03-room103"
    ie - it uses comma to separate the different machine names, not and

  15. #14

    Join Date
    Jan 2011
    Posts
    4
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Awesome! Thats helps a lot! Thanks Guys!



SHARE:
+ Post New Thread

Similar Threads

  1. VBS Script
    By Lipjam in forum Windows
    Replies: 4
    Last Post: 2nd December 2009, 08:54 PM
  2. [MS Office - 2003] Strange MSAccess printing problem with .vbs
    By RabbieBurns in forum Office Software
    Replies: 1
    Last Post: 31st January 2009, 08:36 PM
  3. Printing by location
    By Chuckster in forum Scripts
    Replies: 2
    Last Post: 5th September 2008, 12:50 AM
  4. Sub within a sub - VBS Script
    By FN-GM in forum Scripts
    Replies: 5
    Last Post: 18th May 2008, 07:30 PM
  5. Trace a VBS Script
    By chalkwellstu in forum Scripts
    Replies: 2
    Last Post: 25th January 2008, 11:53 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
  •