From c4a5d4246221ffcca7a2eb6fda1a29c75518a492 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Fri, 31 Jan 2025 16:39:05 -0500 Subject: [PATCH] Update SVSTaskGate.ps1 --- SVSTaskGate.ps1 | 318 +++++++++++++++++++++++++++++------------------- 1 file changed, 195 insertions(+), 123 deletions(-) diff --git a/SVSTaskGate.ps1 b/SVSTaskGate.ps1 index b378e72..98a3b1e 100644 --- a/SVSTaskGate.ps1 +++ b/SVSTaskGate.ps1 @@ -8,10 +8,6 @@ ### 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 - - # --------------------------------------------------------------------------- # 1) CREATE A GLOBAL LOG CACHE (NEW) @@ -222,12 +218,22 @@ function Install-DattoRMM-Helper { #region SVS Module +### SVS Module Overview: +# - This section includes the Install-SVSMSP function, which handles: +# 1. Cleanup of old modules and repositories. +# 2. Installation of new modules and repositories. +# 3. Setting execution policies and prerequisites. +# - TODO: +# 1. Validate all parameters for completeness and add error messages for missing inputs. +# 2. Refactor cleanup and installation steps for modularity and reusability. + + function Install-SVSMSP { param ( - # Cleanup flag + # Cleanup flag: Removes old modules and repositories if enabled. [switch]$Cleanup, - # Toolkit installation flag + # Toolkit installation flag: Installs required modules and repositories. [switch]$InstallToolkit, # Module settings @@ -265,21 +271,15 @@ function Install-SVSMSP { ), - # Log file path + # Log file path for tracking installation steps [Parameter(Mandatory = $false)] - [string]$LogFilePath = "$env:SVSMSP\svstoolkit.log", + [string]$LogFilePath = "$env:SVSMSP\svstoolkit.log" - # DRMM API Settings - [Parameter(Mandatory = $false)] - [string]$ApiUrl, - - [Parameter(Mandatory = $false)] - [string]$ApiKey, - - [Parameter(Mandatory = $false)] - [string]$ApiSecretKey ) - + + ### Function: Perform-Cleanup + # - Removes all old modules and repositories. + # - Logs each step for debugging purposes. function Perform-Cleanup { Write-LogHybrid -Message "Cleanup mode enabled. Starting cleanup process..." -Level "Info" -LogToEvent @@ -326,11 +326,15 @@ function Install-SVSMSP { Write-LogHybrid -Message "Cleanup process completed successfully." -Level "Success" -LogToEvent } + ### Function: Perform-ToolkitInstallation + # - Handles the installation of new repositories and modules. + # - Ensures execution policies are correctly set. function Perform-ToolkitInstallation { - # Perform cleanup + + # Step 1: Cleanup old modules and repositories Perform-Cleanup - # Step 1: Set Execution Policy + # Step 2: Set Execution Policy $localMachineExecutionPolicy = Get-ExecutionPolicy -Scope LocalMachine if ($localMachineExecutionPolicy -ne "RemoteSigned") { Write-LogHybrid -Message "Setting execution policy to RemoteSigned..." -Level "Warning" -LogToEvent @@ -344,20 +348,10 @@ function Install-SVSMSP { } } - # Step 2: Ensure NuGet is Installed - #if (!(Get-PackageProvider -Name "NuGet" -ErrorAction SilentlyContinue)) { - # Write-LogHybrid -Message "NuGet package provider not found. Installing..." -Level "Warning" -LogToEvent - # try { - Install-PackageProvider -Name "NuGet" -Force -Scope AllUsers -Confirm:$false - # Write-LogHybrid -Message "NuGet package provider installed successfully." -Level "Success" -LogToEvent - # } - # catch { - # Write-LogHybrid -Message "Failed to install NuGet package provider. Error: $_" -Level "Error" -LogToEvent - # return - # } - #} - - # Step 3: Register the new repository + # Step 3: Ensure NuGet is Installed + Install-PackageProvider -Name "NuGet" -Force -Scope AllUsers -Confirm:$false + + # Step 4: Register the new repository Write-LogHybrid -Message "Registering the new repository '$NewRepositoryName'..." -Level "Info" -LogToEvent try { if (!(Get-PSRepository -Name $NewRepositoryName -ErrorAction SilentlyContinue)) { @@ -369,7 +363,7 @@ function Install-SVSMSP { Write-LogHybrid -Message "Failed to register new repository '$NewRepositoryName'. Error: $($_.Exception.Message)" -Level "Error" -LogToEvent } - # Step 4: Install the new module + # Step 5: Install the new module Write-LogHybrid -Message "Installing the new module '$NewModuleName'..." -Level "Info" -LogToEvent try { Install-Module -Name $NewModuleName -Repository $NewRepositoryName -Scope AllUsers -Force @@ -382,6 +376,8 @@ function Install-SVSMSP { Write-LogHybrid -Message "Toolkit installation process completed successfully." -Level "Success" -LogToEvent } + ### Function Execution Logic + # - Determines whether to run cleanup, toolkit installation, or default to toolkit installation mode. Write-LogHybrid -Message "Install-SVSMSP function started." -Level "Info" -LogToEvent if ($Cleanup) { @@ -400,11 +396,39 @@ function Install-SVSMSP { #endregion SVS Module # ---------------------------------------------------------------------------------- -# START THE LISTENER +# START THE HTTP LISTENER # ---------------------------------------------------------------------------------- -$listener = New-Object System.Net.HttpListener -$listener.Prefixes.Add("http://localhost:8081/") -$listener.Start() +# This listener serves as the backend for handling requests from the GUI. +# It supports multiple routes, such as: +# - "/" (Root): Serves the HTML GUI. +# - "/getn8npw": Fetches n8n password and site information. +# - "/installSVSMSPModule": Triggers the installation of SVSMSP modules. +# - "/installrmm": Handles RMM installation with dynamic parameters. +# - Additional routes for tweaks and other tasks. + +### Listener Initialization with Error Handling +try { + # Create a new HttpListener object + $listener = New-Object System.Net.HttpListener + + # Check if the object was successfully created + if (-not $listener) { + throw "Failed to initialize HttpListener." + } + + # Add prefix for the listener + $listener.Prefixes.Add("http://localhost:8081/") + Write-LogHybrid -Message "Listener initialized with prefix http://localhost:8081/" -Level "Info" + + # Start the listener + $listener.Start() + Write-LogHybrid -Message "Listener started successfully." -Level "Info" +} catch { + # Log the error and rethrow it for visibility + Write-LogHybrid -Message "Critical error initializing listener: $($_.Exception.Message)" -Level "Error" + throw $_ +} + function Get-N8nWebhookData { @@ -903,73 +927,64 @@ function GetHtmlContent {

Logs will appear here...

+