debug version

This commit is contained in:
2025-05-27 00:10:12 -04:00
parent 8d6ab3f53f
commit d70d17372d

View File

@@ -205,39 +205,47 @@ function Install-DattoRMM-Helper {
#endregion #endregion
# POST /getpw → read JSON body, call helper, return JSON # 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 { function Handle-FetchSites {
param($Context) param($Context)
# 1) Read incoming POST body # 1) Read incoming JSON
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd() $body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
try { try {
$pwObj = $body | ConvertFrom-Json $pw = (ConvertFrom-Json $body).password
$pw = $pwObj.password
} catch { } catch {
Write-Host "[Error][FetchSites] Invalid JSON body: $_" Write-Host "[Error][FetchSites] Invalid JSON body: $_"
returnRespondEmpty $Context returnRespondEmpty $Context
return return
} }
# 2) Fetch dynamic credentials from n8n # 2) Hit the n8n webhook for fresh creds
Write-Host "[Debug][FetchSites] Calling webhook for credentials..." Write-Host "[Debug][FetchSites] Fetching API creds from webhook..."
$creds = Get-DattoApiCredentials -Password $pw $creds = Get-DattoApiCredentials -Password $pw
if (-not $creds) { if (-not $creds) {
Write-Host "[Error][FetchSites] Get-DattoApiCredentials returned \$null" Write-Host "[Error][FetchSites] Failed to retrieve credentials."
returnRespondEmpty $Context returnRespondEmpty $Context
return 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 # 3) Call your helper to get the site list
Write-Host "[Debug][FetchSites] Calling Install-DattoRMM-Helper -FetchSitesOnly..." Write-Host "[Debug][FetchSites] Invoking Install-DattoRMM-Helper -FetchSitesOnly..."
$sites = $null
try { try {
$sites = Install-DattoRMM-Helper ` $sites = Install-DattoRMM-Helper `
-ApiUrl $creds.ApiUrl ` -ApiUrl $creds.ApiUrl `
-ApiKey $creds.ApiKey ` -ApiKey $creds.ApiKey `
-ApiSecretKey $creds.ApiSecretKey ` -ApiSecretKey $creds.ApiSecretKey `
-FetchSitesOnly -FetchSitesOnly
} catch { } catch {
Write-Host "[Error][FetchSites] Helper threw: $($_.Exception.Message)" Write-Host "[Error][FetchSites] Helper threw: $($_.Exception.Message)"
returnRespondEmpty $Context returnRespondEmpty $Context
@@ -245,12 +253,12 @@ function Handle-FetchSites {
} }
if (-not $sites) { if (-not $sites) {
Write-Host "[Error][FetchSites] No sites were returned by helper." Write-Host "[Error][FetchSites] Helper returned no sites."
returnRespondEmpty $Context returnRespondEmpty $Context
return return
} }
# 4) Serialize and send back JSON # 4) Serialize and reply
$json = $sites | ConvertTo-Json -Depth 2 $json = $sites | ConvertTo-Json -Depth 2
$bytes = [Text.Encoding]::UTF8.GetBytes($json) $bytes = [Text.Encoding]::UTF8.GetBytes($json)
$Context.Response.ContentType = 'application/json' $Context.Response.ContentType = 'application/json'