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 ` -EntryType $Level -EventId $EventID `
-Message $Message -Message $Message
} catch { } 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 # 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)
@@ -273,12 +281,13 @@ function Handle-FetchSites {
try { try {
$pw = (ConvertFrom-Json $raw).password $pw = (ConvertFrom-Json $raw).password
} catch { } catch {
Write-Host "[Error][FetchSites] Invalid JSON: $_" Write-LogHybrid "Invalid JSON in request body: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent
returnEmpty $Context; return returnRespondEmpty $Context
return
} }
# 2) Fetch your Datto API creds from the webhook # 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 { 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" `
@@ -287,52 +296,49 @@ function Handle-FetchSites {
$apiKey = $resp.ApiKey $apiKey = $resp.ApiKey
$apiSecretKey = $resp.ApiSecretKey $apiSecretKey = $resp.ApiSecretKey
} catch { } catch {
Write-Host "[Error][FetchSites] webhook failed: $_" Write-LogHybrid "FetchSites webhook call failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent
returnEmpty $Context; return returnRespondEmpty $Context
return
} }
# 3) Exchange for a bearer token # 3) Exchange for a bearer token
Write-Host "[Debug][FetchSites] getting OAuth token..." Write-LogHybrid "Requesting OAuth token from Datto API" "Info" "FetchSites"
try { try {
$securePublic = ConvertTo-SecureString -String 'public' -AsPlainText -Force $securePublic = ConvertTo-SecureString -String '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 -Uri "$apiUrl/auth/oauth/token" `
-Credential $creds ` -Credential $creds -Method Post `
-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
} catch { } catch {
Write-Host "[Error][FetchSites] token request failed: $_" Write-LogHybrid "Token request failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent
returnEmpty $Context; return returnRespondEmpty $Context
return
} }
# 4) Pull the site list # 4) Pull the site list
Write-Host "[Debug][FetchSites] fetching sites list..." Write-LogHybrid "Fetching sites list from Datto RMM API" "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 ` -Method Get -Headers $hdr -ContentType 'application/json'
-Headers $hdr `
-ContentType 'application/json'
$siteList = $sitesResp.sites | ForEach-Object { $siteList = $sitesResp.sites | ForEach-Object {
[PSCustomObject]@{ Name = $_.name; UID = $_.uid } [PSCustomObject]@{ Name = $_.name; UID = $_.uid }
} }
} catch { } catch {
Write-Host "[Error][FetchSites] site list failed: $_" Write-LogHybrid "Site list retrieval failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent
returnEmpty $Context; return returnRespondEmpty $Context
return
} }
# 5) Return JSON array # 5) Return JSON array
$json = $siteList | ConvertTo-Json -Depth 2 Write-LogHybrid "Returning ${($siteList).Count} sites to client" "Info" "FetchSites"
$bytes = [Text.Encoding]::UTF8.GetBytes($json) Respond-JSON $Context $siteList
$Context.Response.ContentType = 'application/json'
$Context.Response.ContentLength64 = $bytes.Length
$Context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
$Context.Response.OutputStream.Close()
} }
# Helper function to consistently return an empty JSON array # Helper function to consistently return an empty JSON array
function returnRespondEmpty { function returnRespondEmpty {
param($Context) param($Context)
@@ -474,7 +480,7 @@ function Handle-InstallDattoRMM {
} }
# Off-boarding handlers # Off-boarding handlers
function Uninstall-CyberQP { function Handle-UninstallCyberQP {
param($Context) param($Context)
# 1) call into your module # 1) call into your module
@@ -483,6 +489,7 @@ function Uninstall-CyberQP {
Write-LogHybrid "CyberQP uninstalled" "Success" "OffBoard" Write-LogHybrid "CyberQP uninstalled" "Success" "OffBoard"
Respond-Text $Context "CyberQP uninstalled" Respond-Text $Context "CyberQP uninstalled"
} }
function Cleanup-SVSMSP { function Cleanup-SVSMSP {
param($Context) param($Context)
Write-LogHybrid "SVSMSP cleaned up" "Success" "OffBoard" Write-LogHybrid "SVSMSP cleaned up" "Success" "OffBoard"