Jump to content

Was this script helpful?  

23 members have voted

  1. 1. Was this script helpful?



Recommended Posts

Posted

And no errors, nothing in the event log?

 

How about changing

      WshNetwork.AddWindowsPrinterConnection "\\"testserv"\ENGLISH_1"
     WshNetwork.SetDefaultPrinter "\\"testserv"\ENGLISH_2"

 

to

      WshNetwork.AddWindowsPrinterConnection "\\testserv\ENGLISH_1"
     WshNetwork.SetDefaultPrinter "\\testserv\ENGLISH_2"

Posted

Yay!! I got it working with this:

 

On Error Resume Next

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

Dim computerName
computerName = LCase(WshNetwork.ComputerName)

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

Select Case (Left(computerName, 5))
Case "eng01"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\english-1"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\english-2"
	WshNetwork.SetDefaultPrinter "\\testserv\english-1"
Case "eng02"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\english-1"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\english-2"
	WshNetwork.SetDefaultPrinter "\\testserv\english-2"
Case "mat01"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\maths-1"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\maths-2"
               WshNetwork.SetDefaultPrinter "\\testserv\maths-1"
Case "mat02"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\maths-1"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\maths-2"
	WshNetwork.SetDefaultPrinter "\\testserv\maths-2"
       Case Else
	' WScript.Echo "No default printers added"
End Select

 

 

Thnx to everyone who's helped and to Ric_ for writing this script:)

Now, to finish off, I'd like to be able to nuke existing connections when a user logs off as opposed to on, so would I use this script as is, or would I need to add anything to it?

 

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

Posted
Yeah, you'd need to start the script with:
On Error Resume Next

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

Posted
This is our printer script

 

 

One question i am wondering is that if anyone know how to rename the printer when it is added in the script, like you can make a drive mapping and rename the drive mapping to something that kids can understand.

 

Tom

 

Thats what I was hoping could be done as well?

 

Does anyone know how to do this?

Posted

As far as I know you can, I've got this running in my script but I have to admit not sure it works 100%. Will have a look in the morning and post it if I have it. Maybe it will help.

 

Just had a look and this is what I have in my script to name the printers. As I said it works for some of the printers but not everyone for some reason. Not too bothered as everyone has not got used to the names that appear and changing them now would only confuse them.

 

'// Map Printers

network.AddWindowsPrinterConnection printer1 , "Black and White Printer"

network.AddWindowsPrinterConnection printer2 , "Colour Printer"

network.AddWindowsPrinterConnection printer3 , "Colour Photocopier"

  • 2 weeks later...
Posted

I will post my script soon as its years of tweaking and contains code to help log errors and also provide some user feedback as well.

Just trying to sanitise it for the real world ;)

Posted
As far as I know you can, I've got this running in my script but I have to admit not sure it works 100%. Will have a look in the morning and post it if I have it. Maybe it will help.

 

Just had a look and this is what I have in my script to name the printers. As I said it works for some of the printers but not everyone for some reason. Not too bothered as everyone has not got used to the names that appear and changing them now would only confuse them.

 

'// Map Printers

network.AddWindowsPrinterConnection printer1 , "Black and White Printer"

network.AddWindowsPrinterConnection printer2 , "Colour Printer"

network.AddWindowsPrinterConnection printer3 , "Colour Photocopier"

 

That didn't work :-(

 

Thanks.

Posted

Here is a copy of the script we use here.

I use function defined in the top to do the work and we use the built in MS agent to provide feedback to the users. It should be able to figure it all out anyway.

The function LogtoEvent is used to log things happening in the script to the event log.

It has 3 states although you wont use them all. I have found this to be very useful for diagnosing dodgy printers/drivers that cause the script to fail.

Its still not really noob friendly but anyone with a little VBS experience should understand it.

Look out for {change} areas in the script also.

 

Good luck ;)

printersv2.82.vbs

Posted

sorry to dig up a slightly old thread, but i have just managed to get this to work.

 

It wasnt working before because of the LCase, changed this and it work on some rooms but not other becase they are lowercase, is there a way to incorperate both, withotut having to rename the PC's?

 

Cheers

 

Mike

Posted
@linkazoid: The LCase just changes the name to lowercase so that you can inspect the string without worrying about case - changing to uppercase would have done the same job. Does that make sense?
Posted
Just a case of using the split function instead using left or right functions makes it easier if there is a common demoninator in the string ie a - symbol to seperate room name from machine number or w/e
  • 4 months later...
Posted
Yay!! I got it working with this:

 

On Error Resume Next

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

Dim computerName
computerName = LCase(WshNetwork.ComputerName)

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

