# Phase 2.6 — CS-SERVER Print Server Setup
# Run on CS-SERVER via GuruRMM remote PowerShell
# Last updated: 2026-05-20 (Howard)
#
# STATUS AS OF 2026-05-20: COMPLETE — all 13 printers installed and shared
#   KM driver folder archived to: D:\Shares\Server\Drivers\KM_Universal_PCL6\
#   Epson INF files at: C:\Users\sysadmin\Documents\ComputerGuru Connect v2\Files\epsonetdrivers\
#
# Drivers installed on CS-SERVER:
#   Canon Generic Plus PCL6             — Copy Room, Accounting, Executive Director, Kitchen, Life Enrichment, Memory Care Director
#   Brother Generic Jpeg Type2 Class Driver — Business Office, Admin Office, Sales Marketing, Culinary Chef, Memory Care MedTech
#   KONICA MINOLTA Universal PCL        — Health Services C368
#   EPSON ET-5800 Series                — Front Desk (driver staged via pnputil, registered via Add-PrinterDriver)
#
# Epson ET-5800 install notes:
#   EPWizard.exe fails on Server 2019 (wlanapi.dll stub — WLAN stack absent).
#   Workaround: run installer on Server, copy extracted INFs from AppData\Local\Temp\ET-5800
#   before dismissing error. pnputil stages them; Add-PrinterDriver registers with spooler.

$ErrorActionPreference = 'Continue'

# Verify Print Server role is installed
$role = Get-WindowsFeature -Name Print-Server -ErrorAction SilentlyContinue
if ($role -and -not $role.Installed) {
    Install-WindowsFeature -Name Print-Server -IncludeManagementTools
    Write-Output '[OK] Print Server role installed'
} else {
    Write-Output '[--] Print Server role already present'
}

$printers = @(
    # Everyone gets this one — set as default via GPO
    @{
        IP       = '192.168.2.230'
        Port     = 'TCP_192.168.2.230'
        Name     = 'Copy Room Canon'
        Driver   = 'Canon Generic Plus PCL6'
        Share    = 'CopyRoom'
        Location = 'Copy Room (1st Floor)'
        Comment  = 'Canon imageRunner C478iF - All staff'
    }
    # Administrative / BOD
    @{
        IP       = '10.0.20.220'
        Port     = 'TCP_10.0.20.220'
        Name     = 'BOD Brother'
        Driver   = 'Brother Generic Jpeg Type2 Class Driver'
        Share    = 'BOD-Brother'
        Location = 'Room 103 area (Acct Asst desk)'
        Comment  = 'Brother MFC-L8900CDW - Business Office Director area'
    }
    @{
        IP       = '192.168.3.227'
        Port     = 'TCP_192.168.3.227'
        Name     = 'Accounting Canon'
        Driver   = 'Canon Generic Plus PCL6'
        Share    = 'Accounting'
        Location = 'Accounting (Room 103)'
        Comment  = 'Canon imageClass MF455DW - Lauren Hasselman'
    }
    @{
        IP       = '192.168.2.145'
        Port     = 'TCP_192.168.2.145'
        Name     = 'Room 103 Brother'
        Driver   = 'Brother Generic Jpeg Type2 Class Driver'
        Share    = 'Room103'
        Location = 'Room 103'
        Comment  = 'Brother MFC-9340CDW - Ashley Jensen, Christina DuPras'
    }
    @{
        IP       = '192.168.2.67'
        Port     = 'TCP_192.168.2.67'
        Name     = 'Executive Office Canon'
        Driver   = 'Canon Generic Plus PCL6'
        Share    = 'Exec-Office'
        Location = "Meredith's Office"
        Comment  = 'Canon imageClass MF743CDW - Meredith Kuhn only'
    }
    # Marketing / Sales
    @{
        IP       = '192.168.3.44'
        Port     = 'TCP_192.168.3.44'
        Name     = 'Marketing Brother'
        Driver   = 'Brother Generic Jpeg Type2 Class Driver'
        Share    = 'Marketing'
        Location = 'Room 217 (Marketing/Sales)'
        Comment  = 'Brother MFC-L8900CDW - Sales team'
    }
    # Culinary
    @{
        IP       = '192.168.3.232'
        Port     = 'TCP_192.168.3.232'
        Name     = 'Kitchen Canon'
        Driver   = 'Canon Generic Plus PCL6'
        Share    = 'Kitchen'
        Location = 'Kitchen Manager desk'
        Comment  = 'Canon imageClass MFC743CDW - Alyssa Brooks'
    }
    @{
        IP       = '192.168.3.88'
        Port     = 'TCP_192.168.3.88'
        Name     = 'Chef Brother'
        Driver   = 'Brother Generic Jpeg Type2 Class Driver'
        Share    = 'Chef'
        Location = 'Kitchen Chef station'
        Comment  = 'Brother MFC-9330CDW - JD Martin / Chef'
    }
    # Front Desk
    @{
        IP       = '192.168.2.147'
        Port     = 'TCP_192.168.2.147'
        Name     = 'Front Desk - Epson ET-5800'
        Driver   = 'EPSON ET-5800 Series'
        Share    = 'FrontDesk'
        Location = 'Front Desk'
        Comment  = 'Epson ET-5800 - Front Desk'
    }
    # Memory Care
    @{
        IP       = '192.168.3.52'
        Port     = 'TCP_192.168.3.52'
        Name     = 'Memory Care Director - Canon MF751CDW'
        Driver   = 'Canon Generic Plus PCL6'
        Share    = 'MCDirector'
        Location = 'Memory Care Room 603'
        Comment  = 'Canon imageClass MF751CDW - Shelby Trozzi'
    }
    @{
        IP       = '192.168.2.53'
        Port     = 'TCP_192.168.2.53'
        Name     = 'Memory Care MedTech - Brother'
        Driver   = 'Brother Generic Jpeg Type2 Class Driver'
        Share    = 'MCMedTech'
        Location = 'Memory Care Room 615'
        Comment  = 'Brother - MedTechs / Nurses'
    }
    # Memory Care Reception (EPSON833571 — dc:cd:2f:83:35:71)
    # Added 2026-05-29. Driver already installed from FrontDesk ET-5800 — no EPWizard re-run needed.
    # Access: OU=Care-Memorycare (GPO ILT). (Alma Montt offboarded 2026-06-25 — reassign to MC Reception replacement when named.)
    @{
        IP       = '10.0.20.78'
        Port     = 'TCP_10.0.20.78'
        Name     = 'Memory Care Reception - Epson ET-5800'
        Driver   = 'EPSON ET-5800 Series'
        Share    = 'MCReception'
        Location = 'Memory Care Reception (Floors 5/6)'
        Comment  = 'Epson ET-5800 - MemCare Reception / MC nurses'
    }
)

