Update testTaskGate.ps1

This commit is contained in:
2025-05-27 00:04:25 -04:00
parent ee4b856227
commit 7a96e7782a

View File

@@ -210,20 +210,33 @@ function Handle-FetchSites {
Write-Host "[Debug] Handle-FetchSites invoked"
try {
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
$pw = (ConvertFrom-Json $body).password
# 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"
# Securely get Datto API credentials
# 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)
return
}
# Call credential helper
$creds = Get-DattoApiCredentials -Password $pw
if (-not $creds) {
Write-LogHybrid "Failed to fetch credentials." "Error" "Server"
Write-Host "[Error] Credential fetch failed"
$Context.Response.StatusCode = 500
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
return
}
# Fetch the site list using the helper
# Fetch site list from Datto API
$sites = Install-DattoRMM-Helper `
-ApiUrl $creds.ApiUrl `
-ApiKey $creds.ApiKey `
@@ -231,20 +244,21 @@ function Handle-FetchSites {
-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)
return
}
# Serialize and respond
$json = $sites | ConvertTo-Json -Depth 2
$bytes = [Text.Encoding]::UTF8.GetBytes($json)
# Return site list as JSON
$jsonSites = $sites | ConvertTo-Json -Depth 2
$bytes = [Text.Encoding]::UTF8.GetBytes($jsonSites)
$Context.Response.ContentType = "application/json"
$Context.Response.ContentLength64 = $bytes.Length
$Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
} catch {
# Write-LogHybrid -Message "Handle-FetchSites failed: $($_.Exception.Message)" -Level "Error" -TaskCategory "Server" -LogToEvent:$true
Write-Host "[Exception] $($_.Exception.Message)"
$Context.Response.StatusCode = 500
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
} finally {
@@ -254,6 +268,8 @@ function Handle-FetchSites {
# On-boarding handlers
function Set-SVSPowerPlan {
param($Context)