diff --git a/SVSTaskGate.ps1 b/SVSTaskGate.ps1 index 1d40c44..d4205fe 100644 --- a/SVSTaskGate.ps1 +++ b/SVSTaskGate.ps1 @@ -697,44 +697,28 @@ function GetHtmlContent { if (installDattoRMM.checked) { const DattoRMMCheckbox = document.querySelectorAll('input[name="dattoRMMOption"]:checked'); - const checkedValues = Array.from(DattoRMMCheckbox).map(c => c.value); + appendLog("Installing selected site RMM...", "cyan"); - // Ensure required variables are set - if (!ApiUrl || !ApiKey || !ApiSecretKey) { - appendLog("Error: Missing API credentials. Please configure them before proceeding.", "red"); - return; + const checkedValues = Array.from(DattoRMMCheckbox).map(c => c.value); + 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'; } - // Build payload - const payload = { - ApiUrl: $data.ApiURL, // Replace with actual variable holding the API URL - ApiKey: $data.ApiKey, // Replace with actual variable holding the API Key - ApiSecretKey: $data/ApiSecretKey, // Replace with actual variable holding the API Secret Key - UID: UID, // Replace with actual UID value - Name: Name, // Replace with actual Name value - PushSiteVars: checkedValues.includes('inputVar'), - InstallRMM: checkedValues.includes('rmm'), - SaveCopy: checkedValues.includes('exe') - }; - - // Send POST request fetch('/installrmm', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(payload) - }) - .then(response => { - if (!response.ok) { - appendLog("Error: Failed to trigger RMM installation.", "red"); - throw new Error("Failed to trigger RMM installation"); - } - return response.text(); - }) - .then(data => appendLog(`Success: ${data}`, "green")) - .catch(error => appendLog(`Error: ${error.message}`, "red")); + body: JSON.stringify({ installRMMCommand, UID, Name }) + }); } - if (setSVSPowerplan.checked) { fetch('/installSVSPowerplan', { method: 'GET' }); appendLog("Setting SVS Powerplan", "cyan"); @@ -846,7 +830,6 @@ try { $response.OutputStream.Close() } - "/getn8npw" { if ($request.HttpMethod -eq "POST") { $bodyStream = New-Object IO.StreamReader $request.InputStream @@ -873,68 +856,44 @@ try { } } - - "/installrmm" { + + + "/installrmm" { if ($request.HttpMethod -eq "POST") { - # Read and parse the incoming JSON request - $bodyStream = New-Object IO.StreamReader $request.InputStream - $body = $bodyStream.ReadToEnd() - Write-LogHybrid -Message "Raw request body: $body" -Level "Info" + $bodyStream = New-Object IO.StreamReader $request.InputStream + $body = $bodyStream.ReadToEnd() + $selectedSite = ConvertFrom-Json $body + + # Extract parameters + $ApiUrl = $selectedSite.ApiUrl + $ApiKey = $selectedSite.ApiKey + $ApiSecretKey = $selectedSite.ApiSecretKey + + # Verify required parameters + if (-not $ApiUrl -or -not $ApiKey -or -not $ApiSecretKey) { + $responseString = "Error: Missing required parameters. ApiUrl='$ApiUrl', ApiKey='$ApiKey', ApiSecretKey='$ApiSecretKey'" + $buffer = [System.Text.Encoding]::UTF8.GetBytes($responseString) + $response.ContentType = "text/plain" + $response.ContentLength64 = $buffer.Length + $response.OutputStream.Write($buffer, 0, $buffer.Length) + $response.OutputStream.Close() + return + } + + # Construct the command + $installCommand = "Install-DattoRMM -ApiUrl '$ApiUrl' -ApiKey '$ApiKey' -ApiSecretKey '$ApiSecretKey'" + Write-LogHybrid -Message "Executing command: $installCommand" -Level "Info" try { - $requestData = $body | ConvertFrom-Json - - # Extract parameters - $ApiUrl = $requestData.ApiUrl - $ApiKey = $requestData.ApiKey - $ApiSecretKey = $requestData.ApiSecretKey - $UID = $requestData.UID - $Name = $requestData.Name - $PushSiteVars = $requestData.PushSiteVars - $InstallRMM = $requestData.InstallRMM - $SaveCopy = $requestData.SaveCopy - - Write-LogHybrid -Message "Parsed parameters: ApiUrl='$ApiUrl', ApiKey='$ApiKey', ApiSecretKey='$ApiSecretKey', UID='$UID', Name='$Name'" -Level "Info" - - # Validate required parameters - if (-not $ApiUrl -or -not $ApiKey -or -not $ApiSecretKey -or -not $UID -or -not $Name) { - $responseString = "Error: Missing required parameters." - Write-LogHybrid -Message $responseString -Level "Error" - $buffer = [System.Text.Encoding]::UTF8.GetBytes($responseString) - $response.ContentType = "text/plain" - $response.ContentLength64 = $buffer.Length - $response.OutputStream.Write($buffer, 0, $buffer.Length) - $response.OutputStream.Close() - return - } - - # Dynamically construct the command - $installCommand = "Install-DattoRMM -ApiUrl '$ApiUrl' -ApiKey '$ApiKey' -ApiSecretKey '$ApiSecretKey'" - if ($PushSiteVars) { $installCommand += " -PushSiteVars" } - if ($InstallRMM) { $installCommand += " -InstallRMM" } - if ($SaveCopy) { $installCommand += " -SaveCopy" } - - Write-LogHybrid -Message "Executing command: $installCommand" -Level "Info" - - try { - Invoke-Expression $installCommand - $responseString = "RMM install triggered successfully for UID: $UID, Name: $Name." - $response.StatusCode = 200 - } - catch { - $responseString = "Error triggering RMM install: $($_.Exception.Message)" - $response.StatusCode = 500 - } + Invoke-Expression $installCommand + $responseString = "RMM install triggered successfully." } catch { - $responseString = "Error parsing request JSON: $($_.Exception.Message)" - Write-LogHybrid -Message $responseString -Level "Error" - $response.StatusCode = 400 + $responseString = "Error triggering RMM install: $($_.Exception.Message)" } - # Send 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() @@ -943,7 +902,6 @@ try { - "/setSVSPowerplan" { if ($request.HttpMethod -eq "GET") { Set-SVSPowerPlan