Link to home
Start Free TrialLog in
Avatar of ExchAzag
ExchAzag

asked on

Printer Script Help

I am trying to create a printer script to map two printers only if they're not present.  Below is what I have so far, but I am receiving a "boolmapped is currently: false" whenever I attempt to run the script.

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

Avatar of Member_2_3654191
Member_2_3654191
Flag of Germany image

Hi ExchAzag,

it's because of the "Wscript.quit" in this excerpt:  

                   boolMapped = True
                        WScript.Echo "Printers already mapped."
                  End If
                  >>>WScript.quit<<<
            Next
   
Your script will always exit here regardless of any values.

Hope this helps
Daniel
Addition:

Just remove that "wscript.quit" I mentioned above and it should work. Furthermore I recommend to put a "wscript.echo "Printer added successfully." after the following line:

If boolMapped = False Then multiPrinter.addwindowsprinterconnection strPrinter
 
Good Luck!
Avatar of ExchAzag
ExchAzag

ASKER

When I remove the wscript.quit you mentioned above the script loops.
The below is corrected code.
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
                  If UCase(oPrinters.Item(i)) = UCase(strPrinter) Then
                        boolMapped = True
                        WScript.Echo "Printer " & strPrinter & " already mapped."
                  End If
            Next
        If boolMapped = False Then
			wscript.echo "Mapping printer " & strPrinter
			multiPrinter.addwindowsprinterconnection strPrinter
		end if
 
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

The first printer in the array maps fine.  The script seems to be having a problem with the second.  I am getting an error that says Line 21, Char 25, The filename, directory name, or volume lable syntax is incorrect.
This would imply that there is either a problem with the path to the printer, or with the installation of the drivers.
Can you manually install the printer by browsing to it in Windows Explorer?  Does the printer path match that you have put into the script?
I was trying test this script on the server where the printers are actually shared.  The script runs runs without error from several workstations.  Is there a way to name the windows similar to the msgbox(text", 7, "window name")?
The script will not work on the server that has the shared printers installed locally, for the simple reason that they are installed locally!
I don't understand what you mean by naming the windows?
I would like to change the "Windows Script Host" to something else.
Purplepomegranite,

The last script you provided works well, but whenever there is one printer that isn't already mapped it seems to map all printers again.  I would like it to only map the printer that is missing.  Any way around this?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of purplepomegranite
purplepomegranite
Flag of United Kingdom of Great Britain and Northern Ireland 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