diff --git a/samy.ps1 b/samy.ps1 index 2127db9..be31959 100644 --- a/samy.ps1 +++ b/samy.ps1 @@ -1111,6 +1111,7 @@ $cssContent +
@@ -1198,6 +1199,48 @@ $cssContent + + +
+

Devices

+

Manage printers and other client devices.

+ + +
+ +
+ + +
+
+ + + + + + +
@@ -1644,6 +1687,111 @@ function Invoke-CleanupSVSMSP { #endregion Offboarding handlers +#region Printer handlers + +function Invoke-GetPrinters { + param($Context) + + try { + if ($Context.Request.HttpMethod -ne 'POST') { + $Context.Response.StatusCode = 405 + Send-Text $Context 'Use POST' + return + } + + # Read JSON body: { "password": "..." } + $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 = $body.password + if (-not $password) { + $Context.Response.StatusCode = 400 + Send-Text $Context 'Password is required.' + return + } + + $uri = 'https://bananas.svstools.ca/getprinters' + Write-LogHybrid "Fetching printers from $uri" Info Printers -LogToEvent + + # NOTE: We never log the actual password + $printers = Get-SamyClientListFromServer -Uri $uri -Password $password + + # Return raw objects as JSON; JS will filter/group + 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 + } + + foreach ($p in $printers) { + # TODO: Replace this with your real install logic once ready + $msg = "Requested install: ClientCode=$($p.ClientCode) ProfileName=$($p.ProfileName) DisplayName=$($p.DisplayName) Location=$($p.Location)" + Write-LogHybrid $msg Info Printers -LogToEvent + } + + $result = @{ + Success = $true + Count = $printers.Count + Message = "Printer install requests logged on the SAMY server." + } + Send-JSON $Context $result + } + catch { + Write-LogHybrid "Invoke-InstallPrinters error: $($_.Exception.Message)" Error Printers -LogToEvent + $Context.Response.StatusCode = 500 + Send-Text $Context "Internal server error installing printers." + } +} + +#endregion Printer handlers + #endregion Handler Stubs @@ -1905,6 +2053,17 @@ function Install-DattoRMM { return } + # ---- Printer endpoints ---- + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'getprinters') { + Invoke-GetPrinters $Context + return + } + + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'installprinters') { + Invoke-InstallPrinters $Context + return + } + # ---- Serve UI pages ---- if ($path -in @('', 'onboard', 'offboard', 'tweaks', 'SVSApps')) { $page = if ($path -eq '') { 'onboard' } else { $path }