diff --git a/SVSTaskGate.ps1 b/SVSTaskGate.ps1 index c38a999..2ff1f9a 100644 --- a/SVSTaskGate.ps1 +++ b/SVSTaskGate.ps1 @@ -703,23 +703,17 @@ function GetHtmlContent { const checkedValues = Array.from(DattoRMMCheckbox).map(c => c.value); - // Predefined PowerShell global variables passed into JavaScript - // const ApiUrl = "$global:ApiUrl"; - // const ApiKey = "$global:ApiKey"; - // const ApiSecretKey = "$global:ApiSecretKey"; + const payload = { + checkedValues, // Array of selected checkbox values + UID, // Selected site UID + Name, // Selected site name + }; - // let installRMMCommand = `Install-DattoRMM -ApiUrl '${ApiUrl}' -ApiKey '${ApiKey}' -ApiSecretKey '${ApiSecretKey}'`; - - - if (checkedValues.includes('inputVar')) { - installRMMCommand += ' -PushSiteVars'; - } - if (checkedValues.includes('rmm')) { - installRMMCommand += ' -InstallRMM'; - } - if (checkedValues.includes('exe')) { - installRMMCommand += ' -SaveCopy'; - } + fetch('/installrmm', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(payload), + }); fetch('/installrmm', { method: 'POST', @@ -867,36 +861,79 @@ try { "/installrmm" { if ($request.HttpMethod -eq "POST") { - $bodyStream = New-Object IO.StreamReader $request.InputStream - $body = $bodyStream.ReadToEnd() - $requestData = ConvertFrom-Json $body - - $installRMMCommand = $requestData.installRMMCommand - $UID = $requestData.UID - $Name = $requestData.Name - - # Log the received command - Write-LogHybrid -Message "Received command: $installRMMCommand for UID: $UID, Name: $Name" -Level "Info" - try { - Invoke-Expression $installRMMCommand - $responseString = "RMM install triggered successfully for UID: $UID, Name: $Name." - } - catch { - $responseString = "Error triggering RMM install: $($_.Exception.Message)" + # Step 1: Read the Request Body + $bodyStream = New-Object IO.StreamReader $request.InputStream + $body = $bodyStream.ReadToEnd() + $requestData = ConvertFrom-Json $body + + # Step 2: Extract Data from the Request + $checkedValues = $requestData.checkedValues + $UID = $requestData.UID + $Name = $requestData.Name + + # Step 3: Validate Input + 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 { + Invoke-Expression $installRMMCommand + $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 { + # 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) - $response.ContentType = "text/plain" + $response.ContentType = "text/plain" $response.ContentLength64 = $buffer.Length $response.OutputStream.Write($buffer, 0, $buffer.Length) $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" {