下面是一個全面的、交互式的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范圍、添加更多的打印機驅動程序支持或增強搜索算法。