Jump to content

Check if a printer's port exist - via Dos Command


fabfisc

Recommended Posts

Hi, i create a batch script which creates 3 ports, in my office lan "192.168.42.217" and 2 others, add printer drivers and creates 3 printers.

Now, if one of the three printers already exists, it creates anyway the printer name, with final " (1)" (without quotes).

I wish check if a port number already exists or a printer name exists and, using an IF command in a batch, bypass the line command which create the printer.

Can someone help me?

Thanks in advance

Link to comment
Share on other sites


First of all tanks for your reply.

Well, i'm trying with a vbs and im coming out dearly.

Because, as i said first, there are 3 printers, i wrote 3 command to:

1) check if a port already exists;

2) for each NOT existing port, add it by using:

Set objWMIService = GetObject("winmgmts:")

Set objNewPort = objWMIService.Get _

("Win32_TCPIPPrinterPort").SpawnInstance_

objNewPort.Name = "IP_192.168.42.xxx"

objNewPort.Protocol = 1

objNewPort.HostAddress = "192.168.42.xxx"

objNewPort.PortNumber = "9100"

objNewPort.SNMPEnabled = False

objNewPort.Put_

3) use the follow line to add and install each printer:

rundll32 printui.dll,PrintUIEntry /if /b "RICOH Aficio 2035 PCL 6 - P1" /f "%systemdrive%\drivers\printer\ricoh2035\oemsetup.inf" /r "IP_192.168.42.xxx" /m "RICOH Aficio 2035 PCL 6"

Now, ther's an arising problem. If the three printers are not yet installed on the pc, the vbs will do it, but too quickly so it can't add drivers for the second or the third printer 'cause it's busy installing for the first or for the first and second at the same time...

In other words, the code execute commands too nearest each other. Is there a method to wait some seconds between command lines? (like: wait 5 seconds? so it will be able to install completely the first, then the second and finally the third printer?)

Thanks againg.

P.S. if you want, i can post my code

Link to comment
Share on other sites

If possible use the Postscript drivers, I find I have had more problems installing PCL drivers then PS drivers.

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

set objNewPort = objWMIService.get("Win32_TCPIPPrinterPort").SpawnInstance_
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
Set objDriver = objWMIService.Get("Win32_PrinterDriver")
set objDictionary = CreateObject("Scripting.Dictionary")

'Installs Printer Port, who cares if it already exists,
'it will overwrite the existing one with this information
'Repeat as necessary for each additional port

objNewPort.Name = "ip_192.168.100.101" ' IP address or FQDN
objNewPort.Protocol = 1
objNewPort.HostAddress = "ip_192.168.100.101" ' IP address or FQDN
objNewPort.PortNumber = 9100
objNewPort.SNMPEnabled = True 'or False your choice
objNewPort.Put_


'Get the printer port for all installed printers, skip duplicates if the port is installed to more than one printer
set colInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer")

For Each instPrinter in colInstalledPrinters
if not objDictionary.exists(instPrinter.PortName) then
objDictionary.Add instPrinter.PortName, instPrinter.PortName
end if
Next

'get all installed printer ports and compare to installed printers
Set colPorts = objWMIService.ExecQuery ("Select * from Win32_TCPIPPrinterPort")
For Each objPort in colPorts
If not objDictionary.Exists(objPort.Name) Then
'If port is installed but printer is not associated with that port do this
msgbox "not found: " & objPort.Name
Select case ucase(objport.name)
case "IP_192.168.100.101"
'Example of a printer that the drivers exist by default in WIndows XP in windows\inf\ntprint.inf

'Laserjet 4000
'Installs Printer Driver
'Name of printer as displayed in the INF file for printer
objDriver.Name = "HP LaserJet 4000 Series PS"
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
intResult = objDriver.AddPrinterDriver(objDriver)

'Install Printer
'Name of printer as displayed in the INF file for printer
'This should match the objDriver string above
objPrinter.DriverName = "HP LaserJet 4000 Series PS"
objPrinter.PortName = "IP_192.168.100.101"
'Friendly display name
objPrinter.DeviceID = "Main Office LJ4000"
objPrinter.Location = ""
objPrinter.Network = True
objPrinter.Put_

