diff --git a/SVSTaskGate.ps1 b/SVSTaskGate.ps1 index 0ae19d6..e7feec2 100644 --- a/SVSTaskGate.ps1 +++ b/SVSTaskGate.ps1 @@ -699,7 +699,7 @@ function GetHtmlContent { 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'; + let installRMMCommand = `Install-DattoRMM -ApiUrl '${ApiUrl}' -ApiKey '${ApiKey}' -ApiSecretKey '${ApiSecretKey}'`; if (checkedValues.includes('inputVar')) { installRMMCommand += ' -PushSiteVars'; @@ -715,6 +715,19 @@ function GetHtmlContent { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ installRMMCommand, UID, Name }) + }) + .then(response => { + if (!response.ok) { + appendLog(`Error: ${response.statusText}`, "red"); + throw new Error(`Server responded with status ${response.status}`); + } + return response.text(); + }) + .then(data => { + appendLog(`Response: ${data}`, "green"); + }) + .catch(error => { + appendLog(`Fetch error: ${error.message}`, "red"); }); } @@ -855,30 +868,57 @@ try { } } - - "/installrmm" { if ($request.HttpMethod -eq "POST") { - $bodyStream = New-Object IO.StreamReader $request.InputStream - $body = $bodyStream.ReadToEnd() + $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 received data + Write-LogHybrid -Message "Received data: $($body)" -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'" + # Extract values from $selectedSite + $installRMMCommand = $selectedSite.installRMMCommand + $UID = $selectedSite.UID + $Name = $selectedSite.Name + + # Validate required fields + if (-not $installRMMCommand) { + $responseString = "Error: Missing 'installRMMCommand' in the request payload." + Write-LogHybrid -Message $responseString -Level "Error" $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() return } + Write-LogHybrid -Message "Executing command: $installRMMCommand" -Level "Info" + Write-LogHybrid -Message "Site UID: $UID, Name: $Name" -Level "Info" + + try { + # Execute the command + Invoke-Expression $installRMMCommand + $responseString = "RMM install triggered successfully for site: $Name (UID: $UID)." + Write-LogHybrid -Message $responseString -Level "Success" + } + catch { + $responseString = "Error executing RMM install: $($_.Exception.Message)" + Write-LogHybrid -Message $responseString -Level "Error" + } + + # Send the response back to the client + $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() + } + } + + + # Construct the command $installCommand = "Install-DattoRMM -ApiUrl '$ApiUrl' -ApiKey '$ApiKey' -ApiSecretKey '$ApiSecretKey'" Write-LogHybrid -Message "Executing command: $installCommand" -Level "Info"