diff --git a/StackMonkey.ps1 b/StackMonkey.ps1 index a68c13e..957bd28 100644 --- a/StackMonkey.ps1 +++ b/StackMonkey.ps1 @@ -117,6 +117,33 @@ $Port = 8082 # Configurable endpoints $Global:DattoWebhookUrl = 'https://automate.svstools.ca/webhook/svsmspkit' +#region Get-DattoApiCredentials +function Get-DattoApiCredentials { + [CmdletBinding()] + param ( + [Parameter(Mandatory)] + [string]$Password + ) + $headers = @{ "SVSMSPKit" = $Password } + try { + $resp = Invoke-RestMethod -Uri $Global:DattoWebhookUrl ` + -Headers $headers ` + -Method GET + return @{ + ApiUrl = $resp.ApiUrl + ApiKey = $resp.ApiKey + ApiSecretKey = $resp.ApiSecretKey + } + } + catch { + Write-LogHybrid "Failed to fetch API credentials: $($_.Exception.Message)" Error DattoAuth + return $null + } +} + +#endregion + + switch ($PSCmdlet.ParameterSetName) { 'Toolkit' { Write-LogHelper "Toolkit-only mode" Info Startup @@ -125,11 +152,52 @@ $Global:DattoWebhookUrl = 'https://automate.svstools.ca/webhook/svsmspkit' } 'Datto' { Write-LogHelper "Headless DattoRMM deploy (via n8n)" Info Startup +switch ($PSCmdlet.ParameterSetName) { + 'Toolkit' { + Write-LogHelper "Toolkit-only mode" Info Startup + Install-SVSMSP -InstallToolkit + return + } + 'Datto' { + Write-LogHelper "Headless DattoRMM deploy (via n8n)" Info Startup - # need to get the info from N8N, so run the try command from line 415 to line 424 - # line 415 - 424 would get th url key and secret from n8n + # ──────────────────────────────────────────── + # 1) Fetch URL, Key & Secret from n8n webhook + # (this is your lines 415–424) + # ──────────────────────────────────────────── + try { + $creds = Get-DattoApiCredentials -Password $N8nPassword + if (-not $creds) { + throw "Failed to retrieve Datto API credentials from n8n" + } + # overwrite the parameters with what we just fetched + $DattoApiUrl = $creds.ApiUrl + $DattoApiKey = $creds.ApiKey + $DattoApiSecretKey = $creds.ApiSecretKey - # need to run the external cmdlet "install-dattoRMM with all variables from ParameterSetName='Datto'" + Write-LogHelper "Fetched Datto API credentials from n8n" Success DattoAuth + } + catch { + Write-LogHelper "N8N credential fetch error: $($_.Exception.Message)" Error DattoAuth + throw + } + + # ──────────────────────────────────────────── + # 2) Invoke the existing Install-DattoRMM cmdlet + # ──────────────────────────────────────────── + Install-DattoRMM ` + -ApiUrl $DattoApiUrl ` + -ApiKey $DattoApiKey ` + -ApiSecretKey $DattoApiSecretKey ` + -SiteUID $SiteUID ` + -SiteName $SiteName ` + -PushSiteVars:$PushSiteVars ` + -InstallRMM:$InstallRMM ` + -SaveCopy:$SaveCopy + + return + } +} } @@ -400,6 +468,20 @@ function Handle-FetchSites { # 2) Fetch your Datto API creds from the webhook Write-LogHybrid "Calling webhook for Datto credentials…" "Info" "FetchSites" + <# + $creds = Get-DattoApiCredentials -Password $N8nPassword + if (-not $creds) { + Write-LogHelper "Failed to retrieve Datto API credentials from n8n" Error DattoAuth + throw + } + + # now exactly the same values are populated: + $Global:ApiUrl = $creds.ApiUrl + $Global:ApiKey = $creds.ApiKey + $Global:ApiSecretKey = $creds.ApiSecretKey + Write-LogHelper "Fetched Datto API credentials from n8n" Success DattoAuth +#> #could replace the following 15 lines + try { $hdr = @{ "SVSMSPKit" = $pw } $resp = Invoke-RestMethod -Uri $Global:DattoWebhookUrl -Headers $hdr -Method GET