From e079d6f84cd25b4271f45cce377b55cdb6f50a17 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Thu, 29 May 2025 00:54:02 -0400 Subject: [PATCH] fixed multiple things --- StackMonkey.ps1 | 69 +++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/StackMonkey.ps1 b/StackMonkey.ps1 index f268691..a33b825 100644 --- a/StackMonkey.ps1 +++ b/StackMonkey.ps1 @@ -100,7 +100,9 @@ function Write-LogHelper { -EntryType $Level -EventId $EventID ` -Message $Message } catch { - Write-Host "([char]0x26A0) [Warning] [EventLog] Failed to write to Event Log: $($_.Exception.Message)" -ForegroundColor Yellow + Write-Host "$([System.Char]::ConvertFromUtf32(0x26A0))$([System.Char]::ConvertFromUtf32(0xFE0F)) [Warning] [EventLog] Failed to write to Event Log: $($_.Exception.Message)" -ForegroundColor Yellow + + } } } @@ -265,6 +267,12 @@ 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) @@ -273,66 +281,64 @@ function Handle-FetchSites { try { $pw = (ConvertFrom-Json $raw).password } catch { - Write-Host "[Error][FetchSites] Invalid JSON: $_" - returnEmpty $Context; return + Write-LogHybrid "Invalid JSON in request body: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent + returnRespondEmpty $Context + return } # 2) Fetch your Datto API creds from the webhook - Write-Host "[Debug][FetchSites] calling webhook..." + Write-LogHybrid "Calling webhook for Datto API 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 } catch { - Write-Host "[Error][FetchSites] webhook failed: $_" - returnEmpty $Context; return + Write-LogHybrid "FetchSites webhook call failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent + returnRespondEmpty $Context + return } # 3) Exchange for a bearer token - Write-Host "[Debug][FetchSites] getting OAuth token..." + Write-LogHybrid "Requesting OAuth token from Datto API" "Info" "FetchSites" try { $securePublic = ConvertTo-SecureString -String '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 ` + $creds = New-Object System.Management.Automation.PSCredential('public-client',$securePublic) + $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" $token = $tokenResp.access_token } catch { - Write-Host "[Error][FetchSites] token request failed: $_" - returnEmpty $Context; return + Write-LogHybrid "Token request failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent + returnRespondEmpty $Context + return } # 4) Pull the site list - Write-Host "[Debug][FetchSites] fetching sites list..." + Write-LogHybrid "Fetching sites list from Datto RMM API" "Info" "FetchSites" try { - $hdr = @{ Authorization = "Bearer $token" } + $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 { + -Method Get -Headers $hdr -ContentType 'application/json' + $siteList = $sitesResp.sites | ForEach-Object { [PSCustomObject]@{ Name = $_.name; UID = $_.uid } } } catch { - Write-Host "[Error][FetchSites] site list failed: $_" - returnEmpty $Context; return + Write-LogHybrid "Site list retrieval failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent + returnRespondEmpty $Context + return } # 5) Return JSON array - $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() + Write-LogHybrid "Returning ${($siteList).Count} sites to client" "Info" "FetchSites" + Respond-JSON $Context $siteList } + # Helper function to consistently return an empty JSON array function returnRespondEmpty { param($Context) @@ -474,15 +480,16 @@ function Handle-InstallDattoRMM { } # Off-boarding handlers -function Uninstall-CyberQP { +function Handle-UninstallCyberQP { param($Context) # 1) call into your module - Uninstall-CyberQP + Uninstall-CyberQP Write-LogHybrid "CyberQP uninstalled" "Success" "OffBoard" Respond-Text $Context "CyberQP uninstalled" } + function Cleanup-SVSMSP { param($Context) Write-LogHybrid "SVSMSP cleaned up" "Success" "OffBoard"