diff --git a/samy.ps1 b/samy.ps1 index dc32c40..5f6d192 100644 --- a/samy.ps1 +++ b/samy.ps1 @@ -2019,50 +2019,39 @@ function Install-DattoRMM { #region EntryPoint: Define Invoke-ScriptAutomationMonkey - # ───────────────────────────────────────────────────────────────────────── - # 3) MAIN LOGIC (Toolkit vs DattoFetch vs DattoInstall vs UI) - # ───────────────────────────────────────────────────────────────────────── - + switch ($PSCmdlet.ParameterSetName) { - 'Toolkit' { - Write-LogHybrid "Toolkit-only mode" Info Startup -LogToEvent - Install-SVSMSP -InstallToolkit - return - } - 'Cleanup' { - Write-LogHybrid "Running Toolkit cleanup mode" Info Startup -LogToEvent - Install-SVSMSP -Cleanup - return - } - - # ─────────────────────────────────────────────────────────── - # 2) If user only wants the site list, do that and exit - # ─────────────────────────────────────────────────────────── + 'Toolkit' { + Write-LogHybrid "Toolkit-only mode" Info Startup -LogToEvent + Install-SVSMSP -InstallToolkit + return + } - 'DattoFetch' { - Write-LogHybrid "Fetching site list only…" Info DattoAuth -LogToEvent - $sites = Install-DattoRMM ` - -UseWebhook ` - -WebhookPassword $WebhookPassword ` - -FetchSites ` - -SaveSitesList:$SaveSitesList ` - -OutputFile $OutputFile + 'Cleanup' { + Write-LogHybrid "Running Toolkit cleanup mode" Info Startup -LogToEvent + Install-SVSMSP -Cleanup + return + } - Write-LogHybrid "Done." Success DattoAuth -LogToEvent - return - } + 'DattoFetch' { + Write-LogHybrid "Fetching site list only…" Info DattoAuth -LogToEvent + $sites = Install-DattoRMM ` + -UseWebhook ` + -WebhookPassword $WebhookPassword ` + -FetchSites ` + -SaveSitesList:$SaveSitesList ` + -OutputFile $OutputFile + Write-LogHybrid "Done." Success DattoAuth -LogToEvent + return + } - # ──────────────────────────────────────────── - # 3) Invoke the existing Install-DattoRMM cmdlet - # ──────────────────────────────────────────── - - 'DattoInstall' { - Write-LogHybrid "Headless DattoRMM deploy" Info DattoAuth -LogToEvent + 'DattoInstall' { + Write-LogHybrid "Headless DattoRMM deploy" Info DattoAuth -LogToEvent - if ($PSCmdlet.ShouldProcess("Datto site '$SiteName'", "Headless install")) { - Install-DattoRMM ` + if ($PSCmdlet.ShouldProcess("Datto site '$SiteName'", "Headless install")) { + Install-DattoRMM ` -UseWebhook ` -WebhookPassword $WebhookPassword ` -SiteUID $SiteUID ` @@ -2070,103 +2059,95 @@ function Install-DattoRMM { -PushSiteVars:$PushSiteVars ` -InstallRMM:$InstallRMM ` -SaveCopy:$SaveCopy - } + } - return + return + } - } - - 'Offboard' { + 'Offboard' { Write-LogHybrid "Headless offboarding requested" Info OffBoard -LogToEvent - # Ne garde que les tâches réellement exécutables $offboardTasks = $Global:SamyTasks | Where-Object { - $_.Page -eq 'offboard' -and -not [string]::IsNullOrWhiteSpace([string]$_.Name) + $_.Page -eq 'offboard' -and -not [string]::IsNullOrWhiteSpace([string]$_.Name) } if (-not $offboardTasks) { - Write-LogHybrid "No offboard tasks configured (or none with a Name)." Warning OffBoard -LogToEvent - return + Write-LogHybrid "No offboard tasks configured (or none with a Name)." Warning OffBoard -LogToEvent + return } if (-not $PSCmdlet.ShouldProcess("Full off-boarding flow", "Execute every offboard task")) { - return + return } - foreach ($task in $offboardTasks) { - try { - Write-LogHybrid "Running offboard task: $($task.Label)" Info OffBoard -LogToEvent + foreach ($task in $offboardTasks) { + try { + Write-LogHybrid "Running offboard task: $($task.Label)" Info OffBoard -LogToEvent - $fn = Get-TaskHandlerName -Task $task - if ([string]::IsNullOrWhiteSpace($fn)) { - Write-LogHybrid "Skipping task with missing handler (Id=$($task.Id) Name='$($task.Name)' Label='$($task.Label)')" Error OffBoard -LogToEvent - continue - } - - $cmd = Get-Command -Name $fn -ErrorAction SilentlyContinue - if (-not $cmd) { - Write-LogHybrid "Skipping task: handler not found '$fn' (task '$($task.Label)')" Error OffBoard -LogToEvent - continue - } - - if ($cmd.Parameters.ContainsKey('Context')) { - & $fn -Context $null - } else { - & $fn - } - } - catch { - Write-LogHybrid "Offboard task '$($task.Label)' failed: $($_.Exception.Message)" Error OffBoard -LogToEvent - } + $fn = Get-TaskHandlerName -Task $task + if ([string]::IsNullOrWhiteSpace($fn)) { + Write-LogHybrid "Skipping task with missing handler (Id=$($task.Id) Name='$($task.Name)' Label='$($task.Label)')" Error OffBoard -LogToEvent + continue } - Write-LogHybrid "Headless offboarding completed" Success OffBoard -LogToEvent - return - } - - - Write-LogHybrid "Headless offboarding completed" Success OffBoard -LogToEvent - return - } - - - 'UI' { - $url = "http://localhost:$Port/" - Write-LogHybrid "Starting ScriptAutomationMonkey UI on $url" Info Startup - - # Resolve Edge path explicitly (x86 first, then 64-bit, then PATH) - $edgeCandidates = @( - "${env:ProgramFiles(x86)}\Microsoft\Edge\Application\msedge.exe", - "$env:ProgramFiles\Microsoft\Edge\Application\msedge.exe" - ) - $edgePath = $edgeCandidates | Where-Object { $_ -and (Test-Path $_) } | Select-Object -First 1 - if (-not $edgePath) { - $cmd = Get-Command -Name 'msedge.exe' -ErrorAction SilentlyContinue - if ($cmd) { $edgePath = $cmd.Path } + $cmd = Get-Command -Name $fn -ErrorAction SilentlyContinue + if (-not $cmd) { + Write-LogHybrid "Skipping task: handler not found '$fn' (task '$($task.Label)')" Error OffBoard -LogToEvent + continue } - # Launch Edge (app mode) in a background job so Start-Server can block - Start-Job -Name 'OpenScriptAutomationMonkeyUI' -ScriptBlock { - param([string]$u, [string]$edge) - Start-Sleep -Milliseconds 400 - try { - if ($edge -and (Test-Path $edge)) { - Start-Process -FilePath $edge -ArgumentList @('--new-window', "--app=$u") - } else { - Start-Process -FilePath $u # fallback to default browser - } - } catch { } - } -ArgumentList $url, $edgePath | Out-Null - - # Now start the blocking listener loop - Start-Server - return + if ($cmd.Parameters.ContainsKey('Context')) { + & $fn -Context $null + } else { + & $fn + } + } + catch { + Write-LogHybrid "Offboard task '$($task.Label)' failed: $($_.Exception.Message)" Error OffBoard -LogToEvent } - - - } - #endregion EntryPoint: Define Invoke-ScriptAutomationMonkey + + Write-LogHybrid "Headless offboarding completed" Success OffBoard -LogToEvent + return + } + + 'UI' { + $url = "http://localhost:$Port/" + Write-LogHybrid "Starting ScriptAutomationMonkey UI on $url" Info Startup + + $edgeCandidates = @( + "${env:ProgramFiles(x86)}\Microsoft\Edge\Application\msedge.exe", + "$env:ProgramFiles\Microsoft\Edge\Application\msedge.exe" + ) + $edgePath = $edgeCandidates | Where-Object { $_ -and (Test-Path $_) } | Select-Object -First 1 + if (-not $edgePath) { + $cmd = Get-Command -Name 'msedge.exe' -ErrorAction SilentlyContinue + if ($cmd) { $edgePath = $cmd.Path } + } + + Start-Job -Name 'OpenScriptAutomationMonkeyUI' -ScriptBlock { + param([string]$u, [string]$edge) + Start-Sleep -Milliseconds 400 + try { + if ($edge -and (Test-Path $edge)) { + Start-Process -FilePath $edge -ArgumentList @('--new-window', "--app=$u") + } else { + Start-Process -FilePath $u + } + } catch { } + } -ArgumentList $url, $edgePath | Out-Null + + Start-Server + return + } + + default { + Write-LogHybrid "Unknown ParameterSetName '$($PSCmdlet.ParameterSetName)'" Error Startup -LogToEvent + throw "Unknown mode." + } +} + + #endregion EntryPoint: Define Invoke-ScriptAutomationMonkey }