Update samy.ps1
This commit is contained in:
58
samy.ps1
58
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
|
||||
|
||||
Reference in New Issue
Block a user