fixing fetch sites

This commit is contained in:
2025-05-26 23:36:16 -04:00
parent c393a1aa10
commit dc8cbe1cae

View File

@@ -131,6 +131,24 @@ function Respond-JSON {
$Context.Response.OutputStream.Write($bytes,0,$bytes.Length)
$Context.Response.OutputStream.Close()
}
#region Get-DattoApiCreds
function Get-DattoApiCredentials {
param ([string]$Password)
$url = "https://automate.svstools.ca/webhook/svsmspkit"
$headers = @{ "SVSMSPKit" = $Password }
try {
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method GET
return @{
ApiUrl = $response.ApiUrl
ApiKey = $response.ApiKey
ApiSecretKey = $response.ApiSecretKey
}
} catch {
Write-LogHybrid "Failed to fetch API credentials: $($_.Exception.Message)" "Error" "DattoAuth"
return $null
}
}
#endregion
#region Install-DattoRMM-Helper
function Install-DattoRMM-Helper {
@@ -189,30 +207,40 @@ function Install-DattoRMM-Helper {
# POST /getpw → read JSON body, call helper, return JSON
function Handle-FetchSites {
param($Context)
Write-Host "[Debug] Handle-FetchSites invoked" # ← add this
Write-Host "[Debug] Handle-FetchSites invoked"
try {
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
$pw = (ConvertFrom-Json $body).password
$creds = Get-DattoApiCredentials -Password $pw
if (-not $creds) {
Write-LogHybrid "Credential fetch failed." "Error"
$Context.Response.StatusCode = 403
Respond-JSON $Context @()
return
}
$sites = Install-DattoRMM-Helper `
-ApiUrl $ApiUrl `
-ApiKey $ApiKey `
-ApiSecretKey $ApiSecretKey `
-ApiUrl $creds.ApiUrl `
-ApiKey $creds.ApiKey `
-ApiSecretKey $creds.ApiSecretKey `
-FetchSitesOnly
if (-not $sites) { $Context.Response.StatusCode = 500; $sites = @() }
$json = $sites | ConvertTo-Json -Depth 2
$bytes = [Text.Encoding]::UTF8.GetBytes($json)
$Context.Response.ContentType = 'application/json'
$Context.Response.OutputStream.Write($bytes,0,$bytes.Length)
if (-not $sites) {
$Context.Response.StatusCode = 500
$sites = @()
}
Respond-JSON $Context $sites
} catch {
$Context.Response.StatusCode = 500
$Context.Response.ContentType = 'application/json'
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes('[]'),0,2)
} finally {
$Context.Response.OutputStream.Close()
Respond-JSON $Context @()
}
}
# On-boarding handlers
function Set-SVSPowerPlan {
param($Context)