From 7609cb4b2f24509ac445d4e3c1067d1dc7558046 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Sun, 29 Jun 2025 03:33:00 -0400 Subject: [PATCH] added --- StackMonkey.ps1 | 70 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/StackMonkey.ps1 b/StackMonkey.ps1 index a1dd6ad..01c9d97 100644 --- a/StackMonkey.ps1 +++ b/StackMonkey.ps1 @@ -204,7 +204,75 @@ function Get-DattoApiCredentials { } } -#region Logging Helpers +function Get-DattoRmmSites { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [string] $Password, + + [Parameter()] + [string] $WebhookUrl = $Global:DattoWebhookUrl + ) + + # 1) Fetch Datto API credentials from your webhook + Write-Verbose "Fetching Datto API credentials from $WebhookUrl" + try { + $headers = @{ 'SVSMSPKit' = $Password } + $creds = Invoke-RestMethod -Uri $WebhookUrl -Headers $headers -Method GET + } + catch { + Throw "Failed to fetch credentials from webhook: $_" + } + + $apiUrl = $creds.ApiUrl + $apiKey = $creds.ApiKey + $apiSecretKey = $creds.ApiSecretKey + + # 2) Request an OAuth token + Write-Verbose "Requesting OAuth token from $apiUrl/auth/oauth/token" + try { + $securePwd = ConvertTo-SecureString -String 'public' -AsPlainText -Force + $credObj = New-Object System.Management.Automation.PSCredential('public-client', $securePwd) + + $tokenResp = Invoke-RestMethod ` + -Uri "$apiUrl/auth/oauth/token" ` + -Credential $credObj ` + -Method 'POST' ` + -ContentType 'application/x-www-form-urlencoded' ` + -Body "grant_type=password&username=$apiKey&password=$apiSecretKey" + + $token = $tokenResp.access_token + } + catch { + Throw "Failed to obtain OAuth token: $_" + } + + # 3) Fetch the list of RMM sites + Write-Verbose "Fetching RMM sites from $apiUrl/api/v2/account/sites" + try { + $authHeader = @{ Authorization = "Bearer $token" } + $sitesResp = Invoke-RestMethod ` + -Uri "$apiUrl/api/v2/account/sites" ` + -Method 'GET' ` + -Headers $authHeader ` + -ContentType 'application/json' + + $siteList = $sitesResp.sites | Select-Object ` + @{ Name = 'Name'; Expression = { $_.name } }, ` + @{ Name = 'UID'; Expression = { $_.uid } } + + if (-not $siteList) { + Write-Warning "No sites were returned by the API." + return @() + } + + return $siteList + } + catch { + Throw "Failed to fetch sites from API: $_" + } +} + # Initialize a global in-memory log cache if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.ArrayList])) {