Update testTaskGate.ps1

This commit is contained in:
2025-05-27 00:07:47 -04:00
parent 7a96e7782a
commit 8d6ab3f53f

View File

@@ -207,69 +207,74 @@ function Install-DattoRMM-Helper {
# POST /getpw → read JSON body, call helper, return JSON
function Handle-FetchSites {
param($Context)
Write-Host "[Debug] Handle-FetchSites invoked"
# 1) Read incoming POST body
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
try {
# Safely read the JSON body using UTF8
$reader = New-Object System.IO.StreamReader($Context.Request.InputStream, [System.Text.Encoding]::UTF8)
$body = $reader.ReadToEnd()
Write-Host "[Debug] Raw body: $body"
# Parse JSON and extract password
$json = $body | ConvertFrom-Json
$pw = $json.password
Write-Host "[Debug] Parsed password: $pw"
if (-not $pw) {
Write-Host "[Error] Password is missing from request body"
$Context.Response.StatusCode = 400
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
$pwObj = $body | ConvertFrom-Json
$pw = $pwObj.password
} catch {
Write-Host "[Error][FetchSites] Invalid JSON body: $_"
returnRespondEmpty $Context
return
}
# Call credential helper
# 2) Fetch dynamic credentials from n8n
Write-Host "[Debug][FetchSites] Calling webhook for credentials..."
$creds = Get-DattoApiCredentials -Password $pw
if (-not $creds) {
Write-Host "[Error] Credential fetch failed"
$Context.Response.StatusCode = 500
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
Write-Host "[Error][FetchSites] Get-DattoApiCredentials returned \$null"
returnRespondEmpty $Context
return
}
Write-Host "[Debug][FetchSites] Got creds: ApiUrl=$($creds.ApiUrl), ApiKey=$($creds.ApiKey.Substring(0,4))..., Secret=$( $creds.ApiSecretKey.Substring(0,4) )..."
# Fetch site list from Datto API
# 3) Call helper to fetch sites
Write-Host "[Debug][FetchSites] Calling Install-DattoRMM-Helper -FetchSitesOnly..."
$sites = $null
try {
$sites = Install-DattoRMM-Helper `
-ApiUrl $creds.ApiUrl `
-ApiKey $creds.ApiKey `
-ApiSecretKey $creds.ApiSecretKey `
-FetchSitesOnly
if (-not $sites) {
Write-Host "[Error] Site list was empty or failed to fetch"
$Context.Response.StatusCode = 500
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
} catch {
Write-Host "[Error][FetchSites] Helper threw: $($_.Exception.Message)"
returnRespondEmpty $Context
return
}
# Return site list as JSON
$jsonSites = $sites | ConvertTo-Json -Depth 2
$bytes = [Text.Encoding]::UTF8.GetBytes($jsonSites)
$Context.Response.ContentType = "application/json"
if (-not $sites) {
Write-Host "[Error][FetchSites] No sites were returned by helper."
returnRespondEmpty $Context
return
}
# 4) Serialize and send back JSON
$json = $sites | ConvertTo-Json -Depth 2
$bytes = [Text.Encoding]::UTF8.GetBytes($json)
$Context.Response.ContentType = 'application/json'
$Context.Response.ContentLength64 = $bytes.Length
$Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
} catch {
Write-Host "[Exception] $($_.Exception.Message)"
$Context.Response.StatusCode = 500
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
} finally {
$Context.Response.OutputStream.Close()
}
# Helper function to consistently return an empty JSON array
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()
}
# On-boarding handlers
function Set-SVSPowerPlan {
param($Context)