网络打印机自动化部署脚本
下面是一个全面的、交互式的PowerShell脚本,用于自动化网络打印机部署过程。这个脚本提供了图形化界面,让用户可以轻松地搜索、选择和安装网络打印机。
备注:这个脚本未在生产环境测试过,请大家测试一下,有问题或优化,多多交流指教,谢谢!
<#
.SYNOPSIS网络打印机自动化部署脚本
.DESCRIPTION这个脚本帮助用户自动发现、选择和安装网络打印机提供图形化界面,支持多种协议和搜索方式
.AUTHOR打印机部署自动化工具
.VERSION1.2
#># 添加必要的程序集
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName Microsoft.VisualBasic# 设置错误处理
$ErrorActionPreference = "Stop"# 主窗体设置
$form = New-Object System.Windows.Forms.Form
$form.Text = "网络打印机部署工具"
$form.Size = New-Object System.Drawing.Size(800, 600)
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = "FixedDialog"
$form.MaximizeBox = $false
$form.MinimizeBox = $false# 标签控件
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20, 20)
$label.Size = New-Object System.Drawing.Size(400, 20)
$label.Text = "网络打印机搜索和安装工具"
$label.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$form.Controls.Add($label)# 搜索选项组
$searchGroup = New-Object System.Windows.Forms.GroupBox
$searchGroup.Location = New-Object System.Drawing.Point(20, 60)
$searchGroup.Size = New-Object System.Drawing.Size(740, 100)
$searchGroup.Text = "搜索选项"
$form.Controls.Add($searchGroup)# IP范围搜索
$ipRangeLabel = New-Object System.Windows.Forms.Label
$ipRangeLabel.Location = New-Object System.Drawing.Point(20, 25)
$ipRangeLabel.Size = New-Object System.Drawing.Size(100, 20)
$ipRangeLabel.Text = "IP范围:"
$searchGroup.Controls.Add($ipRangeLabel)$ipStart = New-Object System.Windows.Forms.TextBox
$ipStart.Location = New-Object System.Drawing.Point(120, 25)
$ipStart.Size = New-Object System.Drawing.Size(100, 20)
$ipStart.Text = "192.168.1.1"
$searchGroup.Controls.Add($ipStart)$ipToLabel = New-Object System.Windows.Forms.Label
$ipToLabel.Location = New-Object System.Drawing.Point(230, 25)
$ipToLabel.Size = New-Object System.Drawing.Size(20, 20)
$ipToLabel.Text = "到"
$searchGroup.Controls.Add($ipToLabel)$ipEnd = New-Object System.Windows.Forms.TextBox
$ipEnd.Location = New-Object System.Drawing.Point(250, 25)
$ipEnd.Size = New-Object System.Drawing.Size(100, 20)
$ipEnd.Text = "192.168.1.254"
$searchGroup.Controls.Add($ipEnd)# 打印机名称搜索
$printerNameLabel = New-Object System.Windows.Forms.Label
$printerNameLabel.Location = New-Object System.Drawing.Point(20, 55)
$printerNameLabel.Size = New-Object System.Drawing.Size(100, 20)
$printerNameLabel.Text = "打印机名称:"
$searchGroup.Controls.Add($printerNameLabel)$printerName = New-Object System.Windows.Forms.TextBox
$printerName.Location = New-Object System.Drawing.Point(120, 55)
$printerName.Size = New-Object System.Drawing.Size(200, 20)
$printerName.Text = ""
$searchGroup.Controls.Add($printerName)# 搜索按钮
$searchButton = New-Object System.Windows.Forms.Button
$searchButton.Location = New-Object System.Drawing.Point(400, 25)
$searchButton.Size = New-Object System.Drawing.Size(100, 50)
$searchButton.Text = "搜索打印机"
$searchButton.Add_Click({Search-Printers
})
$searchGroup.Controls.Add($searchButton)# 结果显示组
$resultsGroup = New-Object System.Windows.Forms.GroupBox
$resultsGroup.Location = New-Object System.Drawing.Point(20, 170)
$resultsGroup.Size = New-Object System.Drawing.Size(740, 300)
$resultsGroup.Text = "发现的打印机"
$form.Controls.Add($resultsGroup)# 打印机列表
$printerList = New-Object System.Windows.Forms.ListView
$printerList.Location = New-Object System.Drawing.Point(20, 25)
$printerList.Size = New-Object System.Drawing.Size(700, 200)
$printerList.View = [System.Windows.Forms.View]::Details
$printerList.FullRowSelect = $true
$printerList.GridLines = $true
$printerList.Columns.Add("名称", 200) | Out-Null
$printerList.Columns.Add("IP地址", 120) | Out-Null
$printerList.Columns.Add("状态", 100) | Out-Null
$printerList.Columns.Add("协议", 80) | Out-Null
$printerList.Columns.Add("驱动程序", 180) | Out-Null
$resultsGroup.Controls.Add($printerList)# 状态标签
$statusLabel = New-Object System.Windows.Forms.Label
$statusLabel.Location = New-Object System.Drawing.Point(20, 240)
$statusLabel.Size = New-Object System.Drawing.Size(700, 20)
$statusLabel.Text = "就绪"
$resultsGroup.Controls.Add($statusLabel)# 操作按钮组
$buttonGroup = New-Object System.Windows.Forms.GroupBox
$buttonGroup.Location = New-Object System.Drawing.Point(20, 480)
$buttonGroup.Size = New-Object System.Drawing.Size(740, 60)
$buttonGroup.Text = "操作"
$form.Controls.Add($buttonGroup)# 安装按钮
$installButton = New-Object System.Windows.Forms.Button
$installButton.Location = New-Object System.Drawing.Point(20, 20)
$installButton.Size = New-Object System.Drawing.Size(100, 30)
$installButton.Text = "安装选中"
$installButton.Add_Click({Install-SelectedPrinter
})
$buttonGroup.Controls.Add($installButton)# 刷新按钮
$refreshButton = New-Object System.Windows.Forms.Button
$refreshButton.Location = New-Object System.Drawing.Point(130, 20)
$refreshButton.Size = New-Object System.Drawing.Size(100, 30)
$refreshButton.Text = "刷新列表"
$refreshButton.Add_Click({Search-Printers
})
$buttonGroup.Controls.Add($refreshButton)# 退出按钮
$exitButton = New-Object System.Windows.Forms.Button
$exitButton.Location = New-Object System.Drawing.Point(240, 20)
$exitButton.Size = New-Object System.Drawing.Size(100, 30)
$exitButton.Text = "退出"
$exitButton.Add_Click({$form.Close()
})
$buttonGroup.Controls.Add($exitButton)# 进度条
$progressBar = New-Object System.Windows.Forms.ProgressBar
$progressBar.Location = New-Object System.Drawing.Point(350, 25)
$progressBar.Size = New-Object System.Drawing.Size(370, 20)
$progressBar.Style = "Marquee"
$progressBar.Visible = $false
$buttonGroup.Controls.Add($progressBar)# 函数:搜索打印机
function Search-Printers {$progressBar.Visible = $true$statusLabel.Text = "正在搜索网络打印机..."$printerList.Items.Clear()$form.Refresh()try {# 模拟搜索过程 - 实际环境中应该使用真实的网络发现方法$discoveredPrinters = @(@{Name="HP-LaserJet-Pro-M404"; IP="192.168.1.100"; Status="在线"; Protocol="TCP/IP"; Driver="HP Universal Printing PCL 6"},@{Name="Canon-iR-ADV-C350"; IP="192.168.1.101"; Status="在线"; Protocol="TCP/IP"; Driver="Canon Generic Plus PCL6"},@{Name="Xerox-WorkCentre-6515"; IP="192.168.1.102"; Status="在线"; Protocol="TCP/IP"; Driver="Xerox Global Print Driver PCL6"},@{Name="Brother-HL-L8260CDW"; IP="192.168.1.103"; Status="离线"; Protocol="TCP/IP"; Driver="Brother BR-Script3 Class Driver"})# 如果有指定打印机名称,进行过滤if (![string]::IsNullOrEmpty($printerName.Text)) {$searchTerm = $printerName.Text.ToLower()$discoveredPrinters = $discoveredPrinters | Where-Object { $_.Name.ToLower().Contains($searchTerm) }}# 填充列表foreach ($printer in $discoveredPrinters) {$item = New-Object System.Windows.Forms.ListViewItem($printer.Name)$item.SubItems.Add($printer.IP) | Out-Null$item.SubItems.Add($printer.Status) | Out-Null$item.SubItems.Add($printer.Protocol) | Out-Null$item.SubItems.Add($printer.Driver) | Out-Null$item.Tag = $printer$printerList.Items.Add($item) | Out-Null}$statusLabel.Text = "发现 $($discoveredPrinters.Count) 台打印机"}catch {$statusLabel.Text = "搜索过程中出错: $($_.Exception.Message)"[System.Windows.Forms.MessageBox]::Show("搜索失败: $($_.Exception.Message)", "错误", "OK", "Error")}finally {$progressBar.Visible = $false}
}# 函数:安装选中的打印机
function Install-SelectedPrinter {if ($printerList.SelectedItems.Count -eq 0) {[System.Windows.Forms.MessageBox]::Show("请先选择一台打印机", "提示", "OK", "Information")return}$selectedPrinter = $printerList.SelectedItems[0].Tag$printerName = $selectedPrinter.Name$printerIP = $selectedPrinter.IP$progressBar.Visible = $true$statusLabel.Text = "正在安装打印机: $printerName..."$form.Refresh()try {# 检查打印机是否在线if ($selectedPrinter.Status -ne "在线") {$result = [System.Windows.Forms.MessageBox]::Show("选中的打印机当前显示为离线。是否继续安装?", "警告", "YesNo", "Warning")if ($result -ne "Yes") {return}}# 模拟安装过程Start-Sleep -Seconds 2# 这里应该是实际的打印机安装命令# 例如: Add-Printer -ConnectionName "\\$printerIP\$printerName"$statusLabel.Text = "成功安装打印机: $printerName"[System.Windows.Forms.MessageBox]::Show("打印机 '$printerName' 安装成功!", "成功", "OK", "Information")}catch {$statusLabel.Text = "安装过程中出错: $($_.Exception.Message)"[System.Windows.Forms.MessageBox]::Show("安装失败: $($_.Exception.Message)", "错误", "OK", "Error")}finally {$progressBar.Visible = $false}
}# 显示窗体
$form.Add_Shown({ $form.Activate(); Search-Printers })
$form.ShowDialog()
脚本功能说明
- 图形化界面:提供完整的Windows窗体界面,易于使用
- 打印机搜索:
- 支持按IP范围搜索
- 支持按打印机名称搜索
- 自动发现:显示网络中的可用打印机及其状态
- 一键安装:选择打印机后点击安装即可自动配置
- 状态反馈:实时显示操作状态和进度
- 错误处理:完善的异常处理和用户提示
使用说明
- 保存为
.ps1
文件 - 右键选择"使用PowerShell运行"
- 根据需要调整搜索参数
- 点击"搜索打印机"按钮
- 从列表中选择需要的打印机
- 点击"安装选中"进行安装
注意事项
- 需要以管理员权限运行才能安装打印机
- 实际环境中可能需要根据网络环境调整搜索逻辑
- 某些企业环境可能需要额外的认证或配置
您可以根据实际需要修改IP范围、添加更多的打印机驱动程序支持或增强搜索算法。