fixed multiple things

This commit is contained in:
2025-05-29 00:54:02 -04:00
parent 520aae8fb4
commit e079d6f84c

View File

@@ -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,12 +281,13 @@ 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" `
@@ -287,52 +296,49 @@ function Handle-FetchSites {
$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 `
-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" }
$sitesResp = Invoke-RestMethod -Uri "$apiUrl/api/v2/account/sites" `
-Method Get `
-Headers $hdr `
-ContentType 'application/json'
-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,7 +480,7 @@ function Handle-InstallDattoRMM {
}
# Off-boarding handlers
function Uninstall-CyberQP {
function Handle-UninstallCyberQP {
param($Context)
# 1) call into your module
@@ -483,6 +489,7 @@ function 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"