# Repair-ClassNotify.ps1 - One-click enterprise diagnosis & self-healing script if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Error "请以管理员身份运行此脚本!(Run as Administrator)" exit 1 } Write-Host "=========================================" -ForegroundColor Cyan Write-Host " ClassNotify 一键自动化诊断与修复工具 " -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan $InstallDir = "C:\ProgramData\ClassNotify" $configPath = Join-Path $InstallDir "appsettings.json" # 1. Check Service Write-Host "[1/4] 检查 Windows 服务状态..." -ForegroundColor Yellow $svc = Get-Service -Name "ClassNotifyService" -ErrorAction SilentlyContinue if (-not $svc) { Write-Host " [警告] ClassNotifyService 未安装,正在尝试重新注册..." -ForegroundColor Red $serviceExe = Join-Path $InstallDir "ClassNotify.Service.exe" if (Test-Path $serviceExe) { sc.exe create ClassNotifyService binPath= "`"$serviceExe`"" start= auto DisplayName= "ClassNotify 班级实时通信服务" | Out-Null sc.exe failure ClassNotifyService reset= 86400 actions= restart/60000/restart/60000/restart/60000 | Out-Null Start-Service -Name "ClassNotifyService" Write-Host " [恢复] 服务已重新注册并启动!" -ForegroundColor Green } } elseif ($svc.Status -ne "Running") { Write-Host " [警告] 服务当前处于停止状态 ($($svc.Status)),正在强制重启..." -ForegroundColor Yellow Restart-Service -Name "ClassNotifyService" -Force Write-Host " [恢复] 服务已重新启动!" -ForegroundColor Green } else { Write-Host " [正常] ClassNotifyService 运行正常。" -ForegroundColor Green } # 2. Check UI Process Write-Host "[2/4] 检查前台 UI 进程..." -ForegroundColor Yellow $uiProc = Get-Process -Name "ClassNotify.Client" -ErrorAction SilentlyContinue if (-not $uiProc) { Write-Host " [提示] 前台 UI 进程未运行。根据 Batch 8 安全规范,客户端必须由守护服务通过动态 Launch Token 安全拉起。" -ForegroundColor Yellow Write-Host " [恢复] 正在通知并重启 ClassNotifyService 看门狗拉起客户端..." -ForegroundColor Green Restart-Service -Name "ClassNotifyService" -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 } else { Write-Host " [正常] 前台 UI 进程运行正常。" -ForegroundColor Green } # 3. Check Network & SignalR Write-Host "[3/4] 检查云端连通性..." -ForegroundColor Yellow if (Test-Path $configPath) { try { $cfg = Get-Content -Path $configPath -Raw | ConvertFrom-Json $serverUrl = $cfg.ClassNotify.ServerUrl $uri = [System.Uri]$serverUrl $ip = [System.Net.Dns]::GetHostAddresses($uri.DnsSafeHost) if ($ip.Length -gt 0) { Write-Host " [正常] 云端域名解析成功 ($($uri.DnsSafeHost) -> $($ip[0]))" -ForegroundColor Green } } catch { Write-Host " [警告] 云端解析异常: $_" -ForegroundColor Red } } # 4. Check TTS Sound Engine Write-Host "[4/4] 测试本地 TTS 中文语音引擎..." -ForegroundColor Yellow try { Add-Type -AssemblyName System.Speech -ErrorAction Stop $synth = New-Object System.Speech.Synthesis.SpeechSynthesizer $voices = $synth.GetInstalledVoices() if ($voices.Count -gt 0) { Write-Host " [正常] 发现 $($voices.Count) 个系统语音包,TTS 引擎就绪。" -ForegroundColor Green } $synth.Dispose() } catch { Write-Host " [警告] TTS 引擎初始化失败: $_" -ForegroundColor Red } Write-Host "`n[诊断完成] 核心组件与自愈排错流程已执行完毕。" -ForegroundColor Cyan