From ada21b427bde5958e2bcf200b08f22c87b40f578 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Thu, 27 Nov 2025 00:19:04 -0500 Subject: [PATCH] Update samy.ps1 --- samy.ps1 | 81 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 11 deletions(-) diff --git a/samy.ps1 b/samy.ps1 index b2fcce8..1132b6a 100644 --- a/samy.ps1 +++ b/samy.ps1 @@ -381,19 +381,78 @@ $ConfirmPreference = 'None' } } + function Repair-SVSMspEventLogBinding { + param( + [string]$EventSource = "SVSMSP_Module", + [string]$TargetLog = "SVSMSP Events" + ) + + Write-LogHybrid "Checking Event Log binding for source '$EventSource'..." Info SVSModule -LogToEvent + + # 1) Make sure the source exists + try { + if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) { + Write-LogHybrid "Event source '$EventSource' not found. Nothing to repair." Info SVSModule -LogToEvent + return + } + + $currentLog = [System.Diagnostics.EventLog]::LogNameFromSourceName($EventSource, '.') + } + catch { + Write-LogHybrid "Failed to query Event Log binding for '$EventSource': $($_.Exception.Message)" Warning SVSModule -LogToEvent + return + } + + if (-not $currentLog) { + Write-LogHybrid "Could not determine current log for event source '$EventSource'. Skipping repair." Warning SVSModule -LogToEvent + return + } + + # 2) If it's already correct, bail out + if ($currentLog -eq $TargetLog) { + Write-LogHybrid "Event source '$EventSource' already bound to '$TargetLog'." Info SVSModule -LogToEvent + return + } + + Write-LogHybrid "Rebinding event source '$EventSource' from '$currentLog' to '$TargetLog'..." Warning SVSModule -LogToEvent + + # 3) Delete and recreate the source bound to the desired log + try { + [System.Diagnostics.EventLog]::DeleteEventSource($EventSource) + + if (-not [System.Diagnostics.EventLog]::Exists($TargetLog)) { + New-EventLog -LogName $TargetLog -Source $EventSource -ErrorAction Stop + } + else { + New-EventLog -LogName $TargetLog -Source $EventSource -ErrorAction Stop + } + + Write-LogHybrid "Event source '$EventSource' rebound to '$TargetLog'." Success SVSModule -LogToEvent + } + catch { + Write-LogHybrid "Failed to rebind event source '$EventSource' to log '$TargetLog': $($_.Exception.Message)" Error SVSModule -LogToEvent + } + } - function Perform-ToolkitInstallation { - Initialize-NuGetProvider - Perform-Cleanup - Write-LogHybrid "Registering repo $NewRepositoryName…" "Info" "SVSModule" -LogToEvent - if (-not (Get-PSRepository -Name $NewRepositoryName -ErrorAction SilentlyContinue)) { - Register-PSRepository -Name $NewRepositoryName -SourceLocation $NewRepositoryURL -InstallationPolicy Trusted - } - Write-LogHybrid "Installing module $NewModuleName…" "Info" "SVSModule" -LogToEvent - Install-Module -Name $NewModuleName -Repository $NewRepositoryName -Scope AllUsers -Force - Write-LogHybrid "Toolkit installation complete." "Success" "SVSModule" -LogToEvent - } + function Perform-ToolkitInstallation { + Initialize-NuGetProvider + Perform-Cleanup + + Write-LogHybrid "Registering repo $NewRepositoryName…" "Info" "SVSModule" -LogToEvent + if (-not (Get-PSRepository -Name $NewRepositoryName -ErrorAction SilentlyContinue)) { + Register-PSRepository -Name $NewRepositoryName -SourceLocation $NewRepositoryURL -InstallationPolicy Trusted + } + + Write-LogHybrid "Installing module $NewModuleName…" "Info" "SVSModule" -LogToEvent + Install-Module -Name $NewModuleName -Repository $NewRepositoryName -Scope AllUsers -Force + + # After module install, repair Event Log binding for legacy systems + Repair-SVSMspEventLogBinding -EventSource "SVSMSP_Module" -TargetLog "SVSMSP Events" + + Write-LogHybrid "Toolkit installation complete." "Success" "SVSModule" -LogToEvent + } + Write-LogHybrid "Install-SVSMSP called" "Info" "SVSModule" -LogToEvent if ($Cleanup) {