Update StackMonkey.ps1

This commit is contained in:
2025-05-29 01:27:19 -04:00
parent e37eccee17
commit d8a2f64cd5

View File

@@ -419,64 +419,51 @@ function Handle-InstallSVSHelpDesk {
function Handle-InstallDattoRMM {
param($Context)
$req = $Context.Request
$resp = $Context.Response
$request = $Context.Request
$response = $Context.Response
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
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
}
# 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
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"
}
# 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: $_"
}
Write-LogHybrid "RMM install triggered for $name" "Success" "DattoRMM"
$resp.StatusCode = 200
$responseString = "Triggered DattoRMM for $name"
}
catch {
Write-LogHybrid "Bad request to /installDattoRMM: $_" "Error" "DattoRMM"
$response.StatusCode = 400
$responseString = "Error: $($_.Exception.Message)"
Write-LogHybrid "Error in Install-DattoRMM: $_" "Error" "DattoRMM"
$resp.StatusCode = 500
$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,38 +843,32 @@ $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 })
body: JSON.stringify({
checkedValues: sub,
UID: uid,
Name: name
})
});
} catch (e) {
console.error(`Error running ${t.label}:`, e);
}
else {
// your existing GET for everyone else
await fetch(t.handler, { method: 'GET' });
}
}
}
} else {
// everything else remains a simple GET
try {
await fetch(t.handler, { method: 'GET' });
} catch (e) {
console.error(`Error running ${t.label}:`, e);
}
}
}
}
// =======================================================================