case "IP_192.168.100.102"
'Laserjet 4200
'Installs Printer Driver
'Name of printer as displayed in the INF file for printer
objDriver.Name = "HP LaserJet 4200 PS"
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
'Path to the driver files
objDriver.FilePath = "c:\\DRIVERS\\lj4200ps\\"
'Path and Filename of the drive INF file
objDriver.Infname = "c:\\DRIVERS\\lj4200ps\\hpc4200d.inf"
intResult = objDriver.AddPrinterDriver(objDriver)

'Install Printer
'Name of printer as displayed in the INF file for printer
'This should match the objDriver string above
objPrinter.DriverName = "HP LaserJet 4200 PS"
objPrinter.PortName = "IP_192.168.100.102"
'friendly display name
objPrinter.DeviceID = "Back Office LJ4200"
objPrinter.Location = ""
objPrinter.Network = True
objPrinter.Put_

end select

Else
'If port is found and associated with a printer do this
msgbox "found: " & objPort.Name
End If
Next


'Set Front Office LJ4000 as default
Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer Where Name = 'Front Office LJ4000'")

For Each objPrinter in colInstalledPrinters
objPrinter.SetDefaultPrinter()
Next

Link to comment
Share on other sites

In DOS, off the top of my head I'd try:

@ECHO. > LPT1:

Then check errorlevel. But I don't remember of the ECHO command in DOS will return an errorlevel. You might have to use something like the type command (type autoexec.bat > LPT1:)

Link to comment
Share on other sites

Wouldn't using DEVCON be possible/easier?

http://www.robvanderwoude.com/index.html

http://www.robvanderwoude.com/devcon.html

http://www.robvanderwoude.com/files/getdevdj.txt

or using rundll32 PRINTUI.DLL?

http://www.robvanderwoude.com/2kprintcontrol.html

To find real devices you can use a "side effect" of the DIR command:

http://www.robvanderwoude.com/isdev.html

Open a command prompt and try:

DIR LPT1:

(a valid port)

and

DIR LPT3:

(a NON valid port)

and see the difference

jaclaz

Link to comment
Share on other sites

DEVCON doesn't return Network printers and ports, at least I've never had it do it, and the scripts on Robs page don't either.

the PrintUI.DLL works sometimes for installing printers, but I have had it be hit and miss. Youcan get info on the printer but then you have to parse a text file to see if the port is installed.

Link to comment
Share on other sites

I made this script.

Hope it helps someone like me who has the same problem.

' *************************************************************************************** '

' Nome routine: InstallRicohDriver

' ---------------------------------------------------------------------------------------

' Descrizione routine:

' La routine, esegue la creazione delle tre porte (192.168.42.203, 192.168.42.217

' e 192.168.42.244), dopo averne controllato l'eventuale esistenza; quindi procede

' con l'installazione dei driver e delle relative stampanti per i piani 1, 2 e 5.

'

' Piano 1 - IP 192.168.42.217

' Piano 2 - IP 192.168.42.244

' Piano 5 - IP 192.168.42.203

'

' - step1: controllo esistenza porte stampanti

' - step2: aggiunta porta ed installazione driver stampante

' - step3: impostazione stampante predefinita

' ---------------------------------------------------------------------------------------

' Created by Fiscella Fabrizio on 20/02/2007

' *************************************************************************************** '

'

' costanti contenenti i nomi delle stampanti

'

prnPiano1 = "RICOH Aficio 2035 PCL 6 - P1"

prnPiano2 = "RICOH Aficio 2035 PCL 6 - P2"

prnPiano5 = "RICOH Aficio 2035 PCL 6 - P5"

nomeStampante = "RICOH Aficio 2035 PCL 6"

'

' -------------------------------------------- '

' STEP 1 - controllo esistenza porte stampanti '

' -------------------------------------------- '

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colPorts = objWMIService.ExecQuery _

("Select * from Win32_TCPIPPrinterPort")

' Inizializzo le variabili che memorizzeranno l'eventuale esistenza di alcune porte

flg217trovata = 0

flg244trovata = 0

flg203trovata = 0

Wscript.Echo "."

Wscript.Echo " CONTROLLO ESISTENZA PORTE"

