fixed handle-fetchsite with global var

This commit is contained in:
2025-05-29 00:59:48 -04:00
parent e079d6f84c
commit e64b5a773f

View File

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