diff --git a/SVSTaskGate.ps1 b/SVSTaskGate.ps1 index 24f7714..b378e72 100644 --- a/SVSTaskGate.ps1 +++ b/SVSTaskGate.ps1 @@ -5,22 +5,31 @@ ### add the .net silent install tweaks to toolkit ### for the reg tweak need to do/undo function maybe it should have it own check box list ### added offboard check boxes for dattormm, dattodeb, rocketcyber, cyberQP, SVSHelpdesk and Splashtop -### for the offboarding button, we need to fix the uninstall-svsmsp module ### need to fix path in the uninstall-DattoEDR - ####### ❌ [Error] [GeneralTask] Uninstallation command 'C:\Program Files\Infocyte\Agent\agent.exe' not found. (Event ID: 3000) - bad path +### need to have the fetch button check if Install-DattoRMM function exist if not run the build in helpder function to fetch sites +### need to move the tweaks to the tweeks tab + -#region Write-Log # --------------------------------------------------------------------------- # 1) CREATE A GLOBAL LOG CACHE (NEW) # --------------------------------------------------------------------------- +# - Global log cache stores logs for session-wide accessibility. +# - Logs are stored in a [System.Collections.ArrayList] for easy JSON conversion. +# - Ensure log cache integrity during session restarts. if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.ArrayList])) { $Global:LogCache = New-Object System.Collections.ArrayList } - -# Check if the Write-Log function exists +#region Write-LogHelper +# --------------------------------------------------------------------------- +# 2) DEFINE THE Write-LogHelper FUNCTION +# --------------------------------------------------------------------------- +# - Write-LogHelper manages logging with customizable levels, task categories, and optional event logging. +# - Supported log levels: Info, Warning, Error, Success, General. +# - Task categories should match high-level operations (e.g., "On-boarding", "Off-boarding"). if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction SilentlyContinue)) { # If the Write-Log function doesn't exist, create the Write-LogHelper function function Write-LogHelper { @@ -35,7 +44,7 @@ if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction Silentl [int]$CustomEventID # Optional custom Event ID ) - # Simplified Event ID mapping + # Simplified Event ID mapping for consistent logging $EventID = switch ($Level) { "Info" { 1000 } "Warning" { 2000 } @@ -53,7 +62,7 @@ if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction Silentl "General" { ([char]0x1F4E6) } # Package icon } - # Map levels to colors + # Map levels to colors for console output $Color = switch ($Level) { "Info" { "Cyan" } "Warning" { "Yellow" } @@ -66,8 +75,9 @@ if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction Silentl # Write-Host "$Icon [$Level] [$TaskCategory] $Message (Event ID: $EventID)" -ForegroundColor $Color # ------------------------------------------------------------------- - # 2) ALSO STORE THE LOG IN OUR GLOBAL LOG CACHE (NEW) + # 3) STORE LOGS IN GLOBAL CACHE # ------------------------------------------------------------------- + # - Cache format: Timestamp, Level, and Message for each log entry. $logEntry = [PSCustomObject]@{ Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") Level = $Level @@ -75,8 +85,11 @@ if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction Silentl } [void]$Global:LogCache.Add($logEntry) # ------------------------------------------------------------------- + ### TODO: Add support for exporting logs to a persistent file. + # Consider implementing a periodic export mechanism to save logs to a file. + # Example: Export-LogCacheToFile -Path "C:\Logs\TaskLogs.json" - # Optionally log to the Windows Event Log + # Optional: Log to Windows Event Log for system-wide visibility. if ($LogToEvent) { $EntryType = switch ($Level) { "Info" { "Information" } @@ -98,6 +111,8 @@ if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction Silentl } } + ### Hybrid Function: + # Wrapper for Write-LogHelper to simplify usage across modules. function Write-LogHybrid { param ( [string]$Message, @@ -136,10 +151,77 @@ 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 + } + } +} +#endregion + + + #region SVS Module - - function Install-SVSMSP { param ( # Cleanup flag @@ -197,10 +279,7 @@ function Install-SVSMSP { [Parameter(Mandatory = $false)] [string]$ApiSecretKey ) - - - - + function Perform-Cleanup { Write-LogHybrid -Message "Cleanup mode enabled. Starting cleanup process..." -Level "Info" -LogToEvent @@ -594,6 +673,7 @@ function GetHtmlContent {