diff --git a/SVSTaskGate.ps1 b/SVSTaskGate.ps1 index ac3c2a2..4fbc098 100644 --- a/SVSTaskGate.ps1 +++ b/SVSTaskGate.ps1 @@ -649,7 +649,7 @@ function GetHtmlContent { } try { - appendLog("Fetching sites and API credentials...", "yellow"); + appendLog("Fetching sites...", "yellow"); const response = await fetch('/getn8npw', { method: 'POST', @@ -658,35 +658,28 @@ function GetHtmlContent { }); if (!response.ok) { - throw new Error('Failed to fetch data. Please try again.'); + throw new Error('Failed to fetch sites. Please try again.'); } - const data = await response.json(); - - // Populate the dropdown with sites + const sites = await response.json(); dropdown.innerHTML = ''; - data.Sites.forEach(site => { + + sites.forEach(site => { const option = document.createElement('option'); - option.value = site.UID; // Adjust property names based on your actual data + // Adjust property names based on your actual data + option.value = site.UID; option.textContent = site.Name; dropdown.appendChild(option); }); - // Store the API credentials globally for later use - window.apiConfig = { - ApiUrl: data.ApiUrl, - ApiKey: data.ApiKey, - ApiSecretKey: data.ApiSecretKey - }; - - appendLog("Sites and API credentials fetched successfully.", "green"); - } catch (error) { + appendLog("Sites fetched successfully, please select a site!", "green"); + } + 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; @@ -838,47 +831,32 @@ try { $response.OutputStream.Close() } - "/getn8npw" { + "/getn8npw" { if ($request.HttpMethod -eq "POST") { $bodyStream = New-Object IO.StreamReader $request.InputStream $body = $bodyStream.ReadToEnd() $data = ConvertFrom-Json $body $password = $data.password - # Fetch data using Get-N8nWebhookData - $webhookData = Get-N8nWebhookData -AuthHeaderValue $password - - if (-not $webhookData) { - Write-Host "No data returned from Get-N8nWebhookData. Please check the password or API." -ForegroundColor Red + 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("Failed to fetch data") + $buffer = [System.Text.Encoding]::UTF8.GetBytes("No sites found") $response.OutputStream.Write($buffer, 0, $buffer.Length) $response.OutputStream.Close() continue } - - # Populate API-related variables - $global:ApiUrl = $webhookData.ApiUrl - $global:ApiKey = $webhookData.ApiKey - $global:ApiSecretKey = $webhookData.ApiSecretKey - - # Include the API 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 10 - - $buffer = [System.Text.Encoding]::UTF8.GetBytes($responseData) - $response.ContentType = "application/json" + $responseData = $sites | ConvertTo-Json + $buffer = [System.Text.Encoding]::UTF8.GetBytes($responseData) + $response.ContentType = "application/json" $response.ContentLength64 = $buffer.Length $response.OutputStream.Write($buffer, 0, $buffer.Length) $response.OutputStream.Close() } } - "/installrmm" { if ($request.HttpMethod -eq "POST") { $bodyStream = New-Object IO.StreamReader $request.InputStream