From d8a2f64cd578e4746d05a2b818cff8403543d510 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Thu, 29 May 2025 01:27:19 -0400 Subject: [PATCH] Update StackMonkey.ps1 --- StackMonkey.ps1 | 137 +++++++++++++++++++++--------------------------- 1 file changed, 59 insertions(+), 78 deletions(-) diff --git a/StackMonkey.ps1 b/StackMonkey.ps1 index 56ab31e..72d0ebd 100644 --- a/StackMonkey.ps1 +++ b/StackMonkey.ps1 @@ -418,65 +418,52 @@ function Handle-InstallSVSHelpDesk { } function Handle-InstallDattoRMM { - param($Context) + param($Context) + $req = $Context.Request + $resp = $Context.Response - $request = $Context.Request - $response = $Context.Response + if ($req.HttpMethod -ne 'POST') { + $resp.StatusCode = 405; $resp.ContentType = 'text/plain' + $resp.OutputStream.Write([Text.Encoding]::UTF8.GetBytes('Use POST'),0,7) + $resp.OutputStream.Close(); return + } - if ($request.HttpMethod -ne "POST") { - $response.StatusCode = 405 - $response.ContentType = "text/plain" - $response.OutputStream.Write( - [Text.Encoding]::UTF8.GetBytes("Method not allowed. Use POST."), - 0, 29 - ) - $response.OutputStream.Close() - return - } + # parse JSON body + $body = (New-Object IO.StreamReader $req.InputStream).ReadToEnd() + $data = $body | ConvertFrom-Json + $checked = $data.checkedValues + $uid = $data.UID + $name = $data.Name - try { - $body = (New-Object IO.StreamReader $request.InputStream).ReadToEnd() - $requestData = $body | ConvertFrom-Json - $checked = $requestData.checkedValues - $UID = $requestData.UID - $Name = $requestData.Name + try { + Install-DattoRMM ` + -ApiUrl $Global:ApiUrl ` + -ApiKey $Global:ApiKey ` + -ApiSecretKey $Global:ApiSecretKey ` + -SiteUID $uid ` + -SiteName $name ` + -PushSiteVars:($checked -contains 'inputVar') ` + -InstallRMM: ($checked -contains 'rmm') ` + -SaveCopy: ($checked -contains 'exe') - if (-not $checked -or -not $UID -or -not $Name) { - throw "Missing required parameters" - } + Write-LogHybrid "RMM install triggered for $name" "Success" "DattoRMM" + $resp.StatusCode = 200 + $responseString = "Triggered DattoRMM for $name" + } + catch { + Write-LogHybrid "Error in Install-DattoRMM: $_" "Error" "DattoRMM" + $resp.StatusCode = 500 + $responseString = "ERROR: $($_.Exception.Message)" + } - # Build the command - $cmd = "Install-DattoRMM -ApiUrl '$ApiUrl' -ApiKey '$ApiKey' -ApiSecretKey '$ApiSecretKey' -SiteName '$Name' -SiteUID '$UID'" - if ($checked -contains 'inputVar') { $cmd += " -PushSiteVars" } - if ($checked -contains 'rmm') { $cmd += " -InstallRMM" } - if ($checked -contains 'exe') { $cmd += " -SaveCopy" } - - # Invoke and respond - try { - Invoke-Expression $cmd - Write-LogHybrid "RMM install triggered for $Name" "Success" "DattoRMM" - $response.StatusCode = 200 - $responseString = "RMM installation triggered successfully for $Name." - } catch { - Write-LogHybrid "Error triggering RMM install: $_" "Error" "DattoRMM" - $response.StatusCode = 500 - $responseString = "Error triggering RMM install: $_" - } - } - catch { - Write-LogHybrid "Bad request to /installDattoRMM: $_" "Error" "DattoRMM" - $response.StatusCode = 400 - $responseString = "Error: $($_.Exception.Message)" - } - - # write the response - $bytes = [Text.Encoding]::UTF8.GetBytes($responseString) - $response.ContentType = "text/plain" - $response.ContentLength64 = $bytes.Length - $response.OutputStream.Write($bytes, 0, $bytes.Length) - $response.OutputStream.Close() + $b = [Text.Encoding]::UTF8.GetBytes($responseString) + $resp.ContentType = 'text/plain' + $resp.ContentLength64 = $b.Length + $resp.OutputStream.Write($b,0,$b.Length) + $resp.OutputStream.Close() } + # Off-boarding handlers function Handle-UninstallCyberQP { param($Context) @@ -856,40 +843,34 @@ $style = @' const cb = document.getElementById(t.id); if (!cb || !cb.checked) continue; - // special-case DattoRMM: POST JSON with sub-options + site info + // special case: DattoRMM is POST if (t.id === 'installDattoRMM') { - const checkedValues = Array.from( - document.querySelectorAll('.sub-option-installDattoRMM') - ) - .filter(sub => sub.checked) - .map(sub => sub.value); - + const sub = Array.from( + document.querySelectorAll('.sub-option-installDattoRMM:checked') + ).map(x=>x.value); const dropdown = document.getElementById('dattoDropdown'); - const UID = dropdown.value; - const Name = dropdown.options[dropdown.selectedIndex].text; + const uid = dropdown.value; + const name = dropdown.selectedOptions[0].text; - try { - await fetch('/installDattoRMM', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ checkedValues, UID, Name }) - }); - } catch (e) { - console.error(`Error running ${t.label}:`, e); - } - - } else { - // everything else remains a simple GET - try { - await fetch(t.handler, { method: 'GET' }); - } catch (e) { - console.error(`Error running ${t.label}:`, e); - } + await fetch('/installDattoRMM', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + checkedValues: sub, + UID: uid, + Name: name + }) + }); + } + else { + // your existing GET for everyone else + await fetch(t.handler, { method: 'GET' }); } } } + // ======================================================================= // Shutdown Handler // =======================================================================