Add src/handlers.printers.ps1

This commit is contained in:
2026-01-14 02:13:17 -05:00
parent 4c7be530e4
commit 66d34572d6

150
src/handlers.printers.ps1 Normal file
View File

@@ -0,0 +1,150 @@
function Invoke-GetPrinters {
param($Context)
try {
if ($Context.Request.HttpMethod -ne 'POST') {
$Context.Response.StatusCode = 405
Send-Text $Context 'Use POST'
return
}
if (-not (Get-Command Get-SvsPrinterProfilesFromServer -ErrorAction SilentlyContinue)) {
Write-LogHybrid "SVSMSP cmdlets missing. Attempting Install-SVSMSP -InstallToolkit..." Warning Printers -LogToEvent
try {
Install-SVSMSP -InstallToolkit
Import-Module SVSMSP -Force -ErrorAction SilentlyContinue
}
catch {
Write-LogHybrid "Auto-install of SVSMSP failed: $($_.Exception.Message)" Error Printers -LogToEvent
$Context.Response.StatusCode = 500
Send-Text $Context "SVSMSP auto-install failed. Run 'Install SVSMSP Module' manually."
return
}
if (-not (Get-Command Get-SvsPrinterProfilesFromServer -ErrorAction SilentlyContinue)) {
Write-LogHybrid "SVSMSP installed but printer cmdlets still unavailable." Error Printers -LogToEvent
$Context.Response.StatusCode = 500
Send-Text $Context "SVSMSP installed but printer commands still not available. Restart SAMY."
return
}
}
$rawBody = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
if (-not $rawBody) {
$Context.Response.StatusCode = 400
Send-Text $Context 'Missing request body.'
return
}
try { $body = $rawBody | ConvertFrom-Json }
catch {
$Context.Response.StatusCode = 400
Send-Text $Context 'Invalid JSON body.'
return
}
$password = [string]$body.password
if ($password -eq '') {
Write-LogHybrid "Printer password is blank; relying on allowlisted IP (server-side)." Info Printers -LogToEvent
}
$uri = 'https://bananas.svstools.ca/getprinters'
Write-LogHybrid "Fetching printers from $uri" Info Printers -LogToEvent
$printers = Get-SvsPrinterProfilesFromServer -Uri $uri -Password $password
if ($null -eq $printers) { $printers = @() }
try { Set-SvsPrinterLocalConfig -PrinterProfiles $printers -SkipIfEmpty }
catch { Write-LogHybrid "Set-SvsPrinterLocalConfig failed: $($_.Exception.Message)" Warning Printers -LogToEvent }
Send-JSON $Context $printers
}
catch {
Write-LogHybrid "Invoke-GetPrinters error: $($_.Exception.Message)" Error Printers -LogToEvent
$Context.Response.StatusCode = 500
Send-Text $Context "Internal server error fetching printers."
}
}
function Invoke-InstallPrinters {
param($Context)
try {
if ($Context.Request.HttpMethod -ne 'POST') {
$Context.Response.StatusCode = 405
Send-Text $Context 'Use POST'
return
}
$rawBody = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
if (-not $rawBody) {
$Context.Response.StatusCode = 400
Send-Text $Context 'Missing request body.'
return
}
try { $body = $rawBody | ConvertFrom-Json }
catch {
$Context.Response.StatusCode = 400
Send-Text $Context 'Invalid JSON body.'
return
}
$printers = $body.printers
if (-not $printers -or $printers.Count -eq 0) {
$Context.Response.StatusCode = 400
Send-Text $Context 'No printers specified.'
return
}
Write-LogHybrid "Received request to install $($printers.Count) printers." Info Printers -LogToEvent
$successCount = 0
$failures = @()
foreach ($p in $printers) {
$clientCode = $p.ClientCode
$profileName = $p.ProfileName
$setDefault = [bool]($p.PSObject.Properties.Name -contains 'SetAsDefault' -and $p.SetAsDefault)
if (-not $clientCode -or -not $profileName) {
$msg = "Skipping printer entry: ClientCode or ProfileName missing."
Write-LogHybrid $msg Warning Printers -LogToEvent
$failures += $msg
continue
}
$summary = "ClientCode=$clientCode ProfileName=$profileName SetAsDefault=$setDefault"
Write-LogHybrid "Installing printer ($summary)" Info Printers -LogToEvent
try {
Invoke-SVSPrinterInstall `
-ClientCode $clientCode `
-ProfileName $profileName `
-SetAsDefault:$setDefault
$successCount++
Write-LogHybrid "Printer installed successfully ($summary)" Success Printers -LogToEvent
}
catch {
$errMsg = "Failed to install printer ($summary): $($_.Exception.Message)"
Write-LogHybrid $errMsg Error Printers -LogToEvent
$failures += $errMsg
}
}
Send-JSON $Context @{
SuccessCount = $successCount
FailureCount = $failures.Count
Failures = $failures
Message = "Printer install processed. Check SAMY logs for detail."
}
}
catch {
Write-LogHybrid "Invoke-InstallPrinters error: $($_.Exception.Message)" Error Printers -LogToEvent
$Context.Response.StatusCode = 500
Send-Text $Context "Internal server error installing printers."
}
}