param( # Istniejące wirtualne kolejki, które chcesz połączyć w pulę [Parameter(Mandatory=$true)][string]$PrinterA, [Parameter(Mandatory=$true)][string]$PrinterB, # Docelowe porty LOCAL: # Dla testu realnego – podaj UNC do zdalnych kolejek (np. \\PRINTSRV\Brother_A i \\PRINTSRV\Brother_B) # Dla demo bez druku – podaj dowolne nazwy (np. LOCAL_A i LOCAL_B) [Parameter(Mandatory=$true)][string]$LocalPortA, [Parameter(Mandatory=$true)][string]$LocalPortB, # Nazwa puli i sterownik (ten sam co w druk_a/druk_b) [string]$PoolPrinterName = "PULA–MONO–BROTHER", [Parameter(Mandatory=$true)][string]$PoolDriverName, # Udostępnienie puli (opcjonalnie) [string]$ShareName = "" ) function Ensure-LocalPort { param([Parameter(Mandatory=$true)][string]$PortName) $exists = Get-PrinterPort -Name $PortName -ErrorAction SilentlyContinue if ($exists) { Write-Host "Local Port '$PortName' już istnieje." -ForegroundColor DarkCyan return } # W PrintManagement wystarczy podać -Name; brak parametrów TCP/IP → powstaje Local Port monitor z tą nazwą. Add-PrinterPort -Name $PortName -ErrorAction Stop Write-Host "Utworzono Local Port: $PortName" -ForegroundColor Green } function Bind-PrinterToPort { param([Parameter(Mandatory=$true)][string]$PrinterName, [Parameter(Mandatory=$true)][string]$PortName) $p = Get-Printer -Name $PrinterName -ErrorAction SilentlyContinue if (-not $p) { throw "Nie znaleziono drukarki '$PrinterName'." } Set-Printer -Name $PrinterName -PortName $PortName -ErrorAction Stop Write-Host "Przypięto '$PrinterName' do portu '$PortName'." -ForegroundColor Green } function Ensure-PoolPrinter-Local { param([Parameter(Mandatory=$true)][string]$PoolPrinterName, [Parameter(Mandatory=$true)][string]$DriverName, [Parameter(Mandatory=$true)][string]$PortA, [Parameter(Mandatory=$true)][string]$PortB, [string]$ShareName = "") $existing = Get-Printer -Name $PoolPrinterName -ErrorAction SilentlyContinue if (-not $existing) { Add-Printer -Name $PoolPrinterName -DriverName $DriverName -PortName $PortA -ErrorAction Stop Write-Host "Utworzono kolejkę puli: $PoolPrinterName (sterownik: $DriverName, port główny: $PortA)" -ForegroundColor Green } else { Write-Host "Kolejka '$PoolPrinterName' już istnieje — ustawiam główny port na $PortA." -ForegroundColor DarkCyan Set-Printer -Name $PoolPrinterName -PortName $PortA -ErrorAction SilentlyContinue } if ($ShareName -and $ShareName.Trim().Length -gt 0) { Set-Printer -Name $PoolPrinterName -Shared $true -ShareName $ShareName -ErrorAction SilentlyContinue Write-Host "Udostępniono jako: \\$env:COMPUTERNAME\$ShareName" -ForegroundColor DarkCyan } # Dwa porty w puli → lista CSV w rejestrze $regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Print\Printers\$PoolPrinterName" if (Test-Path $regPath) { $current = (Get-ItemProperty -Path $regPath -ErrorAction SilentlyContinue).Port $ports = @($PortA, $PortB) if ($current) { $ports += ($current.ToString().Split(",") | ForEach-Object { $_.Trim() }) } $unique = ($ports | Where-Object { $_ } | Select-Object -Unique) -join "," Set-ItemProperty -Path $regPath -Name "Port" -Value $unique -ErrorAction SilentlyContinue Write-Host "Pooling aktywny. Porty: $unique" -ForegroundColor Green } else { Write-Warning "Brak gałęzi rejestru dla '$PoolPrinterName'." } } # --- Wykonanie --- Import-Module PrintManagement -ErrorAction SilentlyContinue # 1) Local Porty Ensure-LocalPort -PortName $LocalPortA Ensure-LocalPort -PortName $LocalPortB # 2) Przepięcie wirtualnych kolejek na Local Port Bind-PrinterToPort -PrinterName $PrinterA -PortName $LocalPortA Bind-PrinterToPort -PrinterName $PrinterB -PortName $LocalPortB # 3) Pula na tym samym sterowniku Ensure-PoolPrinter-Local -PoolPrinterName $PoolPrinterName -DriverName $PoolDriverName -PortA $LocalPortA -PortB $LocalPortB -ShareName $ShareName Write-Host "Gotowe. Sprawdź właściwości puli → zakładka Porty (powinny być zaznaczone oba Local Port)." -ForegroundColor Green