Update SVSTaskGate.ps1

This commit is contained in:
2025-01-05 03:08:57 -05:00
parent 30514341f3
commit 23577263e6

View File

@@ -703,23 +703,17 @@ function GetHtmlContent {
const checkedValues = Array.from(DattoRMMCheckbox).map(c => c.value); const checkedValues = Array.from(DattoRMMCheckbox).map(c => c.value);
// Predefined PowerShell global variables passed into JavaScript const payload = {
// const ApiUrl = "$global:ApiUrl"; checkedValues, // Array of selected checkbox values
// const ApiKey = "$global:ApiKey"; UID, // Selected site UID
// const ApiSecretKey = "$global:ApiSecretKey"; Name, // Selected site name
};
// let installRMMCommand = `Install-DattoRMM -ApiUrl '${ApiUrl}' -ApiKey '${ApiKey}' -ApiSecretKey '${ApiSecretKey}'`; fetch('/installrmm', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
if (checkedValues.includes('inputVar')) { body: JSON.stringify(payload),
installRMMCommand += ' -PushSiteVars'; });
}
if (checkedValues.includes('rmm')) {
installRMMCommand += ' -InstallRMM';
}
if (checkedValues.includes('exe')) {
installRMMCommand += ' -SaveCopy';
}
fetch('/installrmm', { fetch('/installrmm', {
method: 'POST', method: 'POST',
@@ -867,38 +861,81 @@ try {
"/installrmm" { "/installrmm" {
if ($request.HttpMethod -eq "POST") { if ($request.HttpMethod -eq "POST") {
try {
# Step 1: Read the Request Body
$bodyStream = New-Object IO.StreamReader $request.InputStream $bodyStream = New-Object IO.StreamReader $request.InputStream
$body = $bodyStream.ReadToEnd() $body = $bodyStream.ReadToEnd()
$requestData = ConvertFrom-Json $body $requestData = ConvertFrom-Json $body
$installRMMCommand = $requestData.installRMMCommand # Step 2: Extract Data from the Request
$checkedValues = $requestData.checkedValues
$UID = $requestData.UID $UID = $requestData.UID
$Name = $requestData.Name $Name = $requestData.Name
# Log the received command # Step 3: Validate Input
Write-LogHybrid -Message "Received command: $installRMMCommand for UID: $UID, Name: $Name" -Level "Info" if (-not $checkedValues -or -not $UID -or -not $Name) {
Write-LogHybrid -Message "Invalid input received. Missing required parameters: UID, Name, or checkbox values." -Level "Error"
$response.StatusCode = 400
$responseString = "Error: Missing required input parameters."
$buffer = [System.Text.Encoding]::UTF8.GetBytes($responseString)
$response.OutputStream.Write($buffer, 0, $buffer.Length)
$response.OutputStream.Close()
return
}
# Step 4: Build the PowerShell Command Dynamically
$installRMMCommand = "Install-DattoRMM -ApiUrl '$ApiUrl' -ApiKey '$ApiKey' -ApiSecretKey '$ApiSecretKey'"
if ($checkedValues -contains 'inputVar') {
$installRMMCommand += " -PushSiteVars"
}
if ($checkedValues -contains 'rmm') {
$installRMMCommand += " -InstallRMM"
}
if ($checkedValues -contains 'exe') {
$installRMMCommand += " -SaveCopy"
}
Write-LogHybrid -Message "Constructed Command: $installRMMCommand for UID: $UID, Name: $Name" -Level "Info"
# Step 5: Execute the Command
try { try {
Invoke-Expression $installRMMCommand Invoke-Expression $installRMMCommand
$responseString = "RMM install triggered successfully for UID: $UID, Name: $Name." $responseString = "RMM installation triggered successfully for UID: $UID, Name: $Name."
Write-LogHybrid -Message $responseString -Level "Success"
$response.StatusCode = 200
} catch {
$responseString = "Error triggering RMM installation: $($_.Exception.Message)"
Write-LogHybrid -Message $responseString -Level "Error"
$response.StatusCode = 500
} }
catch { } catch {
$responseString = "Error triggering RMM install: $($_.Exception.Message)" # Log General Errors
$errorString = "An error occurred while processing the /installrmm request: $($_.Exception.Message)"
Write-LogHybrid -Message $errorString -Level "Error"
$response.StatusCode = 500
$responseString = $errorString
} }
# Step 6: Return the Response
$buffer = [System.Text.Encoding]::UTF8.GetBytes($responseString) $buffer = [System.Text.Encoding]::UTF8.GetBytes($responseString)
$response.ContentType = "text/plain" $response.ContentType = "text/plain"
$response.ContentLength64 = $buffer.Length $response.ContentLength64 = $buffer.Length
$response.OutputStream.Write($buffer, 0, $buffer.Length) $response.OutputStream.Write($buffer, 0, $buffer.Length)
$response.OutputStream.Close() $response.OutputStream.Close()
} else {
# Handle Invalid HTTP Methods
$response.StatusCode = 405
$response.StatusDescription = "Method Not Allowed"
$responseString = "Error: Only POST requests are allowed."
$buffer = [System.Text.Encoding]::UTF8.GetBytes($responseString)
$response.OutputStream.Write($buffer, 0, $buffer.Length)
$response.OutputStream.Close()
} }
} }
"/setSVSPowerplan" { "/setSVSPowerplan" {
if ($request.HttpMethod -eq "GET") { if ($request.HttpMethod -eq "GET") {
Set-SVSPowerPlan Set-SVSPowerPlan