Re: Adding Missing printer if they not there
- From: "JimsMind" <james.mindrup@xxxxxxxxx>
- Date: 8 Dec 2006 05:45:17 -0800
poboy_n.o_style wrote:
I am trying to set this up so that if the printers are already there it would
skip it but it is not there that it would just add it. Some places have 5 or
more printers.
On error resume next
'Option Explicit
Dim objShell, objNetwork
Dim netPrinter, conprt, newprt1, newprt2, newprt3, newprt4
Dim oPrinter, AlreadyConnected, Network1, i
newprt1 = "\\servera\printerA"
newprt2 = "\\servera\printerB"
Set Network1 = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set oPrinter = objNetwork.EnumPrinterConnections()
AlreadyConnected = False
For i = 0 To oPrinter.Count - 1 Step 2
If oPrinter.Item(i) = conprt Then AlreadyConnected = True
Next
If AlreadyConnected = False then
netPrinter.AddWindowsPrinterConnection newprt1, True
netPrinter.AddWindowsPrinterConnection newprt2, True
objShell.PopUp "Printer " & conprt & " connected successfully."
Else
End if
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
.
- Follow-Ups:
- Re: Adding Missing printer if they not there
- From: poboy_n.o_style
- Re: Adding Missing printer if they not there
- Prev by Date: i need NTFS permission change script
- Next by Date: Re: map resources based on OU membership in a logon script
- Previous by thread: i need NTFS permission change script
- Next by thread: Re: Adding Missing printer if they not there
- Index(es):
Relevant Pages
|