如何用脚本快速添加网络打印机

wen 实用脚本 2

本文目录导读:

如何用脚本快速添加网络打印机

  1. 方法1:PowerShell脚本(Windows)
  2. 方法2:批处理脚本(Windows)
  3. 方法3:Python脚本(跨平台)
  4. 方法4:快速批量添加脚本
  5. 使用说明

方法1:PowerShell脚本(Windows)

# 添加网络打印机 - 使用TCP/IP端口
$printerIP = "192.168.1.100"
$printerName = "HP_LaserJet_Office"
$driverName = "HP LaserJet 4000 Series PCL6"
$portName = "IP_$printerIP"
# 创建TCP/IP端口
Add-PrinterPort -Name $portName -PrinterHostAddress $printerIP
# 安装打印机驱动(需要预先下载)
Add-PrinterDriver -Name $driverName
# 添加打印机
Add-Printer -Name $printerName -DriverName $driverName -PortName $portName
# 设置为默认打印机
Set-Printer -Name $printerName -Default $true

方法2:批处理脚本(Windows)

@echo off
REM 添加网络打印机
REM 使用rundll32添加打印机
REM 添加网络打印机到LPT端口
rundll32 printui.dll,PrintUIEntry /in /n "\\print-server\PrinterName"
REM 添加TCP/IP打印机
cscript %WINDIR%\System32\Printing_Admin_Scripts\zh-CN\prnport.vbs -a -r IP_192.168.1.100 -h 192.168.1.100 -o raw -n 9100
cscript %WINDIR%\System32\Printing_Admin_Scripts\zh-CN\prnmngr.vbs -a -p "PrinterName" -m "HP LaserJet 4000" -r "IP_192.168.1.100"

方法3:Python脚本(跨平台)

import subprocess
import sys
def add_windows_printer(printer_ip, printer_name, driver_name):
    """添加Windows网络打印机"""
    port_name = f"IP_{printer_ip}"
    # 创建端口
    commands = [
        f'powershell Add-PrinterPort -Name "{port_name}" -PrinterHostAddress "{printer_ip}"',
        f'powershell Add-PrinterDriver -Name "{driver_name}"',
        f'powershell Add-Printer -Name "{printer_name}" -DriverName "{driver_name}" -PortName "{port_name}"'
    ]
    for cmd in commands:
        subprocess.run(cmd, shell=True)
def add_linux_printer(printer_ip, printer_name):
    """添加Linux网络打印机"""
    commands = [
        f'lpadmin -p {printer_name} -E -v socket://{printer_ip}:9100 -m everywhere',
        f'cupsenable {printer_name}',
        f'cupsaccept {printer_name}'
    ]
    for cmd in commands:
        subprocess.run(cmd, shell=True)

方法4:快速批量添加脚本

# 批量添加多个网络打印机
$printers = @(
    @{IP="192.168.1.101"; Name="HR_Printer"; Driver="HP Universal Printing PCL 6"},
    @{IP="192.168.1.102"; Name="Finance_Printer"; Driver="Canon LBP6230dw"},
    @{IP="192.168.1.103"; Name="Marketing_Printer"; Driver="Brother HL-L2360D"}
)
foreach ($printer in $printers) {
    $portName = "IP_$($printer.IP)"
    try {
        # 检查端口是否已存在
        if (-not (Get-PrinterPort -Name $portName -ErrorAction SilentlyContinue)) {
            Add-PrinterPort -Name $portName -PrinterHostAddress $printer.IP
            Write-Host "端口 $portName 已创建" -ForegroundColor Green
        }
        # 检查打印机是否已存在
        if (-not (Get-Printer -Name $printer.Name -ErrorAction SilentlyContinue)) {
            Add-Printer -Name $printer.Name -DriverName $printer.Driver -PortName $portName
            Write-Host "打印机 '$($printer.Name)' 已添加" -ForegroundColor Green
        } else {
            Write-Host "打印机 '$($printer.Name)' 已存在" -ForegroundColor Yellow
        }
    }
    catch {
        Write-Host "添加打印机 '$($printer.Name)' 失败: $_" -ForegroundColor Red
    }
}

使用说明

  1. 管理员权限:大多数打印机安装需要管理员权限运行脚本

  2. 驱动准备

    • 确保打印机驱动已安装在系统上
    • 或者使用内置的通用驱动
  3. 网络检查

    • 确认打印机IP地址可达
    • 测试端口连通性:Test-NetConnection -ComputerName 192.168.1.100 -Port 9100
  4. 常见故障排除

    # 检查已安装的打印机驱动
    Get-PrinterDriver | Select-Object Name
    # 查看所有打印机端口
    Get-PrinterPort
    # 删除打印机(如需重装)
    Remove-Printer -Name "PrinterName"

选择合适的脚本,根据你的操作系统和环境进行调整即可。

抱歉,评论功能暂时关闭!