From 2dbb4feaef2057db2e0766cef6e2a8fd91127842 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Sun, 5 Jan 2025 02:14:46 -0500 Subject: [PATCH] Update SVSTaskGate.ps1 --- SVSTaskGate.ps1 | 143 ++++++++++++++++++------------------------------ 1 file changed, 52 insertions(+), 91 deletions(-) diff --git a/SVSTaskGate.ps1 b/SVSTaskGate.ps1 index 4c4cf0d..db0165b 100644 --- a/SVSTaskGate.ps1 +++ b/SVSTaskGate.ps1 @@ -649,7 +649,7 @@ function GetHtmlContent { } try { - appendLog("Fetching sites...", "yellow"); + appendLog("Fetching sites and API credentials...", "yellow"); const response = await fetch('/getn8npw', { method: 'POST', @@ -661,108 +661,68 @@ function GetHtmlContent { throw new Error('Failed to fetch sites. Please try again.'); } - const sites = await response.json(); - dropdown.innerHTML = ''; + const data = await response.json(); - sites.forEach(site => { + // Populate the dropdown with sites + dropdown.innerHTML = ''; + data.Sites.forEach(site => { const option = document.createElement('option'); - // Adjust property names based on your actual data - option.value = site.UID; - option.textContent = site.Name; + option.value = site.UID; // Adjust based on your data structure + option.textContent = site.Name; // Adjust based on your data structure dropdown.appendChild(option); }); appendLog("Sites fetched successfully, please select a site!", "green"); - } - catch (error) { + + // Store the API credentials globally for later use + window.ApiUrl = data.ApiUrl; + window.ApiKey = data.ApiKey; + window.ApiSecretKey = data.ApiSecretKey; + } catch (error) { dropdown.innerHTML = ''; - appendLog('Error:' + error.message, "red"); + appendLog('Error: ' + error.message, "red"); } } + function triggerInstall() { const dropdown = document.getElementById('dattoRmmDropdown'); const UID = dropdown.options[dropdown.selectedIndex].value; const Name = dropdown.options[dropdown.selectedIndex].text; - const setSVSPowerplan = document.querySelector('input[name="setSVSPowerplan"]'); - const installSVSMSPModule = document.querySelector('input[name="installSVSMSPModule"]'); - const installDattoRMM = document.querySelector('input[name="installDattoRMM"]'); - const installCyberQP = document.querySelector('input[name="installCyberQP"]'); - const installSplashtop = document.querySelector('input[name="installSplashtop"]'); - const installSVSHelpDesk = document.querySelector('input[name="installSVSHelpDesk"]'); - const installSVSWatchtower = document.querySelector('input[name="installSVSWatchtower"]'); - const installThreatLocker = document.querySelector('input[name="installThreatlocker"]'); - const installRocketCyber = document.querySelector('input[name="installRocketCyber"]'); + const checkedValues = Array.from(document.querySelectorAll('input[name="dattoRMMOption"]:checked')).map(c => c.value); - 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); - - // Predefined PowerShell global variables passed into JavaScript - // const ApiUrl = "$global:ApiUrl"; - // const ApiKey = "$global:ApiKey"; - // const ApiSecretKey = "$global:ApiSecretKey"; - - // 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({ installRMMCommand, UID, Name }) - }); + if (!window.ApiUrl || !window.ApiKey || !window.ApiSecretKey) { + appendLog("API credentials are missing. Please fetch them first.", "red"); + return; } - if (setSVSPowerplan.checked) { - fetch('/installSVSPowerplan', { method: 'GET' }); - appendLog("Setting SVS Powerplan", "cyan"); + let installRMMCommand = `Install-DattoRMM -ApiUrl '${window.ApiUrl}' -ApiKey '${window.ApiKey}' -ApiSecretKey '${window.ApiSecretKey}'`; + + if (checkedValues.includes('inputVar')) { + installRMMCommand += ' -PushSiteVars'; + } + if (checkedValues.includes('rmm')) { + installRMMCommand += ' -InstallRMM'; + } + if (checkedValues.includes('exe')) { + installRMMCommand += ' -SaveCopy'; } - if (installSVSMSPModule.checked) { - fetch('/installSVSMSPModule', { method: 'GET' }); - appendLog("Installing CyberQP", "cyan"); + console.log("Generated Command:", installRMMCommand); - } - - if (installCyberQP.checked) { - fetch('/installCyberQP', { method: 'GET' }); - appendLog("Installing CyberQP", "cyan"); - } - if (installSplashtop.checked) { - fetch('/installSplashtop', { method: 'GET' }); - appendLog("Installing Splashtop", "cyan"); - } - if (installSVSHelpDesk.checked) { - fetch('/installSVSHelpDesk', { method: 'GET' }); - appendLog("Installing SVSHelpdesk", "cyan"); - } - if (installSVSWatchtower.checked) { - fetch('/installSVSWatchtower', { method: 'GET' }); - appendLog("Installing SVSWatchtower", "cyan"); - } - if (installThreatLocker.checked) { - fetch('/installThreatLocker', { method: 'GET' }); - appendLog("Installing ThreatLocker", "cyan"); - } - if (installRocketCyber.checked) { - fetch('/installRocketCyber', { method: 'GET' }); - appendLog("Installing RocketCyber", "cyan"); - } + fetch('/installrmm', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ installRMMCommand, UID, Name }) + }).then(() => { + appendLog(`RMM installation command sent for ${Name}`, "green"); + }).catch(err => { + appendLog(`Error sending command: ${err.message}`, "red"); + }); } + function endSession() { appendLog("Session ended. Closing application...", "yellow"); fetch('/quit', { method: 'GET' }) @@ -844,18 +804,18 @@ try { $data = ConvertFrom-Json $body $password = $data.password + # Run the function to fetch data Get-N8nWebhookData -AuthHeaderValue $password - $sites = Install-DattoRMM -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly - if (-not $sites) { - Write-Host "No sites returned. Please check the API." -ForegroundColor Red - $response.StatusCode = 500 - $buffer = [System.Text.Encoding]::UTF8.GetBytes("No sites found") - $response.OutputStream.Write($buffer, 0, $buffer.Length) - $response.OutputStream.Close() - continue - } - $responseData = $sites | ConvertTo-Json - $buffer = [System.Text.Encoding]::UTF8.GetBytes($responseData) + + # Include the dynamically fetched variables in the response + $responseData = @{ + ApiUrl = $global:ApiUrl + ApiKey = $global:ApiKey + ApiSecretKey = $global:ApiSecretKey + Sites = Install-DattoRMM -ApiUrl $global:ApiUrl -ApiKey $global:ApiKey -ApiSecretKey $global:ApiSecretKey -FetchSitesOnly + } | ConvertTo-Json -Depth 2 + + $buffer = [System.Text.Encoding]::UTF8.GetBytes($responseData) $response.ContentType = "application/json" $response.ContentLength64 = $buffer.Length $response.OutputStream.Write($buffer, 0, $buffer.Length) @@ -863,6 +823,7 @@ try { } } + "/installrmm" { if ($request.HttpMethod -eq "POST") { $bodyStream = New-Object IO.StreamReader $request.InputStream