diff --git a/samy.ps1 b/samy.ps1 index 34c6072..e8dd834 100644 --- a/samy.ps1 +++ b/samy.ps1 @@ -1832,17 +1832,58 @@ function Invoke-InstallPrinters { return } + Write-LogHybrid "Received request to install $($printers.Count) printers (WHATIF mode)." Info Printers -LogToEvent + + $successCount = 0 + $failures = @() + 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 + # 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 + } + + if (-not $clientCode -or -not $profileName) { + $msg = "Skipping printer entry because ClientCode or ProfileName is missing." + Write-LogHybrid $msg Warning Printers -LogToEvent + $failures += $msg + continue + } + + $summary = "ClientCode=$clientCode ProfileName=$profileName SetAsDefault=$setDefault" + Write-LogHybrid "WHATIF: Installing printer ($summary)" Info Printers -LogToEvent + + try { + # 🔐 SAFE PHASE: we call with -WhatIf so no real change happens + Invoke-SamyPrinterInstall ` + -ClientCode $clientCode ` + -ProfileName $profileName ` + -SetAsDefault:$setDefault ` + -WhatIf + + $successCount++ + } + catch { + $errMsg = "Failed (WHATIF) to install printer ($summary): $($_.Exception.Message)" + Write-LogHybrid $errMsg Error Printers -LogToEvent + $failures += $errMsg + } } $result = @{ - Success = $true - Count = $printers.Count - Message = "Printer install requests logged on the SAMY server." + SuccessCount = $successCount + FailureCount = $failures.Count + Failures = $failures + Message = "Printer install (WHATIF) processed. Check SAMY logs for detail." } + Send-JSON $Context $result } catch {