Wscript.Echo "."

' Ciclo per reperire quali porte siano già esistenti

For Each objPort in colPorts

if objPort.Name = "IP_192.168.42.217" then

Wscript.Echo "TROVATA PORTA: IP_192.168.42.217"

flg217trovata = 1

stampantiGiaEsistenti=stampantiGiaEsistenti+1

elseif objPort.Name = "IP_192.168.42.244" then

Wscript.Echo "TROVATA PORTA: IP_192.168.42.244"

flg244trovata = 1

stampantiGiaEsistenti=stampantiGiaEsistenti+1

elseif objPort.Name = "IP_192.168.42.203" then

Wscript.Echo "TROVATA PORTA: IP_192.168.42.203"

flg203trovata = 1

stampantiGiaEsistenti=stampantiGiaEsistenti+1

end if

Next

if stampantiGiaEsistenti = 3 then

' tutte le stampanti sono gia' installate

Wscript.Echo "TUTTE LE STAMPANTI SONO GIA' INSTALLATE",0,"123"

else

' --------------------------------------------------------- '

' STEP 2 - aggiunta porta ed installazione driver stampante '

' --------------------------------------------------------- '

Set objShell = CreateObject("Wscript.Shell")

if flg217trovata <> 1 then

' ------- '

' PIANO 1 '

' ------- '

AddPort "IP_192.168.42.217","192.168.42.217"

Wscript.Echo "."

Wscript.Echo " AGGIUNTA PORTA 192.168.42.217"

Wscript.Echo "."

' installo driver e stampante

stringa = "rundll32 printui.dll,PrintUIEntry /if /b " + """" + prnPiano1 + """" + " /f ""%systemdrive%\drivers\printer\ricoh2035\oemsetup.inf"" /r ""IP_192.168.42.217"" /m " + """" + nomeStampante + """"

objShell.Run stringa

Wscript.Echo "."

Wscript.Echo " AGGIUNTA STAMPANTE RICOH AFICIO PCL 6 - P1"

Wscript.Echo "."

end if

if flg244trovata <> 1 then

' ------- '

' PIANO 2 '

' ------- '

AddPort "IP_192.168.42.244","192.168.42.244"

Wscript.Echo "."

Wscript.Echo " AGGIUNTA PORTA 192.168.42.244"

Wscript.Echo "."

' prima di installare la stampante del piano 2, controllo che sia

' già stata installata quella del piano 1

chkPrnExist("1")

stringa = "rundll32 printui.dll,PrintUIEntry /if /b " + """" + prnPiano2 + """" + " /f ""%systemdrive%\drivers\printer\ricoh2035\oemsetup.inf"" /r ""IP_192.168.42.244"" /m " + """" + nomeStampante + """"

objShell.Run stringa

Wscript.Echo "."

Wscript.Echo " AGGIUNTA STAMPANTE RICOH AFICIO PCL 6 - P2"

Wscript.Echo "."

end if

if flg203trovata <> 1 then

' ------- '

' PIANO 5 '

' ------- '

AddPort "IP_192.168.42.203","192.168.42.203"

Wscript.Echo "."

Wscript.Echo " AGGIUNTA PORTA 192.168.42.203"

Wscript.Echo "."

' prima di installare la stampante del piano 2, controllo che sia

' già stata installata quella del piano 1

chkPrnExist("2")

stringa = "rundll32 printui.dll,PrintUIEntry /if /b " + """" + prnPiano5 + """" + " /f ""%systemdrive%\drivers\printer\ricoh2035\oemsetup.inf"" /r ""IP_192.168.42.203"" /m " + """" + nomeStampante + """"

objShell.Run stringa

' prima di installare la stampante del piano 5, controllo che sia

' già stata installata quella del piano 2

chkPrnExist("5")

Wscript.Echo "."

Wscript.Echo " AGGIUNTA STAMPANTE RICOH AFICIO PCL 6 - P5"

Wscript.Echo "."

end if

end if

' ------------------------------------------- '

' STEP 3 - impostazione stampante predefinita '

' ------------------------------------------- '

pianoSelezionato = inputbox ("Digitare il piano nel quale si trova la stampante da settare come predefinita","Piano di appartenenza","1, 2 o 5")

