From d70d17372db1e4ad925444bc322222e5b3877ba9 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Tue, 27 May 2025 00:10:12 -0400 Subject: [PATCH] debug version --- testTaskGate.ps1 | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/testTaskGate.ps1 b/testTaskGate.ps1 index b389a77..aaa67e4 100644 --- a/testTaskGate.ps1 +++ b/testTaskGate.ps1 @@ -205,39 +205,47 @@ function Install-DattoRMM-Helper { #endregion # POST /getpw → read JSON body, call helper, return JSON +function returnRespondEmpty { + param($Context) + $empty = [Text.Encoding]::UTF8.GetBytes("[]") + $Context.Response.StatusCode = 500 + $Context.Response.ContentType = 'application/json' + $Context.Response.ContentLength64 = $empty.Length + $Context.Response.OutputStream.Write($empty, 0, $empty.Length) + $Context.Response.OutputStream.Close() +} + function Handle-FetchSites { param($Context) - # 1) Read incoming POST body + # 1) Read incoming JSON $body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd() try { - $pwObj = $body | ConvertFrom-Json - $pw = $pwObj.password + $pw = (ConvertFrom-Json $body).password } catch { Write-Host "[Error][FetchSites] Invalid JSON body: $_" returnRespondEmpty $Context return } - # 2) Fetch dynamic credentials from n8n - Write-Host "[Debug][FetchSites] Calling webhook for credentials..." + # 2) Hit the n8n webhook for fresh creds + Write-Host "[Debug][FetchSites] Fetching API creds from webhook..." $creds = Get-DattoApiCredentials -Password $pw if (-not $creds) { - Write-Host "[Error][FetchSites] Get-DattoApiCredentials returned \$null" + Write-Host "[Error][FetchSites] Failed to retrieve credentials." returnRespondEmpty $Context return } - Write-Host "[Debug][FetchSites] Got creds: ApiUrl=$($creds.ApiUrl), ApiKey=$($creds.ApiKey.Substring(0,4))..., Secret=$( $creds.ApiSecretKey.Substring(0,4) )..." + Write-Host "[Debug][FetchSites] Got ApiUrl=$($creds.ApiUrl), ApiKey startswith='$($creds.ApiKey.Substring(0,4))', Secret startswith='$($creds.ApiSecretKey.Substring(0,4))'" - # 3) Call helper to fetch sites - Write-Host "[Debug][FetchSites] Calling Install-DattoRMM-Helper -FetchSitesOnly..." - $sites = $null + # 3) Call your helper to get the site list + Write-Host "[Debug][FetchSites] Invoking Install-DattoRMM-Helper -FetchSitesOnly..." try { $sites = Install-DattoRMM-Helper ` - -ApiUrl $creds.ApiUrl ` - -ApiKey $creds.ApiKey ` - -ApiSecretKey $creds.ApiSecretKey ` - -FetchSitesOnly + -ApiUrl $creds.ApiUrl ` + -ApiKey $creds.ApiKey ` + -ApiSecretKey $creds.ApiSecretKey ` + -FetchSitesOnly } catch { Write-Host "[Error][FetchSites] Helper threw: $($_.Exception.Message)" returnRespondEmpty $Context @@ -245,12 +253,12 @@ function Handle-FetchSites { } if (-not $sites) { - Write-Host "[Error][FetchSites] No sites were returned by helper." + Write-Host "[Error][FetchSites] Helper returned no sites." returnRespondEmpty $Context return } - # 4) Serialize and send back JSON + # 4) Serialize and reply $json = $sites | ConvertTo-Json -Depth 2 $bytes = [Text.Encoding]::UTF8.GetBytes($json) $Context.Response.ContentType = 'application/json'