Write-Output ''
Write-Output '=== Installing Printers ==='

foreach ($p in $printers) {
    Write-Output ''
    Write-Output "--- $($p.Name) ($($p.IP)) ---"

    # Create TCP/IP port if needed
    if (-not (Get-PrinterPort -Name $p.Port -ErrorAction SilentlyContinue)) {
        Add-PrinterPort -Name $p.Port -PrinterHostAddress $p.IP
        Write-Output "  [OK] Port: $($p.Port)"
    } else {
        Write-Output "  [--] Port exists: $($p.Port)"
    }

    # Add and share the printer
    if (-not (Get-Printer -Name $p.Name -ErrorAction SilentlyContinue)) {
        Add-Printer -Name $p.Name `
            -DriverName $p.Driver `
            -PortName $p.Port `
            -Shared `
            -ShareName $p.Share `
            -Location $p.Location `
            -Comment $p.Comment
        Write-Output "  [OK] Installed + shared: \\CS-SERVER\$($p.Share)"
    } else {
        Write-Output "  [--] Already installed: $($p.Name)"
    }
}

Write-Output ''
Write-Output '=== Connectivity Check ==='
$all = @(
    @{ Name='Copy Room Canon';       IP='192.168.2.230' }
    @{ Name='BOD Brother';           IP='10.0.20.220' }
    @{ Name='Accounting Canon';      IP='192.168.3.227' }
    @{ Name='Room 103 Brother';      IP='192.168.2.145' }
    @{ Name='Executive Office';      IP='192.168.2.67' }
    @{ Name='Marketing Brother';     IP='192.168.3.44' }
    @{ Name='Kitchen Canon';         IP='192.168.3.232' }
    @{ Name='Chef Brother';          IP='192.168.3.88' }
    @{ Name='Front Desk - Epson';          IP='192.168.2.147' }
    @{ Name='Health Services C368';       IP='192.168.1.138' }
    @{ Name='MC Director Canon MF751CDW'; IP='192.168.3.52' }
    @{ Name='MC MedTech Brother';         IP='192.168.2.53' }
    @{ Name='MC Reception Epson ET-5800'; IP='10.0.20.78' }
)
foreach ($p in $all) {
    $ok = Test-Connection -ComputerName $p.IP -Count 1 -Quiet -ErrorAction SilentlyContinue
    $status = if ($ok) { '[OK]  reachable' } else { '[WARN] NOT reachable' }
    Write-Output "  $status - $($p.Name) ($($p.IP))"
}

Write-Output ''
Write-Output '=== All Shared Printers on CS-SERVER ==='
Get-Printer | Where-Object { $_.Shared } |
    Select-Object Name, ShareName, PortName, DriverName |
    Format-Table -AutoSize
