I have a need to check if the local printer HP LaserJet 4 is already
installed on the local machine and quit if it is. If not the printer should
be installed on the local machine. I am fairly new to scripting and got this
example from another posting from Torgeir and have been trying to modify it
to work for me. I seem to be having a syntax issue in the WshShell.run
statement, but I am unsure how to correct the issue.

Set oPrnSet = GetObject( _
"winmgmts:{impersonationLevel=impersonate}!//localhost") _
.ExecQuery("select Name from Win32_Printer")
Set WshShell = WScript.CreateObject ("WScript.Shell")

For Each oPrn in oPrnSet
If Lcase(oPrn.Name) = LCase("HP LaserJet 4") Then
Wscript.Quit
Else
WshShell.run rundll32 printui.dll,PrintUIEntry /q /if /b "HP LaserJet 4" /f
%windir%\inf\ntprint.inf /r "lpt2:" /m "HP LaserJet 4"
End If
Next

If anyone could provide an explanation of what I'm doing wrong it will be
appreciated!!!

Joel

Re: Add a local printer by Torgeir

Torgeir
Thu Oct 07 05:15:58 CDT 2004

Joel wrote:

> I have a need to check if the local printer HP LaserJet 4 is already
> installed on the local machine and quit if it is. If not the printer should
> be installed on the local machine. I am fairly new to scripting and got this
> example from another posting from Torgeir and have been trying to modify it
> to work for me. I seem to be having a syntax issue in the WshShell.run
> statement, but I am unsure how to correct the issue.
>
> Set oPrnSet = GetObject( _
> "winmgmts:{impersonationLevel=impersonate}!//localhost") _
> .ExecQuery("select Name from Win32_Printer")
> Set WshShell = WScript.CreateObject ("WScript.Shell")
>
> For Each oPrn in oPrnSet
> If Lcase(oPrn.Name) = LCase("HP LaserJet 4") Then
> Wscript.Quit
> Else
> WshShell.run rundll32 printui.dll,PrintUIEntry /q /if /b "HP LaserJet 4" /f
> %windir%\inf\ntprint.inf /r "lpt2:" /m "HP LaserJet 4"
> End If
> Next
>
> If anyone could provide an explanation of what I'm doing wrong it will be
> appreciated!!!
Hi

You need to supply a string to the Run method, so you need to put quotes
around your "run" command, like this:

sCmd = "rundll32 printui.dll,PrintUIEntry /q /if /b ""HP LaserJet 4""" _
& " /f %windir%\inf\ntprint.inf /r ""lpt2:"" /m ""HP LaserJet 4"""

MsgBox sCmd

WshShell.run sCmd



Note that inside a quoted string, you will need to use two quotes to get
one effective...


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx