目的:
根据打印机配置文件,成批的添加打印机,例如一台电脑同时要安装一个本地的并口打印机,一个网络打印服务器(并非共享其它电脑的打印机,是那种可用一条网线接上交换机的打印机)。
实现步骤:
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