Link to home
Start Free TrialLog in
Avatar of AzagThothe
AzagThothe

asked on

Printer Script. Query to find out if printer already exists?

I have been using the following code to query for an existing printer and if it already exists, but it seems to think the printers in question already exist when a workstation has an adobe pdf, xml, and or one note.

Thanks in advance for any help.
OPTION EXPLICIT
DIM multiPrinter, UNCpath1, UNCpath2, arrPrinters, oPrinters, strPrinter, boolMapped, i
 
arrPrinters = Array( _
        "\\Server1\Printer1", _
        "\\Server1\Printer2"_
        )
        
Set multiPrinter = CreateObject("wscript.network")
Set oPrinters = multiPrinter.EnumPrinterConnections
For Each strPrinter In arrPrinters 
        boolMapped = False
        For i = 0 to oPrinters.Count - 1 Step 2
                If UCase(oPrinters.Item(i+1)) = UCase(strPrinter) Then boolMapped = True
		WScript.Echo "Printers already mapped."
		WScript.quit
        Next
        If boolMapped = False Then multiPrinter.addwindowsprinterconnection strPrinter
 
Next
' Set the default printer to the first printer in the array
multiPrinter.setdefaultprinter arrPrinters(0)
 
WScript.Echo "Finished mapping printers."
 
wscript.quit

Open in new window

Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, above this line:
If UCase(oPrinters.Item(i+1)) = UCase(strPrinter) Then boolMapped = True

I would add this, just to see what it's comparing, and when it sets boolMapped to True:
WScript.Echo "Comparing " & UCase(oPrinters.Item(i+1)) & " to " & UCase(strPrinter) & ". BoolMapped is currently: " & boolMapped

Or, you might just be getting told the printer is already mapped because this line
WScript.Echo "Printers already mapped."
is not inside the If statement that compares the printer names.  Change this:

            For i = 0 to oPrinters.Count - 1 Step 2
                  WScript.Echo "Comparing " & UCase(oPrinters.Item(i+1)) & " to " & UCase(strPrinter) & ". BoolMapped is currently: " & boolMapped
                  If UCase(oPrinters.Item(i+1)) = UCase(strPrinter) Then boolMapped = True
                  WScript.Echo "Printers already mapped."
                  WScript.quit
            Next


to this:

            For i = 0 to oPrinters.Count - 1 Step 2
                  WScript.Echo "Comparing " & UCase(oPrinters.Item(i+1)) & " to " & UCase(strPrinter) & ". BoolMapped is currently: " & boolMapped
                  If UCase(oPrinters.Item(i+1)) = UCase(strPrinter) Then
                        boolMapped = True
                        WScript.Echo "Printers already mapped."
                  End If
                  WScript.quit
            Next


Regards,

Rob.
Oh, and you probably don't want the wscript.quit line there.....there's no need to quit, because you want to check all of the printers in the arrPrinters array.

Regards,

Rob.
Avatar of AzagThothe
AzagThothe

ASKER

Rob,

Thank you for the help so far.  I made the following changes to the script, but I'm receiving a script message saying, "Comparing \\Server1\Printer1 to \\Server1\Printer2.  BoolMapped is currently: False.

Also, I'm not sure which WScript.Quit you suggest that I remove.
OPTION EXPLICIT
DIM multiPrinter, UNCpath1, UNCpath2, arrPrinters, oPrinters, strPrinter, boolMapped, i
 
arrPrinters = Array( _
        "\\Server1\Printer1", _
        "\\Server1\Printer2"_
        )
        
Set multiPrinter = CreateObject("wscript.network")
Set oPrinters = multiPrinter.EnumPrinterConnections
For Each strPrinter In arrPrinters 
        boolMapped = False
        For i = 0 to oPrinters.Count - 1 Step 2
                  WScript.Echo "Comparing " & UCase(oPrinters.Item(i+1)) & " to " & UCase(strPrinter) & ". BoolMapped is currently: " & boolMapped
                  If UCase(oPrinters.Item(i+1)) = UCase(strPrinter) Then
                        boolMapped = True
                        WScript.Echo "Printers already mapped."
                  End If
                  WScript.quit
            Next
        If boolMapped = False Then multiPrinter.addwindowsprinterconnection strPrinter
 
Next
' Set the default printer to the first printer in the array
multiPrinter.setdefaultprinter arrPrinters(0)
 
WScript.Echo "Finished mapping printers."
 
wscript.quit

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial