# Install-ClassNotify.ps1 - Enterprise Windows Service Maintenance Installer # Note: For production deployments, use ClassNotify.Setup.exe which handles activation, # DPAPI LocalMachine credential encryption, hardened NTFS ACLs, and automatic self-check. param( [string]$ClassId = "801", [string]$ClassName = "八年级(1)班", [string]$ServerUrl = "https://notice.yourdomain.com/hubs/device" ) # Ensure running as Administrator 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 V2.0 商业级服务端部署程序 " -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan $InstallDir = "C:\ProgramData\ClassNotify" $SourceDir = Split-Path -Parent $MyInvocation.MyCommand.Path # 1. Prepare Directory Write-Host "[1/4] 创建企业级安装目录: $InstallDir ..." -ForegroundColor Yellow if (-not (Test-Path $InstallDir)) { New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null } $logDir = Join-Path $InstallDir "logs" if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Force -Path $logDir | Out-Null } # 2. Copy Binaries Write-Host "[2/4] 部署服务与客户端二进制文件..." -ForegroundColor Yellow $binaries = @("ClassNotify.Service.exe", "ClassNotify.Client.exe", "ClassNotify.Shared.dll") foreach ($bin in $binaries) { $src = Join-Path $SourceDir $bin if (Test-Path $src) { Copy-Item -Path $src -Destination $InstallDir -Force } } # 3. Generate AppSettings (No plaintext secret tokens! Credentials managed via DPAPI) Write-Host "[3/4] 写入服务运行配置 appsettings.json..." -ForegroundColor Yellow $configObj = @{ Logging = @{ LogLevel = @{ Default = "Information" "Microsoft.Hosting.Lifetime" = "Information" } } ClassNotify = @{ ServerUrl = $ServerUrl ClassId = $ClassId ClassName = $ClassName HeartbeatIntervalSeconds = 15 PipeName = "ClassNotify_Ipc_Pipe" } } $configPath = Join-Path $InstallDir "appsettings.json" $configObj | ConvertTo-Json -Depth 5 | Set-Content -Path $configPath -Encoding UTF8 # 4. Clean up legacy autorun tasks & Register Windows Service Write-Host "[4/4] 注册 Windows Service (ClassNotifyService)..." -ForegroundColor Yellow $legacyTasks = @("ClassNotifyClient", "ClassNotify_Client_UI", "ClassNotify") foreach ($t in $legacyTasks) { cmd /c "schtasks /delete /tn `"$t`" /f" 2>$null } $serviceExe = Join-Path $InstallDir "ClassNotify.Service.exe" $existingService = Get-Service -Name "ClassNotifyService" -ErrorAction SilentlyContinue if ($existingService) { Stop-Service -Name "ClassNotifyService" -Force -ErrorAction SilentlyContinue sc.exe delete ClassNotifyService | Out-Null Start-Sleep -Seconds 1 } 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 (Service watchdog will securely launch Client with ephemeral token) Start-Service -Name "ClassNotifyService" -ErrorAction SilentlyContinue Write-Host "`n=========================================" -ForegroundColor Green Write-Host " [成功] ClassNotify V2.0 商业级部署完成!" -ForegroundColor Green Write-Host " 服务名称: ClassNotifyService (自动重启看门狗已启用)" -ForegroundColor Green Write-Host " 教室编号: $ClassName ($ClassId)" -ForegroundColor Green Write-Host "=========================================" -ForegroundColor Green