uninstall.ps1 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # uninstall.ps1 - CommModifyKit 驱动卸载脚本
  2. #Requires -RunAsAdministrator
  3. $ErrorActionPreference = 'Continue'
  4. Write-Host '=== CommModifyKit 卸载脚本 ===' -ForegroundColor Cyan
  5. Write-Host ""
  6. Write-Host "通过 pnputil 删除驱动包..." -ForegroundColor Yellow
  7. try {
  8. $raw = pnputil /enum-drivers 2>&1
  9. # pnputil 输出格式:
  10. # Published Name: oem0.inf
  11. # Original Name: CommModifyKit.inf
  12. # ...
  13. # 需要找到 "Original Name: CommModifyKit.inf" 对应的 "Published Name: oem*.inf"
  14. $lines = $raw -split "`r?`n"
  15. $infs = @()
  16. $currentOem = $null
  17. for ($i=0; $i -lt $lines.Count; $i++) {
  18. if ($lines[$i] -match 'Published Name:\s+(oem\d+\.inf)') {
  19. $currentOem = $matches[1]
  20. }
  21. if ($lines[$i] -match 'Original Name:\s+CommModifyKit\.inf') {
  22. if ($currentOem) {
  23. $infs += $currentOem
  24. }
  25. }
  26. }
  27. if ($infs.Count -gt 0) {
  28. foreach ($inf in $infs) {
  29. Write-Host " 删除: $inf"
  30. pnputil /delete-driver $inf /uninstall /force 2>&1 | Out-Null
  31. if ($LASTEXITCODE -eq 0) { Write-Host " 完成" -ForegroundColor Green }
  32. else { Write-Warning " 失败" }
  33. }
  34. } else { Write-Host " 未找到驱动包" -ForegroundColor Green }
  35. } catch { Write-Warning "pnputil 失败: $_" }
  36. $srv = Get-Service -Name 'CommModifyKit' -ErrorAction SilentlyContinue
  37. if ($srv) {
  38. Write-Host ""
  39. if ($srv.Status -eq 'Running') {
  40. sc.exe stop CommModifyKit 2>&1 | Out-Null
  41. Start-Sleep 2
  42. # 检查是否真的停止了
  43. $svcAfter = Get-Service -Name 'CommModifyKit' -ErrorAction SilentlyContinue
  44. if ($svcAfter -and $svcAfter.Status -eq 'Running') {
  45. Write-Warning "驱动服务无法停止(设备栈引用中),需要重启后才能完全卸载"
  46. }
  47. }
  48. sc.exe delete CommModifyKit 2>&1 | Out-Null
  49. Write-Host "服务已删除" -ForegroundColor Green
  50. }
  51. # 从 Ports 类 UpperFilters 移除 CommModifyKit
  52. # 同时过滤空字符串(历史遗留的空字符串会导致 PnP 加载空名称过滤器失败)
  53. $PortsClassGuid = "{4D36E978-E325-11CE-BFC1-08002BE10318}"
  54. $ClassKey = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\$PortsClassGuid"
  55. $existingFilters = (Get-ItemProperty -Path $ClassKey -Name "UpperFilters" -ErrorAction SilentlyContinue)
  56. if ($existingFilters -and $existingFilters.UpperFilters) {
  57. $currentFilters = @($existingFilters.UpperFilters | Where-Object { $_ -and $_.Trim() -ne '' })
  58. if ($currentFilters -contains "CommModifyKit") {
  59. # 移除 CommModifyKit,同时清理任何残留空字符串
  60. $newFilters = @($currentFilters | Where-Object { $_ -ne "CommModifyKit" })
  61. if ($newFilters.Count -eq 0) {
  62. # 必须删除值,不能设为空字符串(空字符串会导致 PnP 加载空名称过滤器失败)
  63. Remove-ItemProperty -Path $ClassKey -Name "UpperFilters" -ErrorAction SilentlyContinue
  64. } else {
  65. Set-ItemProperty -Path $ClassKey -Name "UpperFilters" -Value $newFilters -Type MultiString
  66. }
  67. Write-Host "已从 Ports 类 UpperFilters 移除 CommModifyKit" -ForegroundColor Green
  68. # 触发设备重枚举
  69. pnputil /scan-devices 2>&1 | Out-Null
  70. Write-Host "已触发设备重新枚举" -ForegroundColor Green
  71. }
  72. }
  73. # 驱动文件路径(32 位 PowerShell 需用 Sysnative 绕过 WoW64 重定向)
  74. $driverDir = if ([System.IntPtr]::Size -eq 4) {
  75. Join-Path $env:windir "Sysnative\drivers"
  76. } else {
  77. Join-Path $env:windir "System32\drivers"
  78. }
  79. $sys = Join-Path $driverDir "CommModifyKit.sys"
  80. $inf = Join-Path $driverDir "CommModifyKit.inf"
  81. Write-Host ""
  82. if (Test-Path $sys) { Remove-Item $sys -Force -ErrorAction SilentlyContinue }
  83. if (Test-Path $inf) { Remove-Item $inf -Force -ErrorAction SilentlyContinue }
  84. Write-Host "驱动文件已删除" -ForegroundColor Green
  85. # 删除测试证书
  86. $testCert = Get-ChildItem -Path Cert:\LocalMachine\My -ErrorAction SilentlyContinue | Where-Object { $_.Subject -match "CommModifyKitTest" }
  87. if ($testCert) {
  88. $testCert | Remove-Item -Force -ErrorAction SilentlyContinue
  89. Write-Host "测试证书已从 My 存储删除" -ForegroundColor Green
  90. }
  91. $rootCert = Get-ChildItem -Path Cert:\LocalMachine\Root -ErrorAction SilentlyContinue | Where-Object { $_.Subject -match "CommModifyKitTest" }
  92. if ($rootCert) {
  93. $rootCert | Remove-Item -Force -ErrorAction SilentlyContinue
  94. Write-Host "测试证书已从 Root 存储删除" -ForegroundColor Green
  95. }
  96. Write-Host ""
  97. Write-Host '=== 卸载完成 ===' -ForegroundColor Cyan
  98. Write-Host ""
  99. Write-Host "如果服务无法停止或串口仍不可用,请重启计算机" -ForegroundColor Yellow
  100. Write-Host " 重启命令: shutdown /r /t 0" -ForegroundColor Yellow
  101. Write-Host ""
  102. Write-Host "注意: 测试签名模式仍保持开启,如需关闭请执行:" -ForegroundColor Yellow
  103. Write-Host " bcdedit /set testsigning off" -ForegroundColor Yellow
  104. Write-Host " 然后重启计算机" -ForegroundColor Yellow