diff --git a/SVSTaskGate.ps1 b/SVSTaskGate.ps1 index b6668de..cd8e58b 100644 --- a/SVSTaskGate.ps1 +++ b/SVSTaskGate.ps1 @@ -697,35 +697,44 @@ function GetHtmlContent { if (installDattoRMM.checked) { const DattoRMMCheckbox = document.querySelectorAll('input[name="dattoRMMOption"]:checked'); - appendLog("Installing selected site RMM...", "cyan"); - 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'; + // Ensure required variables are set + if (!ApiUrl || !ApiKey || !ApiSecretKey) { + appendLog("Error: Missing API credentials. Please configure them before proceeding.", "red"); + return; } - if (checkedValues.includes('rmm')) { - installRMMCommand += ' -InstallRMM'; - } - if (checkedValues.includes('exe')) { - installRMMCommand += ' -SaveCopy'; - } - - // Now send this command in the JSON payload + + // Build payload const payload = { - installRMMCommand, - UID, - Name + ApiUrl: ApiUrl, // Replace with actual variable holding the API URL + ApiKey: ApiKey, // Replace with actual variable holding the API Key + ApiSecretKey: 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")); } + if (setSVSPowerplan.checked) { fetch('/installSVSPowerplan', { method: 'GET' }); appendLog("Setting SVS Powerplan", "cyan"); @@ -865,47 +874,67 @@ try { } - "/installrmm" { - if ($request.HttpMethod -eq "GET") { - $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 - - # Log parsed data for debugging + "/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" - Write-LogHybrid -Message "Parsed parameters: ApiUrl='$ApiUrl', ApiKey='$ApiKey', ApiSecretKey='$ApiSecretKey'" -Level "Info" - - - # 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 { - Invoke-Expression $installCommand - $responseString = "RMM install triggered successfully." + $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 + } } catch { - $responseString = "Error triggering RMM install: $($_.Exception.Message)" + $responseString = "Error parsing request JSON: $($_.Exception.Message)" + Write-LogHybrid -Message $responseString -Level "Error" + $response.StatusCode = 400 } + # 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() @@ -914,6 +943,7 @@ try { + "/setSVSPowerplan" { if ($request.HttpMethod -eq "GET") { Set-SVSPowerPlan