' flag per gestione uscita dal loop successivo

pianoOk = 0

'

' ciclo finchè non si setta la stampante predefinita oppure si clicca su ANNULLA

'

do while pianoOk = 0

if trim(pianoSelezionato) = "" then

'msgbox "Nessun piano selezionato." & vbcrlf & _

'"Non è stata impostata la stampante predefinita", 0, "Inserisci piano"

'exit do

else

if isNumeric(pianoSelezionato) then

select case pianoSelezionato

case 1

' prima di impostare la stampante predefinita,

' mi accerto che sia stata correttamente creata

chkPrnExist("1")

' imposto la stampante predefinita

SetDefaultPrinter (prnPiano1)

'msgbox "Stampante selezionata: piano" & pianoSelezionato, 0, "Inserisci piano"

pianoOk = 1

case 2

' prima di impostare la stampante predefinita,

' mi accerto che sia stata correttamente creata

chkPrnExist("2")

' imposto la stampante predefinita

SetDefaultPrinter (prnPiano2)

'msgbox "Stampante selezionata: piano" & pianoSelezionato, 0, "Inserisci piano"

pianoOk = 1

case 5

' prima di impostare la stampante predefinita,

' mi accerto che sia stata correttamente creata

chkPrnExist("5")

' imposto la stampante predefinita

SetDefaultPrinter (prnPiano5)

'msgbox "Stampante selezionata: piano" & pianoSelezionato, 0, "Inserisci piano"

pianoOk = 1

case else

msgbox "Piano non corretto" & vbcrlf & _

"Scegliere tra i seguenti piani: 1, 2 o 5", 0, "Inserisci piano"

pianoSelezionato = inputbox ("Digitare il piano nel quale si trova la stampante da settare come predefinita","Piano di appartenenza","1, 2 o 5")

pianoOk = 0

end select

else

msgbox "Valore non valido!" & vbcrlf & _

"Scegliere tra i seguenti piani: 1, 2 o 5", 0, "Inserisci piano"

pianoSelezionato = inputbox ("Digitare il piano nel quale si trova la stampante da settare come predefinita","Piano di appartenenza","1, 2 o 5")

pianoOk = 0

end if

end if

loop

function chkPrnExist(piano)

' ******************************************

' Funzione per controllo esistenza stampante

' ******************************************

'

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'

' ciclo fino a quando non ho trovato la stampante richiesta

'

prnTrovata = 0

do while prnTrovata = 0

' in ciclo reperisco la lista aggiornata delle stampanti presenti

Set colInstalledPrinters = objWMIService.ExecQuery _

("Select * from Win32_Printer")

For Each objPrinter in colInstalledPrinters

' in ciclo reperisco la lista aggiornata delle stampanti presenti

Set colInstalledPrinters = objWMIService.ExecQuery _

("Select * from Win32_Printer")

if IsLike(objPrinter.Name, "*ricoh*") = true then

if piano = 1 then

if IsLike(objPrinter.Name, "*P1*") = true then

prnTrovata = 1

end if

elseif piano = 2 then

if IsLike(objPrinter.Name, "*P2*") then

prnTrovata = 1

end if

elseif piano = 5 then

if IsLike(objPrinter.Name, "*P5*") then

prnTrovata = 1

end if

end if

end if

Next

loop

end function

Function IsLike(strText, match)

' *************************************************

' Funzione LIKE - per controllo esistenza stampante

' *************************************************

'

Dim i, str, spec, temp, token, nPos

' Turn strings to lower case

str = LCase(strText)

spec = LCase(match)

' Split the various components of the match string

aInput = split(spec, "*") ' "c*.*m" becomes Array("c", ".", "m")

' Walk the array of specification sub-components

i = 0

For Each token In aInput

' The first token plays an important role: the file name must begin

' with a substring identical to the token.

If i = 0 Then

temp = Left(str, Len(token))

' Don't match...

If temp <> token Then

IsLike = False

Exit Function

End If

' Removes the leading substring before next step

str = Right(str, Len(str) - Len(token))

Else

temp = str

' For each asterisk we come accross, we chack that what remains of

' the filename contains the next token of the match string.

