fix: replace array concatenation with Generic List to resolve Hashtable error

This commit is contained in:
2025-12-22 16:34:25 +01:00
parent 37e8d8d463
commit 161e13d997

View File

@@ -70,9 +70,10 @@ function Get-InstalledSoftware {
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
) )
# Use array subexpression to safely collect output objects. # Use Generic List to avoid "Hashtable can only be added to another" errors
# This avoids the "Hash table" addition error because we aren't using += on potentially mixed types. $list = New-Object System.Collections.Generic.List[PSObject]
$list = @(foreach ($path in $paths) {
foreach ($path in $paths) {
$items = Get-ItemProperty $path -ErrorAction SilentlyContinue $items = Get-ItemProperty $path -ErrorAction SilentlyContinue
foreach ($item in $items) { foreach ($item in $items) {
# FORCE STRING CONVERSION IMMEDIATELY # FORCE STRING CONVERSION IMMEDIATELY
@@ -80,14 +81,14 @@ function Get-InstalledSoftware {
$cmd = [string]$item.UninstallString $cmd = [string]$item.UninstallString
if (-not [string]::IsNullOrWhiteSpace($name) -and -not [string]::IsNullOrWhiteSpace($cmd)) { if (-not [string]::IsNullOrWhiteSpace($name) -and -not [string]::IsNullOrWhiteSpace($cmd)) {
# Output object to pipeline (collected by @()) $obj = [PSCustomObject]@{
[PSCustomObject]@{
DisplayName = $name DisplayName = $name
UninstallString = $cmd UninstallString = $cmd
} }
$list.Add($obj)
}
} }
} }
})
return $list return $list
} }
@@ -147,8 +148,8 @@ function Invoke-RemovePrinters {
Write-Host "`nStarting Cleanup..." -ForegroundColor Yellow Write-Host "`nStarting Cleanup..." -ForegroundColor Yellow
$removedPrinters = @() $removedPrinters = New-Object System.Collections.Generic.List[string]
$removedDrivers = @() $removedDrivers = New-Object System.Collections.Generic.List[string]
# 1. Remove Printers # 1. Remove Printers
try { try {
@@ -163,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 += $p.Name $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
} }
@@ -190,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 += $d.Name $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
} }
@@ -238,14 +239,14 @@ function Invoke-UninstallSoftware {
$allSoftware = Get-InstalledSoftware $allSoftware = Get-InstalledSoftware
# Simple array, no ArrayList to avoid potential type issues # Use Generic List to avoid type issues
$matches = @() $matches = New-Object System.Collections.Generic.List[PSObject]
# 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 += $sw $matches.Add($sw)
break break
} }
} }