# Zebra Printer Management System - Sanitized Portfolio Sample # This sample uses placeholder printer names, reserved documentation IPs, and # generic label codes. Review and test in a lab before adapting it to production. Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $printers = [ordered]@{ "Warehouse Printer 1 - 192.0.2.10" = "192.0.2.10" "Warehouse Printer 2 - 192.0.2.11" = "192.0.2.11" "Warehouse Printer 3 - 192.0.2.12" = "192.0.2.12" } $labelPool = @( "LABEL-A", "LABEL-B", "LABEL-C", "LABEL-D", "LABEL-E", "LABEL-F" ) $script:usedLabels = New-Object System.Collections.ArrayList $script:batchHistory = New-Object System.Collections.ArrayList $script:batchRunning = $false $script:batchStartTime = $null # Set to $true only in a controlled lab or authorized production environment. $script:EnableActualPrinting = $false function Add-Log { param([string]$Message) $timestamp = Get-Date -Format "HH:mm:ss" $activityLog.AppendText("[$timestamp] $Message`r`n") } function Get-SelectedPrinters { $selected = @() foreach ($item in $printerList.CheckedItems) { $selected += [string]$item } return $selected } function Get-SelectedLabels { $selected = @() foreach ($item in $labelList.CheckedItems) { $selected += [string]$item } return $selected } function Reset-UsedLabels { $script:usedLabels.Clear() Add-Log "Used label tracking reset. All selected labels are available again." Update-UsedLabelDisplay } function Update-UsedLabelDisplay { $usedBox.Items.Clear() foreach ($label in $script:usedLabels) { [void]$usedBox.Items.Add($label) } $usedCountLabel.Text = "Used Labels: $($script:usedLabels.Count)" } function Get-RandomUnusedLabel { param([string[]]$SelectedLabels) if ($SelectedLabels.Count -eq 0) { throw "No labels selected." } $availableLabels = @($SelectedLabels | Where-Object { $script:usedLabels -notcontains $_ }) if ($availableLabels.Count -eq 0) { Add-Log "All selected labels have been used once. Resetting used-label tracking." $script:usedLabels.Clear() $availableLabels = $SelectedLabels } $randomLabel = Get-Random -InputObject $availableLabels [void]$script:usedLabels.Add($randomLabel) Update-UsedLabelDisplay return $randomLabel } function New-ZplLabel { param( [string]$Barcode, [int]$Quantity ) @" ^XA ^BY6,2,300 ^FO150,70^BCR^FD$Barcode^FS ^PQ$Quantity ^XZ "@ } function Send-ZebraJob { param( [string]$IpAddress, [string]$Barcode, [int]$Quantity ) $zpl = New-ZplLabel -Barcode $Barcode -Quantity $Quantity if (-not $script:EnableActualPrinting) { Add-Log "TEST MODE: Would send '$Barcode' Qty $Quantity to $IpAddress." return } $client = New-Object System.Net.Sockets.TcpClient try { $client.Connect($IpAddress, 9100) $stream = $client.GetStream() $writer = New-Object System.IO.StreamWriter($stream) $writer.Write($zpl) $writer.Flush() $writer.Close() $client.Close() } catch { if ($client) { $client.Close() } throw $_ } } function Lock-Controls { param([bool]$Locked) $printerList.Enabled = -not $Locked $labelList.Enabled = -not $Locked $quantityBox.Enabled = -not $Locked $jobsBox.Enabled = -not $Locked $startButton.Enabled = -not $Locked $resetButton.Enabled = -not $Locked $selectAllLabelsButton.Enabled = -not $Locked $clearLabelsButton.Enabled = -not $Locked } function Wait-WithUiEvents { param([int]$Milliseconds) $endTime = (Get-Date).AddMilliseconds($Milliseconds) while ((Get-Date) -lt $endTime) { [System.Windows.Forms.Application]::DoEvents() Start-Sleep -Milliseconds 50 } } $form = New-Object System.Windows.Forms.Form $form.Text = "Zebra Random Batch Printer" $form.Size = New-Object System.Drawing.Size(900, 650) $form.StartPosition = "CenterScreen" $form.BackColor = [System.Drawing.Color]::FromArgb(245,245,245) $form.Font = New-Object System.Drawing.Font("Segoe UI", 9) $title = New-Object System.Windows.Forms.Label $title.Text = "Zebra Random Batch Printer" $title.Font = New-Object System.Drawing.Font("Segoe UI", 16, [System.Drawing.FontStyle]::Bold) $title.Location = New-Object System.Drawing.Point(20, 15) $title.Size = New-Object System.Drawing.Size(500, 35) $form.Controls.Add($title) $subtitle = New-Object System.Windows.Forms.Label $subtitle.Text = "Randomized label assignment with no repeats until all selected labels are used once." $subtitle.Location = New-Object System.Drawing.Point(23, 50) $subtitle.Size = New-Object System.Drawing.Size(650, 22) $form.Controls.Add($subtitle) $printerGroup = New-Object System.Windows.Forms.GroupBox $printerGroup.Text = "1. Select Printer(s)" $printerGroup.Location = New-Object System.Drawing.Point(20, 90) $printerGroup.Size = New-Object System.Drawing.Size(260, 145) $form.Controls.Add($printerGroup) $printerList = New-Object System.Windows.Forms.CheckedListBox $printerList.Location = New-Object System.Drawing.Point(15, 25) $printerList.Size = New-Object System.Drawing.Size(230, 95) $printerList.CheckOnClick = $true foreach ($printer in $printers.Keys) { [void]$printerList.Items.Add($printer) } $printerGroup.Controls.Add($printerList) $labelGroup = New-Object System.Windows.Forms.GroupBox $labelGroup.Text = "2. Select Label Pool" $labelGroup.Location = New-Object System.Drawing.Point(300, 90) $labelGroup.Size = New-Object System.Drawing.Size(260, 320) $form.Controls.Add($labelGroup) $labelList = New-Object System.Windows.Forms.CheckedListBox $labelList.Location = New-Object System.Drawing.Point(15, 25) $labelList.Size = New-Object System.Drawing.Size(230, 220) $labelList.CheckOnClick = $true foreach ($label in $labelPool) { [void]$labelList.Items.Add($label) } $labelGroup.Controls.Add($labelList) $selectAllLabelsButton = New-Object System.Windows.Forms.Button $selectAllLabelsButton.Text = "Select All" $selectAllLabelsButton.Location = New-Object System.Drawing.Point(15, 260) $selectAllLabelsButton.Size = New-Object System.Drawing.Size(100, 32) $labelGroup.Controls.Add($selectAllLabelsButton) $clearLabelsButton = New-Object System.Windows.Forms.Button $clearLabelsButton.Text = "Clear" $clearLabelsButton.Location = New-Object System.Drawing.Point(145, 260) $clearLabelsButton.Size = New-Object System.Drawing.Size(100, 32) $labelGroup.Controls.Add($clearLabelsButton) $batchGroup = New-Object System.Windows.Forms.GroupBox $batchGroup.Text = "3. Batch Settings" $batchGroup.Location = New-Object System.Drawing.Point(580, 90) $batchGroup.Size = New-Object System.Drawing.Size(280, 320) $form.Controls.Add($batchGroup) $qtyLabel = New-Object System.Windows.Forms.Label $qtyLabel.Text = "Quantity per label:" $qtyLabel.Location = New-Object System.Drawing.Point(20, 35) $qtyLabel.Size = New-Object System.Drawing.Size(140, 22) $batchGroup.Controls.Add($qtyLabel) $quantityBox = New-Object System.Windows.Forms.NumericUpDown $quantityBox.Location = New-Object System.Drawing.Point(165, 32) $quantityBox.Size = New-Object System.Drawing.Size(80, 25) $quantityBox.Minimum = 1 $quantityBox.Maximum = 999999 $quantityBox.Value = 1 $batchGroup.Controls.Add($quantityBox) $jobsLabel = New-Object System.Windows.Forms.Label $jobsLabel.Text = "Random pulls/jobs:" $jobsLabel.Location = New-Object System.Drawing.Point(20, 75) $jobsLabel.Size = New-Object System.Drawing.Size(140, 22) $batchGroup.Controls.Add($jobsLabel) $jobsBox = New-Object System.Windows.Forms.NumericUpDown $jobsBox.Location = New-Object System.Drawing.Point(165, 72) $jobsBox.Size = New-Object System.Drawing.Size(80, 25) $jobsBox.Minimum = 1 $jobsBox.Maximum = 999999 $jobsBox.Value = 1 $batchGroup.Controls.Add($jobsBox) $modeLabel = New-Object System.Windows.Forms.Label $modeLabel.Text = "Mode: Random / No Repeat Until Pool Used" $modeLabel.Location = New-Object System.Drawing.Point(20, 115) $modeLabel.Size = New-Object System.Drawing.Size(240, 22) $batchGroup.Controls.Add($modeLabel) $productionModeLabel = New-Object System.Windows.Forms.Label $productionModeLabel.Text = "Current Mode: TEST - printing disabled" $productionModeLabel.ForeColor = [System.Drawing.Color]::DarkOrange $productionModeLabel.Location = New-Object System.Drawing.Point(20, 145) $productionModeLabel.Size = New-Object System.Drawing.Size(240, 22) $batchGroup.Controls.Add($productionModeLabel) $startButton = New-Object System.Windows.Forms.Button $startButton.Text = "Start Random Batch" $startButton.Location = New-Object System.Drawing.Point(20, 190) $startButton.Size = New-Object System.Drawing.Size(225, 38) $batchGroup.Controls.Add($startButton) $resetButton = New-Object System.Windows.Forms.Button $resetButton.Text = "Reset Used Labels" $resetButton.Location = New-Object System.Drawing.Point(20, 240) $resetButton.Size = New-Object System.Drawing.Size(225, 34) $batchGroup.Controls.Add($resetButton) $usedGroup = New-Object System.Windows.Forms.GroupBox $usedGroup.Text = "Used Label Tracking" $usedGroup.Location = New-Object System.Drawing.Point(20, 250) $usedGroup.Size = New-Object System.Drawing.Size(260, 160) $form.Controls.Add($usedGroup) $usedCountLabel = New-Object System.Windows.Forms.Label $usedCountLabel.Text = "Used Labels: 0" $usedCountLabel.Location = New-Object System.Drawing.Point(15, 25) $usedCountLabel.Size = New-Object System.Drawing.Size(220, 22) $usedGroup.Controls.Add($usedCountLabel) $usedBox = New-Object System.Windows.Forms.ListBox $usedBox.Location = New-Object System.Drawing.Point(15, 50) $usedBox.Size = New-Object System.Drawing.Size(230, 90) $usedGroup.Controls.Add($usedBox) $statusGroup = New-Object System.Windows.Forms.GroupBox $statusGroup.Text = "Batch Status" $statusGroup.Location = New-Object System.Drawing.Point(20, 425) $statusGroup.Size = New-Object System.Drawing.Size(840, 65) $form.Controls.Add($statusGroup) $statusLabel = New-Object System.Windows.Forms.Label $statusLabel.Text = "Status: Ready" $statusLabel.Location = New-Object System.Drawing.Point(15, 25) $statusLabel.Size = New-Object System.Drawing.Size(430, 25) $statusGroup.Controls.Add($statusLabel) $checkInLabel = New-Object System.Windows.Forms.Label $checkInLabel.Text = "Come Check Back Around: N/A" $checkInLabel.Location = New-Object System.Drawing.Point(480, 25) $checkInLabel.Size = New-Object System.Drawing.Size(320, 25) $statusGroup.Controls.Add($checkInLabel) $activityGroup = New-Object System.Windows.Forms.GroupBox $activityGroup.Text = "Activity Log" $activityGroup.Location = New-Object System.Drawing.Point(20, 500) $activityGroup.Size = New-Object System.Drawing.Size(840, 95) $form.Controls.Add($activityGroup) $activityLog = New-Object System.Windows.Forms.TextBox $activityLog.Location = New-Object System.Drawing.Point(15, 22) $activityLog.Size = New-Object System.Drawing.Size(810, 58) $activityLog.Multiline = $true $activityLog.ScrollBars = "Vertical" $activityLog.ReadOnly = $true $activityGroup.Controls.Add($activityLog) $selectAllLabelsButton.Add_Click({ for ($i = 0; $i -lt $labelList.Items.Count; $i++) { $labelList.SetItemChecked($i, $true) } Add-Log "All labels selected." }) $clearLabelsButton.Add_Click({ for ($i = 0; $i -lt $labelList.Items.Count; $i++) { $labelList.SetItemChecked($i, $false) } Add-Log "Label selection cleared." }) $resetButton.Add_Click({ Reset-UsedLabels }) $startButton.Add_Click({ $selectedPrinters = Get-SelectedPrinters $selectedLabels = Get-SelectedLabels $quantity = [int]$quantityBox.Value $jobCount = [int]$jobsBox.Value if ($selectedPrinters.Count -eq 0) { [System.Windows.Forms.MessageBox]::Show("Please select at least one printer.") return } if ($selectedLabels.Count -eq 0) { [System.Windows.Forms.MessageBox]::Show("Please select at least one label.") return } $script:batchRunning = $true $script:batchStartTime = Get-Date $statusLabel.Text = "Status: Running random batch" $totalEstimatedMinutes = $jobCount * 20 $estimatedReturn = (Get-Date).AddMinutes($totalEstimatedMinutes) $checkInLabel.Text = "Come Check Back Around: $($estimatedReturn.ToString('hh:mm tt'))" Lock-Controls $true try { for ($jobNumber = 1; $jobNumber -le $jobCount; $jobNumber++) { foreach ($printerName in $selectedPrinters) { $ip = $printers[$printerName] $barcode = Get-RandomUnusedLabel -SelectedLabels $selectedLabels $statusLabel.Text = "Status: Job $jobNumber / $jobCount - $barcode to $printerName" $form.Refresh() Add-Log "Selected $barcode for $printerName ($ip), Qty $quantity." try { Send-ZebraJob -IpAddress $ip -Barcode $barcode -Quantity $quantity Add-Log "Success: $barcode handled for $ip." $historyItem = [PSCustomObject]@{ Time = Get-Date PrinterName = $printerName IP = $ip Barcode = $barcode Quantity = $quantity Status = if ($script:EnableActualPrinting) { "Sent" } else { "Test" } } [void]$script:batchHistory.Add($historyItem) } catch { Add-Log "FAILED: $barcode to $ip - $($_.Exception.Message)" } Wait-WithUiEvents -Milliseconds 500 } } $statusLabel.Text = "Status: Batch complete" Add-Log "Batch complete. Used label list remains stored until reset or all selected labels cycle." Add-Log "Estimate used: $jobCount random cycle(s) x 20 minutes = $totalEstimatedMinutes minute(s)." } finally { $script:batchRunning = $false Add-Log "Batch runtime complete." Lock-Controls $false } }) $form.Add_Shown({ Add-Log "App loaded in TEST mode. Actual printing is disabled." Add-Log "Enable printing only in a controlled lab or authorized production environment." }) [void]$form.ShowDialog()