nPos = Instr(1, temp, token)

' Don't match...

If nPos = 0 Then

IsLike = False

Exit Function

End If

' Removes the leading substring before next step

str = Right(str, Len(str) - nPos + 1)

End If

i = i + 1

Next

IsLike = True

End Function

function AddPort(nomePorta,indIp)

' -------------------------------------------------------------------------- '

' AGGIUNTA PORTA '

' ______________ '

' PARAMETRI: '

' nomePorta: nome della porta da creare (es.: "IP_192.168.42.217") '

' indIp : indirizzo della porta da creare (es.: "192.168.42.217") '

' '

' -------------------------------------------------------------------------- '

if nomePorta <>"" then

Set objWMIService = GetObject("winmgmts:")

Set objNewPort = objWMIService.Get _

("Win32_TCPIPPrinterPort").SpawnInstance_

objNewPort.Name = nomePorta

objNewPort.Protocol = 1

objNewPort.HostAddress = indIp

objNewPort.PortNumber = "9100"

objNewPort.SNMPEnabled = False

objNewPort.Put_

end if

end function

Function SetDefaultPrinter(prnDefault)

' *********************************************** '

' FUNZIONA PER IMPOSTARE LA STAMPANTE PREDEFINITA '

' *********************************************** '

'

Set WshNet = CreateObject("Wscript.Network")

WshNet.SetDefaultPrinter prnDefault

Set WshNet = Nothing

Wscript.Echo "."

Wscript.Echo " STAMPANTE PREDEFINITA: " + prnDefault

Wscript.Echo "."

end function

' *************************************************************************************** '

Link to comment
Share on other sites

First of all tanks for your reply.

Well, i'm trying with a vbs and im coming out dearly.

Because, as i said first, there are 3 printers, i wrote 3 command to:

1) check if a port already exists;

2) for each NOT existing port, add it by using:

Set objWMIService = GetObject("winmgmts:")

Set objNewPort = objWMIService.Get _

("Win32_TCPIPPrinterPort").SpawnInstance_

objNewPort.Name = "IP_192.168.42.xxx"

objNewPort.Protocol = 1

objNewPort.HostAddress = "192.168.42.xxx"

objNewPort.PortNumber = "9100"

objNewPort.SNMPEnabled = False

objNewPort.Put_

3) use the follow line to add and install each printer:

rundll32 printui.dll,PrintUIEntry /if /b "RICOH Aficio 2035 PCL 6 - P1" /f "%systemdrive%\drivers\printer\ricoh2035\oemsetup.inf" /r "IP_192.168.42.xxx" /m "RICOH Aficio 2035 PCL 6"

Now, ther's an arising problem. If the three printers are not yet installed on the pc, the vbs will do it, but too quickly so it can't add drivers for the second or the third printer 'cause it's busy installing for the first or for the first and second at the same time...

In other words, the code execute commands too nearest each other. Is there a method to wait some seconds between command lines? (like: wait 5 seconds? so it will be able to install completely the first, then the second and finally the third printer?)

Thanks againg.

P.S. if you want, i can post my code

I havn't read all the topic, but fabfisc you can make a "pause" with ping command like

ping 127.0.0.1

You can add more ping to "pause" more ...

It's not really beautiful, but works great for many batchs of mine ...

++

Link to comment
Share on other sites

Sonic, thanks but i don't understeand how can i use it.

My problem is that i have to find a method to make it wait until the first printer installation is finished.

Only after this process, the second installation must will start, and so on with the third installation (only after the second).

So, 'cause the creation of the port is not a problem (the three creations can run togheter, it will creates without any problem), i need a "install the first printer...DONE? well, run the second installation TILL the first is finished (or till i can see that the first printer exists, that it was created)...finished? ok can run...run the third installation TILL the second is finished (same procedure as the second)...finished? ok go on"

Ok? Hope i'm clear.

Thanks

Link to comment
Share on other sites

I cannot say anything about VBS, but if it was batch files I would make three of them and call them with a "START /WAIT" one after another, I am sure that something similar can be done as well, or even better make just one file that accepts as parameters IP of port and name of Printer and call it three times with "Start /WAIT" changing parameters at each call.

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...