From e7d65a68d5b468693d2b21b5edecc71ed0dd82b8 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Sun, 29 Jun 2025 14:36:03 -0400 Subject: [PATCH] move function ahead of switch --- StackMonkey.ps1 | 247 ++++++++++++++++++++++++------------------------ 1 file changed, 125 insertions(+), 122 deletions(-) diff --git a/StackMonkey.ps1 b/StackMonkey.ps1 index a899b7f..e114fb5 100644 --- a/StackMonkey.ps1 +++ b/StackMonkey.ps1 @@ -135,128 +135,6 @@ [Parameter(ParameterSetName='DattoInstall')][switch] $SaveCopy ) - #region ScriptMonkey run silently Entrypoint - - # ───────────────────────────────────────────────────────────────────────── - # 3) MAIN LOGIC (Toolkit vs DattoFetch vs DattoInstall vs UI) - # ───────────────────────────────────────────────────────────────────────── - - switch ($PSCmdlet.ParameterSetName) { - 'Toolkit' { - Write-LogHybrid "Toolkit-only mode" Info Startup - Install-SVSMSP -InstallToolkit - return - } - - 'Cleanup' { - Write-LogHybrid "Running Toolkit cleanup mode" Info Startup - Install-SVSMSP -Cleanup - return - } - - # ─────────────────────────────────────────────────────────── - # 2) If user only wants the site list, do that and exit - # ─────────────────────────────────────────────────────────── - - 'DattoFetch' { - Write-LogHybrid "Fetching site list only…" Info DattoAuth - $sites = Get-DattoRmmSites -Password $N8nPassword - - $ext = [IO.Path]::GetExtension($OutputFile).ToLower() - if ($ext -eq '.json') { - $sites | ConvertTo-Json -Depth 3 | Out-File -FilePath $OutputFile -Encoding UTF8 - } else { - $sites | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8 - } - - Write-LogHybrid "Wrote $($sites.Count) sites to $OutputFile" Success DattoAuth - return - } - - # ──────────────────────────────────────────── - # 3) Invoke the existing Install-DattoRMM cmdlet - # ──────────────────────────────────────────── - - 'DattoInstall' { - Write-LogHybrid "Headless DattoRMM deploy" Info DattoAuth - if ($PSCmdlet.ShouldProcess("Datto site '$SiteName'", "Headless install")) { - Install-DattoRMM ` - -ApiUrl $Global:ApiUrl ` - -ApiKey $Global:ApiKey ` - -ApiSecretKey $Global:ApiSecretKey ` - -SiteUID $SiteUID ` - -SiteName $SiteName ` - -PushSiteVars:$PushSiteVars ` - -InstallRMM:$InstallRMM ` - -SaveCopy:$SaveCopy - } - - return - - } - - 'UI' { - Write-Host "Launching UI" Info Startup -ForegroundColor Cyan - Write-Host "Starting ScriptMonkey UI on http://localhost:$Port/" -ForegroundColor Cyan - Start-Process "msedge.exe" -ArgumentList "--app=http://localhost:$Port" - Start-Server # blocks until you click Exit - return - } - - } - #endregion ScriptMonkey run silently Entrypoint - - Write-Host "🛠️ SAMY - Script Automation Monkey (Yeah!)" -ForegroundColor Cyan - Write-Host "ParameterSetName: $($PSCmdlet.ParameterSetName)" -ForegroundColor Yellow - - #region — guarantee NuGet provider is present without prompting - - # ─── Top of script ─── - Import-Module PackageManagement -Force -ErrorAction SilentlyContinue | Out-Null - Import-Module PowerShellGet -Force -ErrorAction SilentlyContinue | Out-Null - - # ─── ensure TLS 1.2 + no prompts ─── - [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 - $ProgressPreference = 'SilentlyContinue' - $ConfirmPreference = 'None' - - # check if NuGet exists (no output—assigned to $nuget) - $nuget = Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction SilentlyContinue - - if (-not $nuget) { - # install it (again, assignment suppresses the table) - Install-PackageProvider ` - -Name NuGet ` - -MinimumVersion 2.8.5.201 ` - -Force ` - -Confirm:$false - - - # re-query just for version info - $found = Get-PackageProvider -Name NuGet -ListAvailable - Write-Host "Installed NuGet provider v$($found.Version)" -ForegroundColor Green - } - else { - Write-Host "NuGet provider already present (v$($found.Version))" -ForegroundColor DarkGray - } - - # now import it silently - Import-PackageProvider -Name NuGet -Force -ErrorAction SilentlyContinue | Out-Null - - # ensure trust PSGallery without its own output (so you don't get “untrusted repository” prompt - $gallery = Get-PSRepository -Name PSGallery -ErrorAction SilentlyContinue - if ($gallery.InstallationPolicy -ne 'Trusted') { - Set-PSRepository ` - -Name PSGallery ` - -InstallationPolicy Trusted ` - -ErrorAction SilentlyContinue | Out-Null - - Write-Host "PSGallery marked as Trusted" -ForegroundColor Green - } - - #endregion - - # ───────────────────────────────────────────────────────────────────────── # 2) GLOBAL SETTINGS & HELPERS # ───────────────────────────────────────────────────────────────────────── @@ -435,6 +313,131 @@ #endregion + + #region ScriptMonkey run silently Entrypoint + + # ───────────────────────────────────────────────────────────────────────── + # 3) MAIN LOGIC (Toolkit vs DattoFetch vs DattoInstall vs UI) + # ───────────────────────────────────────────────────────────────────────── + + switch ($PSCmdlet.ParameterSetName) { + 'Toolkit' { + Write-LogHybrid "Toolkit-only mode" Info Startup + Install-SVSMSP -InstallToolkit + return + } + + 'Cleanup' { + Write-LogHybrid "Running Toolkit cleanup mode" Info Startup + Install-SVSMSP -Cleanup + return + } + + # ─────────────────────────────────────────────────────────── + # 2) If user only wants the site list, do that and exit + # ─────────────────────────────────────────────────────────── + + 'DattoFetch' { + Write-LogHybrid "Fetching site list only…" Info DattoAuth + $sites = Get-DattoRmmSites -Password $N8nPassword + + $ext = [IO.Path]::GetExtension($OutputFile).ToLower() + if ($ext -eq '.json') { + $sites | ConvertTo-Json -Depth 3 | Out-File -FilePath $OutputFile -Encoding UTF8 + } else { + $sites | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8 + } + + Write-LogHybrid "Wrote $($sites.Count) sites to $OutputFile" Success DattoAuth + return + } + + # ──────────────────────────────────────────── + # 3) Invoke the existing Install-DattoRMM cmdlet + # ──────────────────────────────────────────── + + 'DattoInstall' { + Write-LogHybrid "Headless DattoRMM deploy" Info DattoAuth + if ($PSCmdlet.ShouldProcess("Datto site '$SiteName'", "Headless install")) { + Install-DattoRMM ` + -ApiUrl $Global:ApiUrl ` + -ApiKey $Global:ApiKey ` + -ApiSecretKey $Global:ApiSecretKey ` + -SiteUID $SiteUID ` + -SiteName $SiteName ` + -PushSiteVars:$PushSiteVars ` + -InstallRMM:$InstallRMM ` + -SaveCopy:$SaveCopy + } + + return + + } + + 'UI' { + Write-Host "Launching UI" Info Startup -ForegroundColor Cyan + Write-Host "Starting ScriptMonkey UI on http://localhost:$Port/" -ForegroundColor Cyan + Start-Process "msedge.exe" -ArgumentList "--app=http://localhost:$Port" + Start-Server # blocks until you click Exit + return + } + + } + #endregion ScriptMonkey run silently Entrypoint + + Write-Host "🛠️ SAMY - Script Automation Monkey (Yeah!)" -ForegroundColor Cyan + Write-Host "ParameterSetName: $($PSCmdlet.ParameterSetName)" -ForegroundColor Yellow + + #region — guarantee NuGet provider is present without prompting + + # ─── Top of script ─── + Import-Module PackageManagement -Force -ErrorAction SilentlyContinue | Out-Null + Import-Module PowerShellGet -Force -ErrorAction SilentlyContinue | Out-Null + + # ─── ensure TLS 1.2 + no prompts ─── + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + $ProgressPreference = 'SilentlyContinue' + $ConfirmPreference = 'None' + + # check if NuGet exists (no output—assigned to $nuget) + $nuget = Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction SilentlyContinue + + if (-not $nuget) { + # install it (again, assignment suppresses the table) + Install-PackageProvider ` + -Name NuGet ` + -MinimumVersion 2.8.5.201 ` + -Force ` + -Confirm:$false + + + # re-query just for version info + $found = Get-PackageProvider -Name NuGet -ListAvailable + Write-Host "Installed NuGet provider v$($found.Version)" -ForegroundColor Green + } + else { + Write-Host "NuGet provider already present (v$($found.Version))" -ForegroundColor DarkGray + } + + # now import it silently + Import-PackageProvider -Name NuGet -Force -ErrorAction SilentlyContinue | Out-Null + + # ensure trust PSGallery without its own output (so you don't get “untrusted repository” prompt + $gallery = Get-PSRepository -Name PSGallery -ErrorAction SilentlyContinue + if ($gallery.InstallationPolicy -ne 'Trusted') { + Set-PSRepository ` + -Name PSGallery ` + -InstallationPolicy Trusted ` + -ErrorAction SilentlyContinue | Out-Null + + Write-Host "PSGallery marked as Trusted" -ForegroundColor Green + } + + #endregion + + + + #region Config & Task Definitions # Define every task once here: