Jump to content

Recommended Posts

Posted

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:

 

http://www.edugeek.net/forums/scripts/22475-printing-location.html

 

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

Posted

Here's a skeleton of the vbs script we use with Group Policy:

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.

  • Thanks 2
Posted

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

Posted

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;

 

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

 

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.

  • 1 month later...
Posted

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.

Posted

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

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:

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!

  • 1 year later...
Posted

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

Posted (edited)

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"

 

??

Edited by cgresq192
Posted
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.
Posted

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!

 

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.
Posted

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:

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

You could identify these by getting the name and taking the first 3 eg

s08

or even more specific (if your naming convention works this way)

s08-w1

And using case statements with this.

The OU method would be my preferred approach though.

Posted

If you go with the case statement, you need to get the syntax right - eg your example becomes:

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

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...