[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[系统相关] [分享]BAT连接TCP/IP网络打印机

本帖最后由 狄钦dQ 于 2018-7-8 09:14 编辑

偶然在论坛里看到一篇vbs实现打印机连接的帖子:http://www.bathome.net/thread-29876-1-1.html,便想着用BAT实现下,然而在论坛内没有找到相关帖子,网络上使用BAT实现的多是连接共享打印机,而非支持TCP/IP连接的网络打印机,VBS/Powershell实现比较常见,于是花了点时间琢磨,最后做了下BAT的实现,给大家分享下,有什么意见想法可以共同探讨(大佬轻喷)~
特点实现:自动检测网络环境,获取打印驱动,连接指定IP的打印机,重命名打印机并将其设置为默认。
参数配置:
[PrinterName]:打印机的显示名称
[IP_Port]:网络打印机的IP地址
[DriverDir]:原打印机驱动所在目录
[DriverInf]:本地.inf的文件路径。
    为了保证驱动安装速度,首先将驱动文件从某服务器或共享文件夹上复制到本地%temp%下的PrintDrv临时目录。
[DriverName]:驱动程序名称
    需要根据系统类型,选择合适的打印机程序x86,x64等。在.inf文件或"打印机属性-高级"选项卡处可查。
  1. @echo off
  2. REM 说明:连接TCP/IP网络打印机,需要管理员权限下载所需驱动。
  3. set PrinterName="printer_xxxx"
  4. set IP_Port="10.x.x.x"
  5. set DriverDir="\\10.x.x.x\xxxx"
  6. set DriverInf="%tmp%\PrintDrv\xxx.inf"
  7. set DriverName="xxxx pcl/ps"
  8. REM =====注意:请根据实际环境修改以上参数,其他无需修改=========
  9. call :MakeReg
  10. echo 正在连接打印机%PrinterName%,请不要关闭此窗口...
  11. md %tmp%\PrintDrv>nul 2>nul
  12. xcopy %DriverDir% %tmp%\PrintDrv /s /y>nul
  13. ping -n 1 %IP_Port% | find "TTL">nul
  14. if %errorlevel% NEQ 0 goto :Failure
  15. wmic path win32_printer get name | find %PrinterName%>nul
  16. if %errorlevel%==0 (
  17. rundll32 printui.dll,PrintUIEntry /dl /q /n %PrinterName%
  18. )
  19. regedit /s %tmp%\printer.reg
  20. net stop spooler & net start spooler
  21. rundll32 printui.dll,PrintUIEntry /if /b %PrinterName% /f %DriverInf% /r %IP_Port% /m %DriverName% /z
  22. wmic path win32_printer get name | find %PrinterName%>nul
  23. if %errorlevel%==0 (
  24. rundll32 printui.dll,PrintUIEntry /y /n %PrinterName%
  25. msg * "打印机连接成功!"
  26. ) else ( goto :Failure )
  27. del /q %tmp%\printer.reg>nul
  28. del /q /s %tmp%\PrintDrv>nul
  29. exit
  30. :MakeReg
  31. >%tmp%\printer.reg echo Windows Registry Editor Version 5.00
  32. >>%tmp%\printer.reg echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports]
  33. >>%tmp%\printer.reg echo "StatusUpdateInterval"=dword:0000000a
  34. >>%tmp%\printer.reg echo "StatusUpdateEnabled"=dword:00000001
  35. >>%tmp%\printer.reg echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\%IP_Port:~1,-1%]
  36. >>%tmp%\printer.reg echo "Protocol"=dword:00000001
  37. >>%tmp%\printer.reg echo "Version"=dword:00000002
  38. >>%tmp%\printer.reg echo "HostName"=%IP_Port%
  39. >>%tmp%\printer.reg echo "IPAddress"=""
  40. >>%tmp%\printer.reg echo "HWAddress"=""
  41. >>%tmp%\printer.reg echo "PortNumber"=dword:0000238c
  42. >>%tmp%\printer.reg echo "SNMP Community"="public"
  43. >>%tmp%\printer.reg echo "SNMP Enabled"=dword:00000000
  44. >>%tmp%\printer.reg echo "SNMP Index"=dword:00000001
  45. >>%tmp%\printer.reg echo "PortMonMibPortIndex"=dword:00000000
  46. goto :eof
  47. :Failure
  48. msg * "连接失败,请检查网络连接或打印机状态!"
  49. goto :eof
复制代码
如果有做得不好的,欢迎大家指点~相比VBS/PS,BAT代码比较少,但能适用于常见的办公环境。当然,如果是在防火墙禁ping、系统版本多样等环境的话,还是有点欠缺,具体解决方法大家也可以一起讨论下~
1

评分人数

返回列表