Jump to content

problems with my printer Script, can you cast your eye over it?


Recommended Posts

Posted

I keep get intermittent problems with the script we have been running for a longtime. It's normally flawless but now for some reasons I get errors.

 

* Sometimes the correct printers are shown and loaded sometimes they are not.

*pretty much everyone gets an erroneous printer which does not exist and is not in the script, from one of our old servers, can not find anywhere where it is running.

 

Keep getting script errors:

 

Script: xxxxxxxx

Line: 10

Char: 5

Error: Generic

Code: 80041001

Source: SWBemOBJECTEx

 

Script is below, Thanks :

 


'This Deletes all mapped printers for the user

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters = objWMIService.ExecQuery _
   ("Select * From Win32_Printer Where Network = True")

For Each objPrinter in colInstalledPrinters
   objPrinter.Delete_
Next

'***********************************************
'This checks the name of the machine and applies the printer path

Dim oNetwork, sPrintPath, sShort
Set oNetwork = CreateObject("WScript.Network")
sShort = Left (oNetwork.ComputerName, 4)

Select Case sShort
Case "MUS2"
	sPrintpath = "\\140_T_MUSIC\MUSICPRINT"
Case "TEX_"
	sPrintpath = "\\support\textiles"
Case "SEN0"
	sPrintPath = "\\support\116"
Case "117A"
	sPrintPath = "\\support\Laserjet 117B"
Case "117B"
	sPrintPath = "\\support\Laserjet 117C"
Case "STR_"
	sPrintPath = "\\support\Staffroom"
Case "129_"
	sPrintPath = "\\support\Brother HL-1440 -129-"
Case "204A"
	sPrintPath = "\\support\LaserJet -204A-"
Case "204B"
	sPrintPath = "\\support\Laserjet -204B-"
Case "204C"
	sPrintPath = "\\support\LaserJet -204C-"
Case "64_A"
	sPrintPath = "\\support\Laserjet -64A-"
Case "64_B"
	sPrintPath = "\\support\Laserjet -64B-"
Case "65_A"
	sPrintPath = "\\support\Brother65"
Case "KB4A"
	sPrintPath = "\\support\K4A"
Case "KB4B"
	sPrintPath = "\\support\K4B"
Case "LLRC"
	sPrintPath = "\\support\LLRC"
Case "ART2"
	sPrintPath = "\\support\ART1"
Case "M1_T"
	sPrintPath = "\\support\MFL"
Case "M2_T"
	sPrintPath = "\\support\MFL"
Case "M3_T"
	sPrintPath = "\\support\MFL"
Case "ENG_"
	sPrintPath = "\\support\English"
Case "SCLT"
	sPrintPath = "\\support\Science"
Case "FTEC"
	sPrintPath = "\\support\FoodTech"
Case "ENG2"
	sPrintPath = "\\support\English_2"
	
Case Else
	WScript.Quit
End Select
oNetwork.AddWindowsPrinterConnection sPrintPath
oNetwork.SetDefaultPrinter sPrintPath

'**************************************************************
'This applies secondary printers(colour) to the user

Select Case sShort
Case "64_A"
	sPrintPath = "\\support\A3_64Colour"
Case "64_B"
	sPrintPath = "\\support\A3_64Colour"
Case "65_A"
	sPrintPath = "\\support\HPcolour - 65"
Case "204A"
	sPrintPath = "\\support\204colour"
Case "204B"
	sPrintPath = "\\support\204colour"
Case "204C"
	sPrintPath = "\\support\204colour"
Case "117A"
	sPrintPath = "\\support\117_colour"
Case "117B"
	sPrintPath = "\\support\117_colour"
Case "SCLT"
	sPrintPath = "\\support\ScienceColour"
Case "KB4A"
	sPrintPath = "\\support\K4_COLOUR"
Case "KB4B"
	sPrintPath = "\\support\K4_COLOUR"
	
Case Else
	WScript.Quit
End Select
oNetwork.AddWindowsPrinterConnection sPrintPath

'**************************************************************
'This applies a third printer to the user

select Case sShort
Case "64_A"
	sPrintPath = "\\support\A4_64Colour"
Case "64_B"
	sPrintPath = "\\support\A4_64Colour"
Case "65_A"
	sPrintPath = "\\support\65_A4_Colour"

Case Else
	WScript.Quit
End Select
oNetwork.AddwindowsPrinterConnection sPrintPath



Posted
The underscore is telling the interpreter to append the following line as if it were a continuation, isn't it?

 

No. This is one of the wacky bits of method names in chunks of the Windows API; they end with an underscore just to make life difficult :-)

 

Is the error reproducible (ie always for 1 user, 1 machine etc??) If so, can you try putting in an "on error resume next" at the beginning of the code and then this:

For Each objPrinter in colInstalledPrinters
   objPrinter.Delete_
   if err.number<>0 then
     wscript.echo objPrinter.name
  end if
Next

that will at least tell you which printer is not deleting.

 

The 0x80040001 error seems to be pretty non-specific ("something's gone wrong but I don't know what!") but there's some stuff here Code 80041001 - VBScript Error called failed which may help; particularly about clearing out the WBEM repository.

Posted

' killprinters.vbs 

Option Explicit
Dim objNetwork
on error resume next

Set objNetwork = CreateObject("WScript.Network") 

objNetwork.RemovePrinterConnection "\\server\lasercol", true, true 
objNetwork.RemovePrinterConnection "\\server\colourlt", true, true 

 

not sure if this helps at all but thats my printer removal script

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