fixing fetch sites
This commit is contained in:
@@ -131,6 +131,24 @@ function Respond-JSON {
|
|||||||
$Context.Response.OutputStream.Write($bytes,0,$bytes.Length)
|
$Context.Response.OutputStream.Write($bytes,0,$bytes.Length)
|
||||||
$Context.Response.OutputStream.Close()
|
$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
|
#region Install-DattoRMM-Helper
|
||||||
function Install-DattoRMM-Helper {
|
function Install-DattoRMM-Helper {
|
||||||
@@ -189,30 +207,40 @@ function Install-DattoRMM-Helper {
|
|||||||
# POST /getpw → read JSON body, call helper, return JSON
|
# POST /getpw → read JSON body, call helper, return JSON
|
||||||
function Handle-FetchSites {
|
function Handle-FetchSites {
|
||||||
param($Context)
|
param($Context)
|
||||||
Write-Host "[Debug] Handle-FetchSites invoked" # ← add this
|
Write-Host "[Debug] Handle-FetchSites invoked"
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
|
$body = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
|
||||||
$pw = (ConvertFrom-Json $body).password
|
$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 `
|
$sites = Install-DattoRMM-Helper `
|
||||||
-ApiUrl $ApiUrl `
|
-ApiUrl $creds.ApiUrl `
|
||||||
-ApiKey $ApiKey `
|
-ApiKey $creds.ApiKey `
|
||||||
-ApiSecretKey $ApiSecretKey `
|
-ApiSecretKey $creds.ApiSecretKey `
|
||||||
-FetchSitesOnly
|
-FetchSitesOnly
|
||||||
if (-not $sites) { $Context.Response.StatusCode = 500; $sites = @() }
|
|
||||||
$json = $sites | ConvertTo-Json -Depth 2
|
if (-not $sites) {
|
||||||
$bytes = [Text.Encoding]::UTF8.GetBytes($json)
|
$Context.Response.StatusCode = 500
|
||||||
$Context.Response.ContentType = 'application/json'
|
$sites = @()
|
||||||
$Context.Response.OutputStream.Write($bytes,0,$bytes.Length)
|
}
|
||||||
|
|
||||||
|
Respond-JSON $Context $sites
|
||||||
} catch {
|
} catch {
|
||||||
$Context.Response.StatusCode = 500
|
$Context.Response.StatusCode = 500
|
||||||
$Context.Response.ContentType = 'application/json'
|
Respond-JSON $Context @()
|
||||||
$Context.Response.OutputStream.Write([Text.Encoding]::UTF8.GetBytes('[]'),0,2)
|
|
||||||
} finally {
|
|
||||||
$Context.Response.OutputStream.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# On-boarding handlers
|
# On-boarding handlers
|
||||||
function Set-SVSPowerPlan {
|
function Set-SVSPowerPlan {
|
||||||
param($Context)
|
param($Context)
|
||||||
|
|||||||
Reference in New Issue
Block a user