Update testTaskGate.ps1
This commit is contained in:
@@ -210,41 +210,55 @@ function Handle-FetchSites {
|
|||||||
Write-Host "[Debug] Handle-FetchSites invoked"
|
Write-Host "[Debug] Handle-FetchSites invoked"
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
|
# Safely read the JSON body using UTF8
|
||||||
$pw = (ConvertFrom-Json $body).password
|
$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
|
$creds = Get-DattoApiCredentials -Password $pw
|
||||||
|
|
||||||
if (-not $creds) {
|
if (-not $creds) {
|
||||||
Write-LogHybrid "Failed to fetch credentials." "Error" "Server"
|
Write-Host "[Error] Credential fetch failed"
|
||||||
$Context.Response.StatusCode = 500
|
$Context.Response.StatusCode = 500
|
||||||
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
|
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
# Fetch the site list using the helper
|
# Fetch site list from Datto API
|
||||||
$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
|
||||||
|
|
||||||
if (-not $sites) {
|
if (-not $sites) {
|
||||||
|
Write-Host "[Error] Site list was empty or failed to fetch"
|
||||||
$Context.Response.StatusCode = 500
|
$Context.Response.StatusCode = 500
|
||||||
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
|
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
# Serialize and respond
|
# Return site list as JSON
|
||||||
$json = $sites | ConvertTo-Json -Depth 2
|
$jsonSites = $sites | ConvertTo-Json -Depth 2
|
||||||
$bytes = [Text.Encoding]::UTF8.GetBytes($json)
|
$bytes = [Text.Encoding]::UTF8.GetBytes($jsonSites)
|
||||||
$Context.Response.ContentType = "application/json"
|
$Context.Response.ContentType = "application/json"
|
||||||
$Context.Response.ContentLength64 = $bytes.Length
|
$Context.Response.ContentLength64 = $bytes.Length
|
||||||
$Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
|
$Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
|
||||||
|
|
||||||
} catch {
|
} 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.StatusCode = 500
|
||||||
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
|
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes("[]"), 0, 2)
|
||||||
} finally {
|
} finally {
|
||||||
@@ -254,6 +268,8 @@ function Handle-FetchSites {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# On-boarding handlers
|
# On-boarding handlers
|
||||||
function Set-SVSPowerPlan {
|
function Set-SVSPowerPlan {
|
||||||
param($Context)
|
param($Context)
|
||||||
|
|||||||
Reference in New Issue
Block a user