# uninstall.ps1 - CommModifyKit 驱动卸载脚本 #Requires -RunAsAdministrator $ErrorActionPreference = 'Continue' Write-Host '=== CommModifyKit 卸载脚本 ===' -ForegroundColor Cyan Write-Host "" Write-Host "通过 pnputil 删除驱动包..." -ForegroundColor Yellow try { $raw = pnputil /enum-drivers 2>&1 # pnputil 输出格式: # Published Name: oem0.inf # Original Name: CommModifyKit.inf # ... # 需要找到 "Original Name: CommModifyKit.inf" 对应的 "Published Name: oem*.inf" $lines = $raw -split "`r?`n" $infs = @() $currentOem = $null for ($i=0; $i -lt $lines.Count; $i++) { if ($lines[$i] -match 'Published Name:\s+(oem\d+\.inf)') { $currentOem = $matches[1] } if ($lines[$i] -match 'Original Name:\s+CommModifyKit\.inf') { if ($currentOem) { $infs += $currentOem } } } if ($infs.Count -gt 0) { foreach ($inf in $infs) { Write-Host " 删除: $inf" pnputil /delete-driver $inf /uninstall /force 2>&1 | Out-Null if ($LASTEXITCODE -eq 0) { Write-Host " 完成" -ForegroundColor Green } else { Write-Warning " 失败" } } } else { Write-Host " 未找到驱动包" -ForegroundColor Green } } catch { Write-Warning "pnputil 失败: $_" } $srv = Get-Service -Name 'CommModifyKit' -ErrorAction SilentlyContinue if ($srv) { Write-Host "" if ($srv.Status -eq 'Running') { sc.exe stop CommModifyKit 2>&1 | Out-Null Start-Sleep 2 # 检查是否真的停止了 $svcAfter = Get-Service -Name 'CommModifyKit' -ErrorAction SilentlyContinue if ($svcAfter -and $svcAfter.Status -eq 'Running') { Write-Warning "驱动服务无法停止(设备栈引用中),需要重启后才能完全卸载" } } sc.exe delete CommModifyKit 2>&1 | Out-Null Write-Host "服务已删除" -ForegroundColor Green } # 从 Ports 类 UpperFilters 移除 CommModifyKit # 同时过滤空字符串(历史遗留的空字符串会导致 PnP 加载空名称过滤器失败) $PortsClassGuid = "{4D36E978-E325-11CE-BFC1-08002BE10318}" $ClassKey = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\$PortsClassGuid" $existingFilters = (Get-ItemProperty -Path $ClassKey -Name "UpperFilters" -ErrorAction SilentlyContinue) if ($existingFilters -and $existingFilters.UpperFilters) { $currentFilters = @($existingFilters.UpperFilters | Where-Object { $_ -and $_.Trim() -ne '' }) if ($currentFilters -contains "CommModifyKit") { # 移除 CommModifyKit,同时清理任何残留空字符串 $newFilters = @($currentFilters | Where-Object { $_ -ne "CommModifyKit" }) if ($newFilters.Count -eq 0) { # 必须删除值,不能设为空字符串(空字符串会导致 PnP 加载空名称过滤器失败) Remove-ItemProperty -Path $ClassKey -Name "UpperFilters" -ErrorAction SilentlyContinue } else { Set-ItemProperty -Path $ClassKey -Name "UpperFilters" -Value $newFilters -Type MultiString } Write-Host "已从 Ports 类 UpperFilters 移除 CommModifyKit" -ForegroundColor Green # 触发设备重枚举 pnputil /scan-devices 2>&1 | Out-Null Write-Host "已触发设备重新枚举" -ForegroundColor Green } } # 驱动文件路径(32 位 PowerShell 需用 Sysnative 绕过 WoW64 重定向) $driverDir = if ([System.IntPtr]::Size -eq 4) { Join-Path $env:windir "Sysnative\drivers" } else { Join-Path $env:windir "System32\drivers" } $sys = Join-Path $driverDir "CommModifyKit.sys" $inf = Join-Path $driverDir "CommModifyKit.inf" Write-Host "" if (Test-Path $sys) { Remove-Item $sys -Force -ErrorAction SilentlyContinue } if (Test-Path $inf) { Remove-Item $inf -Force -ErrorAction SilentlyContinue } Write-Host "驱动文件已删除" -ForegroundColor Green # 删除测试证书 $testCert = Get-ChildItem -Path Cert:\LocalMachine\My -ErrorAction SilentlyContinue | Where-Object { $_.Subject -match "CommModifyKitTest" } if ($testCert) { $testCert | Remove-Item -Force -ErrorAction SilentlyContinue Write-Host "测试证书已从 My 存储删除" -ForegroundColor Green } $rootCert = Get-ChildItem -Path Cert:\LocalMachine\Root -ErrorAction SilentlyContinue | Where-Object { $_.Subject -match "CommModifyKitTest" } if ($rootCert) { $rootCert | Remove-Item -Force -ErrorAction SilentlyContinue Write-Host "测试证书已从 Root 存储删除" -ForegroundColor Green } Write-Host "" Write-Host '=== 卸载完成 ===' -ForegroundColor Cyan Write-Host "" Write-Host "如果服务无法停止或串口仍不可用,请重启计算机" -ForegroundColor Yellow Write-Host " 重启命令: shutdown /r /t 0" -ForegroundColor Yellow Write-Host "" Write-Host "注意: 测试签名模式仍保持开启,如需关闭请执行:" -ForegroundColor Yellow Write-Host " bcdedit /set testsigning off" -ForegroundColor Yellow Write-Host " 然后重启计算机" -ForegroundColor Yellow