Update samy.ps1
This commit is contained in:
105
samy.ps1
105
samy.ps1
@@ -269,6 +269,10 @@ $ConfirmPreference = 'None'
|
|||||||
$Script:SamyBranch = 'main' # 'beta'
|
$Script:SamyBranch = 'main' # 'beta'
|
||||||
$Script:SamyRepoBase = 'https://git.svstools.ca/SVS_Public_Repo/SAMY/raw/branch'
|
$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)
|
# Top-left corner logo (SVS)
|
||||||
$Script:SamyTopLogoUrl = "$Script:SamyRepoBase/$Script:SamyBranch/SVS_logo.svg"
|
$Script:SamyTopLogoUrl = "$Script:SamyRepoBase/$Script:SamyBranch/SVS_logo.svg"
|
||||||
@@ -1771,14 +1775,6 @@ function Invoke-CleanupSVSMSP {
|
|||||||
#region Printer handlers
|
#region Printer handlers
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function Invoke-GetPrinters {
|
function Invoke-GetPrinters {
|
||||||
param($Context)
|
param($Context)
|
||||||
|
|
||||||
@@ -1789,6 +1785,30 @@ function Invoke-GetPrinters {
|
|||||||
return
|
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": "..." }
|
# Read JSON body: { "password": "..." }
|
||||||
$rawBody = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
|
$rawBody = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
|
||||||
if (-not $rawBody) {
|
if (-not $rawBody) {
|
||||||
@@ -1815,19 +1835,9 @@ function Invoke-GetPrinters {
|
|||||||
$uri = 'https://bananas.svstools.ca/getprinters'
|
$uri = 'https://bananas.svstools.ca/getprinters'
|
||||||
Write-LogHybrid "Fetching printers from $uri" Info Printers -LogToEvent
|
Write-LogHybrid "Fetching printers from $uri" Info Printers -LogToEvent
|
||||||
|
|
||||||
# NOTE: We never log the actual password
|
|
||||||
$printers = Get-SvsPrinterProfilesFromServer -Uri $uri -Password $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 {
|
try {
|
||||||
Set-SvsPrinterLocalConfig -PrinterProfiles $printers -SkipIfEmpty
|
Set-SvsPrinterLocalConfig -PrinterProfiles $printers -SkipIfEmpty
|
||||||
}
|
}
|
||||||
@@ -1835,7 +1845,6 @@ function Invoke-GetPrinters {
|
|||||||
Write-LogHybrid "Set-SvsPrinterLocalConfig failed: $($_.Exception.Message)" Warning Printers -LogToEvent
|
Write-LogHybrid "Set-SvsPrinterLocalConfig failed: $($_.Exception.Message)" Warning Printers -LogToEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
# Return raw objects as JSON; JS will filter/group
|
|
||||||
Send-JSON $Context $printers
|
Send-JSON $Context $printers
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
@@ -1845,6 +1854,7 @@ function Invoke-GetPrinters {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function Invoke-InstallPrinters {
|
function Invoke-InstallPrinters {
|
||||||
param($Context)
|
param($Context)
|
||||||
|
|
||||||
@@ -1855,6 +1865,31 @@ function Invoke-InstallPrinters {
|
|||||||
return
|
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()
|
$rawBody = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
|
||||||
if (-not $rawBody) {
|
if (-not $rawBody) {
|
||||||
$Context.Response.StatusCode = 400
|
$Context.Response.StatusCode = 400
|
||||||
@@ -1883,31 +1918,21 @@ function Invoke-InstallPrinters {
|
|||||||
$failures = @()
|
$failures = @()
|
||||||
|
|
||||||
foreach ($p in $printers) {
|
foreach ($p in $printers) {
|
||||||
# Expecting fields from JSON:
|
|
||||||
# ClientCode = 'ABC'
|
|
||||||
# ProfileName = 'FrontDesk'
|
|
||||||
# SetAsDefault = $true/$false (optional)
|
|
||||||
|
|
||||||
$clientCode = $p.ClientCode
|
$clientCode = $p.ClientCode
|
||||||
$profileName = $p.ProfileName
|
$profileName = $p.ProfileName
|
||||||
$setDefault = $false
|
$setDefault = [bool]($p.PSObject.Properties.Name -contains 'SetAsDefault' -and $p.SetAsDefault)
|
||||||
|
|
||||||
if ($p.PSObject.Properties.Name -contains 'SetAsDefault' -and $p.SetAsDefault) {
|
|
||||||
$setDefault = $true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (-not $clientCode -or -not $profileName) {
|
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
|
Write-LogHybrid $msg Warning Printers -LogToEvent
|
||||||
$failures += $msg
|
$failures += $msg
|
||||||
continue
|
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
|
Write-LogHybrid "Installing printer ($summary)" Info Printers -LogToEvent
|
||||||
|
|
||||||
try {
|
try {
|
||||||
# SAFE PHASE: we call with -WhatIf so no real change happens
|
|
||||||
Invoke-SamyPrinterInstall `
|
Invoke-SamyPrinterInstall `
|
||||||
-ClientCode $clientCode `
|
-ClientCode $clientCode `
|
||||||
-ProfileName $profileName `
|
-ProfileName $profileName `
|
||||||
@@ -1923,14 +1948,12 @@ function Invoke-InstallPrinters {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = @{
|
Send-JSON $Context @{
|
||||||
SuccessCount = $successCount
|
SuccessCount = $successCount
|
||||||
FailureCount = $failures.Count
|
FailureCount = $failures.Count
|
||||||
Failures = $failures
|
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 {
|
catch {
|
||||||
Write-LogHybrid "Invoke-InstallPrinters error: $($_.Exception.Message)" Error Printers -LogToEvent
|
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
|
#endregion Printer handlers
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user