From 9e71dbf0f1b9e4dc79039ef8f289ee08a99a6a71 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Tue, 28 Jan 2025 00:13:11 -0500 Subject: [PATCH] Update TGBeta.ps1 --- TGBeta.ps1 | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/TGBeta.ps1 b/TGBeta.ps1 index 083d7ea..4f4699b 100644 --- a/TGBeta.ps1 +++ b/TGBeta.ps1 @@ -151,6 +151,109 @@ else { # Example usage of Write-LogHybrid Write-LogHybrid -Message "Starting SVS TaskGate" -Level "Info" -TaskCategory "SVSTaskGate" -LogToEvent:$true +#endregion + +#region Install-DattoRMM-Helper + +function Install-DattoRMM-Helper { + param ( + [string]$ApiUrl, + [string]$ApiKey, + [string]$ApiSecretKey, + [switch]$FetchSitesOnly, # Fetch client sites without performing installation + [string]$SiteName, # For actual installation, pass the selected site Name + [string]$SiteUID # For actual installation, pass the selected site UID + ) + + + # Ensure mandatory parameters are provided + if (-not $ApiUrl -or -not $ApiKey -or -not $ApiSecretKey) { + Write-Log -Message "Missing required parameters. Please provide ApiUrl, ApiKey, and ApiSecretKey." -Level "Error" -LogToEvent + return + } + + # Enable secure protocols + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + + # Step 1: Fetch OAuth token + Write-Log -Message "Fetching OAuth token..." -Level "Info" + try { + $securePassword = ConvertTo-SecureString -String 'public' -AsPlainText -Force + $apiGenToken = Invoke-WebRequest -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('public-client', $securePassword)) ` + -Uri ('{0}/auth/oauth/token' -f $ApiUrl) ` + -Method 'POST' ` + -ContentType 'application/x-www-form-urlencoded' ` + -Body ('grant_type=password&username={0}&password={1}' -f $ApiKey, $ApiSecretKey) ` + | ConvertFrom-Json + $requestToken = $apiGenToken.access_token + Write-Log -Message "OAuth token fetched successfully." -Level "Success" -LogToEvent + } catch { + Write-Log -Message "Failed to fetch OAuth token. Details: $($_.Exception.Message)" -Level "Error" -LogToEvent + return + } + + # Set headers for the API request + $getHeaders = @{"Authorization" = "Bearer $requestToken"} + + # Step 2: Fetch list of sites + if ($FetchSitesOnly) { + Write-Host "Fetching list of sites from the Datto RMM API..." -ForegroundColor Cyan + try { + $getHeaders = @{"Authorization" = "Bearer $requestToken" } + $getSites = Invoke-WebRequest -Uri "$ApiUrl/api/v2/account/sites" -Method Get -Headers $getHeaders -ContentType "application/json" + $sitesJson = $getSites.Content | ConvertFrom-Json + $siteList = $sitesJson.sites | ForEach-Object { + [PSCustomObject]@{ + Name = $_.name + UID = $_.uid + } + } + Write-Host "Successfully fetched list of sites." -ForegroundColor Green + return $siteList + } + catch { + Write-Host "Failed to fetch sites from the API. Details: $($_.Exception.Message)" -ForegroundColor Red + return + } + } +} + +# Add below or near Install-DattoRMM-Helper +function Fetch-Sites { + param ( + [string]$ApiUrl = "https://example.com/api/sites", # Replace with your actual API URL + [string]$ApiKey = "YourAPIKey", # Replace with your actual API Key + [string]$ApiSecretKey = "YourSecretKey" # Replace with your actual Secret Key + ) + + # Check if Install-DattoRMM exists + if (-not (Get-Command -Name Install-DattoRMM -CommandType Function -ErrorAction SilentlyContinue)) { + Write-Host "Install-DattoRMM function not found. Using helper function." -ForegroundColor Yellow + + # Use the helper function + $sites = Install-DattoRMM-Helper -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly + } + else { + Write-Host "Using the existing Install-DattoRMM function." -ForegroundColor Green + + # Use the existing function + $sites = Install-DattoRMM -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly + } + + # Handle the fetched sites + if ($sites) { + Write-Host "Fetched Sites:" -ForegroundColor Cyan + $sites | ForEach-Object { + Write-Host "Site Name: $_.Name, Site UID: $_.UID" + } + } + else { + Write-Host "Failed to fetch sites. Please check your API credentials or connection." -ForegroundColor Red + } +} + + + #endregion #region SVS Module @@ -1353,7 +1456,7 @@ try { $password = $data.password Get-N8nWebhookData -AuthHeaderValue $password - $sites = Install-DattoRMM -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly + $sites = Fetch-Sites -ApiUrl $ApiUrl -ApiKey $ApiKey -ApiSecretKey $ApiSecretKey -FetchSitesOnly if (-not $sites) { Write-Host "No sites returned. Please check the API." -ForegroundColor Red $response.StatusCode = 500