Select Case (Left(computerName, 5))
Case "eng01"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\english-1"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\english-2"
	WshNetwork.SetDefaultPrinter "\\testserv\english-1"
Case "eng02"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\english-1"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\english-2"
	WshNetwork.SetDefaultPrinter "\\testserv\english-2"
Case "mat01"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\maths-1"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\maths-2"
               WshNetwork.SetDefaultPrinter "\\testserv\maths-1"
Case "mat02"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\maths-1"
	WshNetwork.AddWindowsPrinterConnection "\\testserv\maths-2"
	WshNetwork.SetDefaultPrinter "\\testserv\maths-2"
       Case Else
	' WScript.Echo "No default printers added"
End Select

 

 

Thnx to everyone who's helped and to Ric_ for writing this script:)

Now, to finish off, I'd like to be able to nuke existing connections when a user logs off as opposed to on, so would I use this script as is, or would I need to add anything to it?

 

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

 

 


 

 

Hi

 

I have used The script above but it doesn't work, any ideas?

 

This is my script

 

On Error Resume Next

 

Set WshShell = WScript.CreateObject("WScript.Shell")

Set WshNetwork = WScript.CreateObject("WScript.Network")

Set oPrinters = WshNetwork.EnumPrinterConnections

 

Dim computerName

computerName = LCase(WshNetwork.ComputerName)

 

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

 

Select Case (Left(computerName, 5))

Case "HC-IT1-"

WshNetwork.AddWindowsPrinterConnection "\\HC-Print\Printer2"

WshNetwork.AddWindowsPrinterConnection "\\HC-Print\Printer2"

WshNetwork.SetDefaultPrinter "\\HC-Print\Printer2"

' WScript.Echo "No default printers added"

End Select

 

This bit i am lost with this:

Select Case (Left(computerName, 5))

Case "HC-IT1-"

Is this the computer name?

 

Please help.

 

Z

Posted

Yes, the first line is selecting the first five characters of the computer name, from the left. (eg if computername is HC-IT1-001, then HC-IT is used). You may want to adjust the 5 to 7 based on the script you provided.

 

If your computer naming scheme follows a similar pattern, then you should be ok.

Posted

So, this is the part of your script that assigns the printers:

 

Select Case (Left(computerName, 7))

Case "hc-it1-"

WshNetwork.AddWindowsPrinterConnection "\\HC-Print\Printer2"

WshNetwork.AddWindowsPrinterConnection "\\HC-Print\Printer2"

WshNetwork.SetDefaultPrinter "\\HC-Print\Printer2"

' WScript.Echo "No default printers added"

End Select

 

Lines 3 and 4 seem to be duplicated. You can just change the UNC of the printer share on line 4. Then duplicate and modify the UNC as necessary for additional printers.

 

For example, the following would assign printer1 and printer2 (default) on computer names beginning with hc-it1-. Printer3 (default) and printer4 will be assigned to computer names beginning with hc-it2-:

 

Select Case (Left(computerName, 7))
Case "hc-it1-"
WshNetwork.AddWindowsPrinterConnection "\\HC-Print\Printer2"
WshNetwork.AddWindowsPrinterConnection "\\HC-Print\Printer1"
WshNetwork.SetDefaultPrinter "\\HC-Print\Printer2"

Case "hc-it2-"
WshNetwork.AddWindowsPrinterConnection "\\HC-Print\Printer3"
WshNetwork.AddWindowsPrinterConnection "\\HC-Print\Printer4"
WshNetwork.SetDefaultPrinter "\\HC-Print\Printer3"

Case Else
' WScript.Echo "No default printers added"
End Select

Posted

Sorry to double post, but just in case meastaugh1 already read my last post, so i didn't edit.

 

Anyway can this be added to a start-up script, I ask this because I want to introduce this on certain computers only for now. I only want it to work on one ICT room not all the computers in the school. If not how do I adapt it so it will work with a start-up script please?

 

Thanks

 

Z

Posted

Hmm, network printer mappings are per-user connections. To deploy to a testbed of computers, I suggest you stick it in a GPO in loopback merge mode and deploy it as a login script.

 

Having said that, you can add a network printer mapping on a computer for all users. I'm sure you'd only need to run it once on each computer, and wouldn't need to be run as a startup script.

 

http://maisonbisson.com/blog/post/11106

Posted

Ok I will try loopback when I get back :D

 

So if i add the following to a .bat script and set it as a start-up script it should work?

 

rundll32 printui.dll,PrintUIEntry /ga /n\\printers\%printername%

 

Thank you for all your help

 

Z

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