diff --git a/samy.ps1 b/samy.ps1 index 83fcb15..f8f8206 100644 --- a/samy.ps1 +++ b/samy.ps1 @@ -1910,6 +1910,26 @@ function Install-DattoRMM { #endregion Install-DattoRMM + # --- Task handler resolver (prevents Get-Command $null + supports fallback naming) --- + function Get-TaskHandlerName { + param([object]$Task) + + # Preferred explicit properties (if your JSON provides them) + foreach ($p in @('HandlerFn','Handler','Fn')) { + if ($Task.PSObject.Properties.Name -contains $p) { + $v = [string]$Task.$p + if (-not [string]::IsNullOrWhiteSpace($v)) { return $v } + } + } + + # Fallback convention: Name="InstallChrome" -> Invoke-InstallChrome + $n = [string]$Task.Name + if (-not [string]::IsNullOrWhiteSpace($n)) { return "Invoke-$n" } + + return $null + } + + #region Dispatch-Request # Sends the HTML for a given page or invokes a task handler @@ -1966,26 +1986,33 @@ function Install-DattoRMM { } # ---- Task invocation ---- - $task = $Global:SamyTasks | Where-Object Name -EQ $path - if ($task) { - $fn = $task.HandlerFn - $cmd = Get-Command $fn -ErrorAction SilentlyContinue - if (-not $cmd) { - $Context.Response.StatusCode = 500 - Send-Text $Context "Handler not found: $fn" - return - } + $task = $Global:SamyTasks | Where-Object Name -EQ $path | Select-Object -First 1 + if ($task) { - # If the handler declares a Context parameter, pass it by name. - if ($cmd.Parameters.ContainsKey('Context')) { - & $fn -Context $Context - } - else { - & $fn - } + $fn = Get-TaskHandlerName -Task $task + if ([string]::IsNullOrWhiteSpace($fn)) { + $Context.Response.StatusCode = 500 + Send-Text $Context "Task '$($task.Label)' is missing a handler (HandlerFn/Handler/Fn/Name)." return } + $cmd = Get-Command -Name $fn -ErrorAction SilentlyContinue + if (-not $cmd) { + $Context.Response.StatusCode = 500 + Send-Text $Context "Handler not found: $fn" + return + } + + # If the handler declares a Context parameter, pass it by name. + if ($cmd.Parameters.ContainsKey('Context')) { + & $fn -Context $Context + } + else { + & $fn + } + return + } + # ---- 404 ---- $Context.Response.StatusCode = 404 Send-Text $Context '404 - Not Found' @@ -2066,17 +2093,32 @@ function Install-DattoRMM { } foreach ($task in $offboardTasks) { - try { + try { Write-LogHybrid "Running offboard task: $($task.Label)" Info OffBoard -LogToEvent - if (-not (Get-Command $task.HandlerFn -ErrorAction SilentlyContinue)) { - Write-LogHybrid "Missing handler $($task.HandlerFn)" Error OffBoard -LogToEvent - continue + + $fn = Get-TaskHandlerName -Task $task + if ([string]::IsNullOrWhiteSpace($fn)) { + Write-LogHybrid "Offboard task is missing a handler (Id=$($task.Id) Label='$($task.Label)')" Error OffBoard -LogToEvent + continue + } + + $cmd = Get-Command -Name $fn -ErrorAction SilentlyContinue + if (-not $cmd) { + Write-LogHybrid "Missing handler $fn (task '$($task.Label)')" Error OffBoard -LogToEvent + continue + } + + if ($cmd.Parameters.ContainsKey('Context')) { + & $fn -Context $null + } else { + & $fn } - & $task.HandlerFn $null - } catch { - Write-LogHybrid "Offboard task $($task.Label) failed: $($_.Exception.Message)" Error OffBoard -LogToEvent - } } + catch { + Write-LogHybrid "Offboard task $($task.Label) failed: $($_.Exception.Message)" Error OffBoard -LogToEvent + } + } + Write-LogHybrid "Headless offboarding completed" Success OffBoard -LogToEvent return