fixed handle-fetchsite with global var
This commit is contained in:
@@ -267,11 +267,6 @@ function Install-SVSMSP {
|
||||
|
||||
# POST /getpw → read JSON body, call helper, return JSON
|
||||
|
||||
$Global:DattoApi = Get-DattoApiCredentials -Password $pw
|
||||
$Global:ApiUrl = $Global:DattoApi.ApiUrl
|
||||
$Global:ApiKey = $Global:DattoApi.ApiKey
|
||||
$Global:ApiSecretKey = $Global:DattoApi.ApiSecretKey
|
||||
|
||||
|
||||
function Handle-FetchSites {
|
||||
param($Context)
|
||||
@@ -281,60 +276,73 @@ function Handle-FetchSites {
|
||||
try {
|
||||
$pw = (ConvertFrom-Json $raw).password
|
||||
} catch {
|
||||
Write-LogHybrid "Invalid JSON in request body: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent
|
||||
Write-LogHybrid "Invalid JSON in /getpw payload: $($_.Exception.Message)" "Error" "FetchSites"
|
||||
returnRespondEmpty $Context
|
||||
return
|
||||
}
|
||||
|
||||
# 2) Fetch your Datto API creds from the webhook
|
||||
Write-LogHybrid "Calling webhook for Datto API credentials" "Info" "FetchSites"
|
||||
Write-LogHybrid "Calling webhook for Datto credentials…" "Info" "FetchSites"
|
||||
try {
|
||||
$hdr = @{ "SVSMSPKit" = $pw }
|
||||
$resp = Invoke-RestMethod -Uri "https://automate.svstools.ca/webhook/svsmspkit" `
|
||||
$hdr = @{ "SVSMSPKit" = $pw }
|
||||
$resp = Invoke-RestMethod -Uri "https://automate.svstools.ca/webhook/svsmspkit" `
|
||||
-Headers $hdr -Method Get
|
||||
$apiUrl = $resp.ApiUrl
|
||||
$apiKey = $resp.ApiKey
|
||||
$apiSecretKey = $resp.ApiSecretKey
|
||||
|
||||
# store for later RMM calls
|
||||
$Global:ApiUrl = $resp.ApiUrl
|
||||
$Global:ApiKey = $resp.ApiKey
|
||||
$Global:ApiSecretKey = $resp.ApiSecretKey
|
||||
Write-LogHybrid "Fetched and stored API credentials." "Success" "FetchSites"
|
||||
} catch {
|
||||
Write-LogHybrid "FetchSites webhook call failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent
|
||||
Write-LogHybrid "Webhook call failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent
|
||||
returnRespondEmpty $Context
|
||||
return
|
||||
}
|
||||
|
||||
# 3) Exchange for a bearer token
|
||||
Write-LogHybrid "Requesting OAuth token from Datto API" "Info" "FetchSites"
|
||||
Write-LogHybrid "Requesting OAuth token…" "Info" "FetchSites"
|
||||
try {
|
||||
$securePublic = ConvertTo-SecureString -String 'public' -AsPlainText -Force
|
||||
$securePublic = ConvertTo-SecureString 'public' -AsPlainText -Force
|
||||
$creds = New-Object System.Management.Automation.PSCredential('public-client',$securePublic)
|
||||
$tokenResp = Invoke-RestMethod -Uri "$apiUrl/auth/oauth/token" `
|
||||
-Credential $creds -Method Post `
|
||||
$tokenResp = Invoke-RestMethod `
|
||||
-Uri "$ApiUrl/auth/oauth/token" `
|
||||
-Credential $creds `
|
||||
-Method Post `
|
||||
-ContentType 'application/x-www-form-urlencoded' `
|
||||
-Body "grant_type=password&username=$apiKey&password=$apiSecretKey"
|
||||
-Body "grant_type=password&username=$ApiKey&password=$ApiSecretKey"
|
||||
$token = $tokenResp.access_token
|
||||
Write-LogHybrid "OAuth token acquired." "Success" "FetchSites"
|
||||
} catch {
|
||||
Write-LogHybrid "Token request failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent
|
||||
Write-LogHybrid "OAuth request failed: $($_.Exception.Message)" "Error" "FetchSites"
|
||||
returnRespondEmpty $Context
|
||||
return
|
||||
}
|
||||
|
||||
# 4) Pull the site list
|
||||
Write-LogHybrid "Fetching sites list from Datto RMM API" "Info" "FetchSites"
|
||||
Write-LogHybrid "Fetching Datto RMM site list…" "Info" "FetchSites"
|
||||
try {
|
||||
$hdr = @{ Authorization = "Bearer $token" }
|
||||
$sitesResp = Invoke-RestMethod -Uri "$apiUrl/api/v2/account/sites" `
|
||||
-Method Get -Headers $hdr -ContentType 'application/json'
|
||||
$siteList = $sitesResp.sites | ForEach-Object {
|
||||
$sitesResp = Invoke-RestMethod -Uri "$ApiUrl/api/v2/account/sites" `
|
||||
-Method Get `
|
||||
-Headers $hdr `
|
||||
-ContentType 'application/json'
|
||||
$siteList = $sitesResp.sites | ForEach-Object {
|
||||
[PSCustomObject]@{ Name = $_.name; UID = $_.uid }
|
||||
}
|
||||
Write-LogHybrid "Site list retrieved (${siteList.Count} sites)." "Success" "FetchSites"
|
||||
} catch {
|
||||
Write-LogHybrid "Site list retrieval failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent
|
||||
Write-LogHybrid "Failed to fetch site list: $($_.Exception.Message)" "Error" "FetchSites"
|
||||
returnRespondEmpty $Context
|
||||
return
|
||||
}
|
||||
|
||||
# 5) Return JSON array
|
||||
Write-LogHybrid "Returning ${($siteList).Count} sites to client" "Info" "FetchSites"
|
||||
Respond-JSON $Context $siteList
|
||||
$json = $siteList | 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)
|
||||
$Context.Response.OutputStream.Close()
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user