Re: Adding Missing printer if they not there



On line 38 you are calling a subroutine named 'ErrHandler()' and
passing it the argument '826'. But if this is your entire script, you
have no subroutine name 'ErrHandler'. I made one for my script which
writes the errors to a log file I can access at my leisure, clears the
error, then continues on with the script. Take this line out and you
should run all the way through, except if you get an error (which seems
likely if you have many clients and you are accessing system info and
connecting to network printers). You will probably need to find a way
to handle your errors without throwing them in front of your end users,
unless all your users are computer savy (I have found the unsavy user
skips the analyze and concern stages and goes straight to the panic
stage when any error is seen!)

James

poboy_n.o_style wrote:
I edited the script but it keeps erroring on line 38, any reason why
Const T = 1
Const F = 0

Dim Printer(2,3)
'DIM Printer(5,3) would be for 5 printers.
'Printer() Dimensions:0= TF(Attach); 1= UNC Path; 2= TF(Attached)
Printer(0,1) = "\\server\printera"
Printer(0,0) = T
Printer(0,2) = F
Printer(1,1) = "\\server\printerB"
Printer(1,0) = T
Printer(1,2) = F
Printer(2,1) = "\\server\printerC"
Printer(2,0) = T
Printer(2,2) = F

'Bind to the WMI service on the local machine to collect information
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPrinters = objWMIService.ExecQuery ("Select * From Win32_Printer")

For Each objPrinter in colPrinters
CurrPrint = Replace(objPrinter.Name, " ", "")
If Left(CurrPrint,2) = "\\" Then
For CheckPrint = 0 to Ubound(Printer)
If Printer(CheckPrint,1) = CurrPrint Then
Printer(CheckPrint,2) = 1
Exit For
End If
Next
End If
Next
For ConPrint = 0 To Ubound(Printer)
If Err.Number Then ErrHandler(826)
If Printer(ConPrint,0) = 1 Then
If Printer(ConPrint,2) = 0 Then
objNetwork.AddWindowsPrinterConnection Printer(ConPrint,1)
If LogStatus = T Then LogEntry("Connecting Printer " &
Printer(ConPrint,1))
Else
If LogStatus = T Then LogEntry(Printer(ConPrint,1) & "
already connected")
End If
End If
Next


I used an Array to do this very thing. I know, it is too dirty, but it
works so I am just leaving it for now! I believe I got the basic logic
from the ms scripting guys. I use this script to assign all network
resources so I may do some things you don't need.

Const T = 1
Const F = 0

Dim Printer(2,3)
'Printer() Dimensions:0= TF(Attach); 1= UNC Path; 2= TF(Attached)
Printer(0,1) = "\\server\Printera"
Printer(0,0) = T
Printer(0,2) = F
Printer(1,1) = "\\server\PrinterB"
Printer(1,0) = T
Printer(1,2) = F
Printer(2,1) = "\\server\PrinterC"
Printer(2,0) = T
Printer(2,2) = F

So here I made the array, the first and last dimension of each record
are simple true or false using a 0 or 1 (I like to use the T or F for
better readability). That way during my data collection and analasys I
can simpy flip the switches on or off as needed. The 0 location stores
the true or false that I look at later to know if I should attach it at
all. The 2 location stores the true or false that i look at later to
see if the printer is already connected.

'Bind to the WMI service on the local machine to collect information
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")

Set colPrinters = objWMIService.ExecQuery ("Select * From
Win32_Printer")

For Each objPrinter in colPrinters
CurrPrint = Replace(objPrinter.Name, " ", "")
If Left(CurrPrint,2) = "\\" Then
For CheckPrint = 0 to Ubound(Printer)
If Printer(CheckPrint,1) = CurrPrint Then
Printer(CheckPrint,2) = 1
Exit For
End If
Next
End If
Next

Here, we checked the machine and found all printers installed, then we
checked only the network printers against our list to see if any of our
network printers are already present, and if they are we set our
connected switch to true.

For ConPrint = 0 To Ubound(Printer)
If Err.Number Then ErrHandler(826)
If Printer(ConPrint,0) = 1 Then
If Printer(ConPrint,2) = 0 Then
objNetwork.AddWindowsPrinterConnection
Printer(ConPrint,1)
If LogStatus = T Then LogEntry("Connecting Printer " _
& Printer(ConPrint,1))
Else
If LogStatus = T Then LogEntry(Printer(ConPrint,1) _
& " already connected")
End If
End If
End If
Next

Finally, at the end of my script, I simply check the true/false lines
and add accordingly.

Hope This helps

James
Just some guy





.