From 48c68cbc7284cea55ab03284baa010edde6390ce Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Fri, 5 Dec 2025 15:39:53 -0500 Subject: [PATCH] Update samy.ps1 --- samy.ps1 | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/samy.ps1 b/samy.ps1 index 468711a..2e5fe84 100644 --- a/samy.ps1 +++ b/samy.ps1 @@ -1834,12 +1834,21 @@ function Invoke-GetPrinters { # NOTE: We never log the actual password $printers = Get-SamyClientListFromServer -Uri $uri -Password $password - # 🔹 EXTRA SAFETY: never pass $null to Send-JSON + # EXTRA SAFETY: never pass $null to Send-JSON if ($null -eq $printers) { Write-LogHybrid "Get-SamyClientListFromServer returned `$null; sending empty JSON array." Warning Printers -LogToEvent $printers = @() } + # Always update local printers.json with latest from bananas + # but don't wipe a good file when we got *nothing* back. + try { + Update-SamyPrinterConfig -PrinterProfiles $printers -SkipIfEmpty + } + catch { + Write-LogHybrid "Update-SamyPrinterConfig failed: $($_.Exception.Message)" Warning Printers -LogToEvent + } + # Return raw objects as JSON; JS will filter/group Send-JSON $Context $printers } @@ -2199,6 +2208,53 @@ function Invoke-SamyPrinterInstall { } } +function Update-SamyPrinterConfig { + <# + .SYNOPSIS + Writes the fetched printer profiles to the local printers.json file. + + .DESCRIPTION + - Uses Get-SamyPrinterLocalConfigPath to determine where printers.json lives. + - Always overwrites printers.json when non-empty data is provided. + - If called with -SkipIfEmpty and the data is empty/null, it does *nothing* + so we don’t wipe a good config on a bad day. + - Resets the in-memory cache so future Get-SamyPrinterProfiles calls reload from disk. + #> + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [object]$PrinterProfiles, + + [switch]$SkipIfEmpty + ) + + $path = Get-SamyPrinterLocalConfigPath + + # Normalize to array + $profilesArray = @($PrinterProfiles) + + if ($SkipIfEmpty -and ($null -eq $PrinterProfiles -or $profilesArray.Count -eq 0)) { + Write-LogHybrid "Update-SamyPrinterConfig: no printer profiles returned; keeping existing printers.json." Warning Printers -LogToEvent + return + } + + if ($profilesArray.Count -eq 0) { + Write-LogHybrid "Update-SamyPrinterConfig: zero profiles; writing empty printers.json." Warning Printers -LogToEvent + } + + try { + $profilesArray | ConvertTo-Json -Depth 5 | Set-Content -Path $path -Encoding UTF8 + Write-LogHybrid "Saved $($profilesArray.Count) printer profiles to '$path'." Success Printers -LogToEvent + + # Invalidate per-session cache so future reads use the new file + $Script:Samy_PrinterProfiles = $null + } + catch { + Write-LogHybrid "Failed to write printers.json to '$path': $($_.Exception.Message)" Error Printers -LogToEvent + } +} + + #endregion Printer core (local config + install) #endregion Printer handlers