目的:
根据打印机配置文件,成批的添加打印机,例如一台电脑同时要安装一个本地的并口打印机,一个网络打印服务器(并非共享其它电脑的打印机,是那种可用一条网线接上交换机的打印机)。
实现步骤:
1、轮询目录下的“打印机X.txt”,根据其中的端口描述判断是否本地LPT端口,如果是LPT本地打印机就不理会,如果是打印服务器,会自动创建新的TCP/IP端口。
2、重启打印服务,使新创建的本地IP端口生效。
3、轮询目录下的“打印机X.txt”,根据其中的打印机名称、驱动位置、打印机型号、打印机端口添加打印服务器。
关键语句:
rundll32 printui.dll,PrintUIEntry /?
缺陷:
因为我这里用不到USB口的本地打印机,以及不使用共享打印机,所以没有添加判断这类打印机的语句。
★★★★★★★★★
下面是批处理的代码
echo off
cls
rem 以下是判断端口
set count=0
:addreginfo
set /a count=%count%+1
if exist 打印机%count%.txt (set filename=打印机%count%.txt) else (goto eof)
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr /I "打印机IP="') do set pIP=%%a
if /I "%pIP%"=="LPT1:" goto addreginfo
if /I "%pIP%"=="LPT2:" goto addreginfo
rem 以下是添加端口在注册表中的信息
set regpath1=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\IP_
set regpath2=HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports\IP_
:addreg1
@reg add "%regpath1%%pip%" /v "Protocol" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "Version" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "HostName" /d "" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "IPAddress" /d "%pip%" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "HWAddress" /d "" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "PortNumber" /d "9100" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "SNMP Community" /d "public" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "SNMP Enabled" /d "00000000" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "SNMP Index" /d "00000001" /f /t REG_DWORD >nul 2>nul
:addreg2
@reg add "%regpath2%%pip%" /v "Protocol" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "Version" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "HostName" /d "" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "IPAddress" /d "%pip%" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "HWAddress" /d "" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "PortNumber" /d "9100" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "SNMP Community" /d "public" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "SNMP Enabled" /d "00000000" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "SNMP Index" /d "00000001" /f /t REG_DWORD >nul 2>nul
goto addreginfo
:eof
rem 以下是重新启动打印服务,使新端口生效
:restartservice
cls
echo.
echo 请稍候,正在启用打印机端口!
echo.
net stop spooler >nul 2>nul
net start spooler >nul 2>nul
rem 以下是添加打印机
set count=0
:instprinter
set /a count=%count%+1
if exist 打印机%count%.txt (set filename=打印机%count%.txt) else (goto over)
cls
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机IP="') do set pIP=%%a
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机名称="') do set pname=%%a
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机型号="') do set pmodel=%%a
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机INF位置="') do set pinf=%%a
echo.
echo 请稍等,正在添加打印机%count%,相关信息如下:
echo.
echo IP :%pIP%
echo 名称 :%pname%
echo 型号 :%pmodel%
echo 驱动 :%pinf%
if /I "%pIP%"=="LPT1:" (goto ok)
if /I "%pIP%"=="LPT2:" (goto ok) else (set pip=IP_%pip%)
:ok
rundll32 printui.dll,PrintUIEntry /if /b "%pname%" /f "%pinf%" /r "%pIP%" /m "%pmodel%"
goto instprinter
:over
cls
echo.
echo 打印机添加完毕!
pause
exit
★★★★★★★★★
网络打印服务器配置文件“打印机1.txt”的内容:
打印机IP=44.44.44.44
打印机名称=柯尼卡 220 打印服务器
打印机型号=KONICA MINOLTA 220
打印机INF位置=D:\sysbak\打印机\220驱动\TSK.inf
★★★★★★★★★
本地打印机配置文件“打印机2.txt”的内容:
打印机IP=LPT1:
打印机名称=本地HP 3050
打印机型号=HP LaserJet 3050 Series PCL 5e
打印机INF位置=D:\sysbak\打印机\hp3055\Drivers\2K_XP_Vista\hppcp501.inf
Last edited by exzzz on 2008-12-15 at 14:46 ]
Purpose:
Batch - add printers according to the printer configuration file. For example, a computer needs to install a local parallel port printer and a network print server at the same time (not sharing the printer of other computers, but the printer that can be connected to the switch with a network cable).
Implementation steps:
1. Poll the "Printer X.txt" in the directory. According to the port description in it, judge whether it is a local LPT port. If it is a local LPT printer, ignore it. If it is a print server, automatically create a new TCP/IP port.
2. Restart the print service to make the newly created local IP port take effect.
3. Poll the "Printer X.txt" in the directory. According to the printer name, driver location, printer model and printer port, add the print server.
Key statement:
rundll32 printui.dll,PrintUIEntry /?
Defect:
Because I don't need local printers with USB ports and don't use shared printers, there are no statements added to judge such printers.
★★★★★★★★★
The following is the batch code
@echo off
cls
rem The following is to judge the port
set count=0
:addreginfo
set /a count=%count%+1
if exist 打印机%count%.txt (set filename=打印机%count%.txt) else (goto eof)
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr /I "打印机IP="') do set pIP=%%a
if /I "%pIP%"=="LPT1:" goto addreginfo
if /I "%pIP%"=="LPT2:" goto addreginfo
rem The following is to add the information in the registry about the port
set regpath1=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\IP_
set regpath2=HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports\IP_
:addreg1
@reg add "%regpath1%%pip%" /v "Protocol" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "Version" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "HostName" /d "" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "IPAddress" /d "%pip%" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "HWAddress" /d "" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "PortNumber" /d "9100" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "SNMP Community" /d "public" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "SNMP Enabled" /d "00000000" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "SNMP Index" /d "00000001" /f /t REG_DWORD >nul 2>nul
:addreg2
@reg add "%regpath2%%pip%" /v "Protocol" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "Version" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "HostName" /d "" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "IPAddress" /d "%pip%" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "HWAddress" /d "" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "PortNumber" /d "9100" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "SNMP Community" /d "public" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "SNMP Enabled" /d "00000000" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "SNMP Index" /d "00000001" /f /t REG_DWORD >nul 2>nul
goto addreginfo
:eof
rem The following is to restart the print service to make the new port take effect
:restartservice
cls
echo.
echo Please wait, enabling the printer port!
echo.
net stop spooler >nul 2>nul
net start spooler >nul 2>nul
rem The following is to add the printer
set count=0
:instprinter
set /a count=%count%+1
if exist 打印机%count%.txt (set filename=打印机%count%.txt) else (goto over)
cls
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机IP="') do set pIP=%%a
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机名称="') do set pname=%%a
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机型号="') do set pmodel=%%a
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机INF位置="') do set pinf=%%a
echo.
echo Please wait, adding printer %count%, the relevant information is as follows:
echo.
echo IP: %pIP%
echo Name: %pname%
echo Model: %pmodel%
echo Driver: %pinf%
if /I "%pIP%"=="LPT1:" (goto ok)
if /I "%pIP%"=="LPT2:" (goto ok) else (set pip=IP_%pip%)
:ok
rundll32 printui.dll,PrintUIEntry /if /b "%pname%" /f "%pinf%" /r "%pIP%" /m "%pmodel%"
goto instprinter
:over
cls
echo.
echo Printer addition is complete!
pause
exit
★★★★★★★★★
The content of the network print server configuration file "Printer 1.txt":
Printer IP=44.44.44.44
Printer Name=Konica 220 Print Server
Printer Model=KONICA MINOLTA 220
Printer INF Location=D:\sysbak\Printers\220 Drivers\TSK.inf
★★★★★★★★★
The content of the local printer configuration file "Printer 2.txt":
Printer IP=LPT1:
Printer Name=Local HP 3050
Printer Model=HP LaserJet 3050 Series PCL 5e
Printer INF Location=D:\sysbak\Printers\hp3055\Drivers\2K_XP_Vista\hppcp501.inf
Last edited by exzzz on 2008-12-15 at 14:46 ]