Update samy.ps1

This commit is contained in:
2025-12-17 23:29:10 -05:00
parent 2a209dad5d
commit 5cca263199

105
samy.ps1
View File

@@ -269,6 +269,10 @@ $ConfirmPreference = 'None'
$Script:SamyBranch = 'main' # 'beta'
$Script:SamyRepoBase = 'https://git.svstools.ca/SVS_Public_Repo/SAMY/raw/branch'
if (Get-Command Set-SvsPrinterRepoConfig -ErrorAction SilentlyContinue) {
Set-SvsPrinterRepoConfig -RepoBase $Script:SamyRepoBase -Branch $Script:SamyBranch
}
# Top-left corner logo (SVS)
$Script:SamyTopLogoUrl = "$Script:SamyRepoBase/$Script:SamyBranch/SVS_logo.svg"
@@ -1771,14 +1775,6 @@ function Invoke-CleanupSVSMSP {
#region Printer handlers
function Invoke-GetPrinters {
param($Context)
@@ -1789,6 +1785,30 @@ function Invoke-GetPrinters {
return
}
# If printer cmdlets missing, try installing toolkit automatically
if (-not (Get-Command Get-SvsPrinterProfilesFromServer -ErrorAction SilentlyContinue)) {
Write-LogHybrid "SVSMSP cmdlets missing. Attempting Install-SVSMSP -InstallToolkit..." Warning Printers -LogToEvent
try {
Install-SVSMSP -InstallToolkit
}
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
}
# Re-check after install
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
}
}
# Read JSON body: { "password": "..." }
$rawBody = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
if (-not $rawBody) {
@@ -1815,19 +1835,9 @@ function Invoke-GetPrinters {
$uri = 'https://bananas.svstools.ca/getprinters'
Write-LogHybrid "Fetching printers from $uri" Info Printers -LogToEvent
# NOTE: We never log the actual password
$printers = Get-SvsPrinterProfilesFromServer -Uri $uri -Password $password
if ($null -eq $printers) { $printers = @() }
if ($null -eq $printers) { $printers = @() } # belt + suspenders
# EXTRA SAFETY: never pass $null to Send-JSON
if ($null -eq $printers) {
Write-LogHybrid "Get-SvsPrinterProfilesFromServer 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 {
Set-SvsPrinterLocalConfig -PrinterProfiles $printers -SkipIfEmpty
}
@@ -1835,7 +1845,6 @@ function Invoke-GetPrinters {
Write-LogHybrid "Set-SvsPrinterLocalConfig failed: $($_.Exception.Message)" Warning Printers -LogToEvent
}
# Return raw objects as JSON; JS will filter/group
Send-JSON $Context $printers
}
catch {
@@ -1845,6 +1854,7 @@ function Invoke-GetPrinters {
}
}
function Invoke-InstallPrinters {
param($Context)
@@ -1855,6 +1865,31 @@ function Invoke-InstallPrinters {
return
}
# Ensure printer install worker exists (from SVSMSP module or your module-integrated functions)
if (-not (Get-Command Invoke-SamyPrinterInstall -ErrorAction SilentlyContinue)) {
Write-LogHybrid "Printer install 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 Invoke-SamyPrinterInstall -ErrorAction SilentlyContinue)) {
Write-LogHybrid "SVSMSP installed but Invoke-SamyPrinterInstall still unavailable." Error Printers -LogToEvent
$Context.Response.StatusCode = 500
Send-Text $Context "SVSMSP installed but printer install commands still not available. Restart SAMY."
return
}
}
# Read JSON body
$rawBody = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
if (-not $rawBody) {
$Context.Response.StatusCode = 400
@@ -1883,31 +1918,21 @@ function Invoke-InstallPrinters {
$failures = @()
foreach ($p in $printers) {
# Expecting fields from JSON:
# ClientCode = 'ABC'
# ProfileName = 'FrontDesk'
# SetAsDefault = $true/$false (optional)
$clientCode = $p.ClientCode
$profileName = $p.ProfileName
$setDefault = $false
if ($p.PSObject.Properties.Name -contains 'SetAsDefault' -and $p.SetAsDefault) {
$setDefault = $true
}
$setDefault = [bool]($p.PSObject.Properties.Name -contains 'SetAsDefault' -and $p.SetAsDefault)
if (-not $clientCode -or -not $profileName) {
$msg = "Skipping printer entry because ClientCode or ProfileName is missing."
$msg = "Skipping printer entry: ClientCode or ProfileName missing."
Write-LogHybrid $msg Warning Printers -LogToEvent
$failures += $msg
continue
}
$summary = "ClientCode=$clientCode ProfileName=$profileName DisplayName=$($p.DisplayName) Location=$($p.Location) SetAsDefault=$setDefault"
$summary = "ClientCode=$clientCode ProfileName=$profileName SetAsDefault=$setDefault"
Write-LogHybrid "Installing printer ($summary)" Info Printers -LogToEvent
try {
# SAFE PHASE: we call with -WhatIf so no real change happens
Invoke-SamyPrinterInstall `
-ClientCode $clientCode `
-ProfileName $profileName `
@@ -1923,14 +1948,12 @@ function Invoke-InstallPrinters {
}
}
$result = @{
Send-JSON $Context @{
SuccessCount = $successCount
FailureCount = $failures.Count
Failures = $failures
Message = "Printer install (WHATIF) processed. Check SAMY logs for detail."
Message = "Printer install processed. Check SAMY logs for detail."
}
Send-JSON $Context $result
}
catch {
Write-LogHybrid "Invoke-InstallPrinters error: $($_.Exception.Message)" Error Printers -LogToEvent
@@ -1939,16 +1962,8 @@ function Invoke-InstallPrinters {
}
}
#region Printer core (local config + install)
# Per-session cache
$Script:Samy_PrinterProfiles = $null
#endregion Printer core (local config + install)
#endregion Printer handlers