fix: switch to System.Collections.ArrayList for robust collection handling

This commit is contained in:
2025-12-22 16:39:40 +01:00
parent e8514a8640
commit d3ade86eec

View File

@@ -70,8 +70,8 @@ function Get-InstalledSoftware {
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
) )
# Use Generic List to avoid "Hashtable can only be added to another" errors # Use ArrayList for maximum compatibility and to avoid fixed-size array errors
$list = New-Object System.Collections.Generic.List[PSObject] $list = New-Object System.Collections.ArrayList
foreach ($path in $paths) { foreach ($path in $paths) {
$items = Get-ItemProperty $path -ErrorAction SilentlyContinue $items = Get-ItemProperty $path -ErrorAction SilentlyContinue
@@ -85,7 +85,7 @@ function Get-InstalledSoftware {
DisplayName = $name DisplayName = $name
UninstallString = $cmd UninstallString = $cmd
} }
$list.Add($obj) [void]$list.Add($obj)
} }
} }
} }
@@ -148,8 +148,8 @@ function Invoke-RemovePrinters {
Write-Host "`nStarting Cleanup..." -ForegroundColor Yellow Write-Host "`nStarting Cleanup..." -ForegroundColor Yellow
$removedPrinters = New-Object System.Collections.Generic.List[string] $removedPrinters = New-Object System.Collections.ArrayList
$removedDrivers = New-Object System.Collections.Generic.List[string] $removedDrivers = New-Object System.Collections.ArrayList
# 1. Remove Printers # 1. Remove Printers
try { try {
@@ -164,7 +164,7 @@ function Invoke-RemovePrinters {
Write-Host "[-] Removing Printer: $($p.Name)" Write-Host "[-] Removing Printer: $($p.Name)"
try { try {
Remove-Printer -Name $p.Name -ErrorAction Stop Remove-Printer -Name $p.Name -ErrorAction Stop
$removedPrinters.Add($p.Name) [void]$removedPrinters.Add($p.Name)
} catch { } catch {
Write-Host " [!] Failed to remove $($p.Name): $($_.Exception.Message)" -ForegroundColor Red Write-Host " [!] Failed to remove $($p.Name): $($_.Exception.Message)" -ForegroundColor Red
} }
@@ -191,7 +191,7 @@ function Invoke-RemovePrinters {
Write-Host "[-] Removing Driver: $($d.Name)" Write-Host "[-] Removing Driver: $($d.Name)"
try { try {
Remove-PrinterDriver -Name $d.Name -ErrorAction Stop Remove-PrinterDriver -Name $d.Name -ErrorAction Stop
$removedDrivers.Add($d.Name) [void]$removedDrivers.Add($d.Name)
} catch { } catch {
Write-Host " [!] Locked or in use (skipping): $($d.Name)" -ForegroundColor DarkGray Write-Host " [!] Locked or in use (skipping): $($d.Name)" -ForegroundColor DarkGray
} }
@@ -239,14 +239,14 @@ function Invoke-UninstallSoftware {
$allSoftware = Get-InstalledSoftware $allSoftware = Get-InstalledSoftware
# Use Generic List to avoid type issues # Use ArrayList to avoid type issues
$matches = New-Object System.Collections.Generic.List[PSObject] $matches = New-Object System.Collections.ArrayList
# Filter software list # Filter software list
foreach ($sw in $allSoftware) { foreach ($sw in $allSoftware) {
foreach ($brand in $brands) { foreach ($brand in $brands) {
if ($sw.DisplayName -match "(?i)\b$brand\b") { # Case insensitive regex match if ($sw.DisplayName -match "(?i)\b$brand\b") { # Case insensitive regex match
$matches.Add($sw) [void]$matches.Add($sw)
break break
} }
} }