Utoljára aktív 1 month ago

jifeiyun gist felülvizsgálása 1 month ago. Revízióhoz ugrás

1 file changed, 1578 insertions

Wps.ps1(fájl létrehozva)

@@ -0,0 +1,1578 @@
1 + # WPS Office 2023/2019 专业增强版安装工具 - 最终完整修复版
2 + # 彻底解决空值错误和路径查找问题
3 + # 编码格式: UTF-8 with BOM
4 +
5 + # 要求管理员权限
6 + if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
7 + Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
8 + exit
9 + }
10 +
11 + # 隐藏控制台窗口
12 + if (-not ("Console.Window" -as [type])) {
13 + Add-Type -Name Window -Namespace Console -MemberDefinition '
14 + [DllImport("Kernel32.dll")]
15 + public static extern IntPtr GetConsoleWindow();
16 + [DllImport("user32.dll")]
17 + public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
18 + '
19 + }
20 + $consolePtr = [Console.Window]::GetConsoleWindow()
21 + [Console.Window]::ShowWindow($consolePtr, 0) | Out-Null
22 +
23 + Add-Type -AssemblyName System.Windows.Forms
24 + Add-Type -AssemblyName System.Drawing
25 +
26 + [System.Windows.Forms.Application]::EnableVisualStyles()
27 +
28 + # =============================================
29 + # 全局变量
30 + # =============================================
31 + $global:isInstalling = $false
32 + $global:form = $null
33 + $global:installButton = $null
34 + $global:install2019Button = $null
35 + $global:logTextBox = $null
36 + $global:progressBar = $null
37 + $global:progressLabel = $null
38 + $global:lastProgressText = ""
39 + $global:selectedVersion = "2023" # 默认安装版本
40 +
41 + # =============================================
42 + # 颜色定义
43 + # =============================================
44 + $primaryColor = [System.Drawing.Color]::FromArgb(22, 115, 255) # 主色调
45 + $primaryHover = [System.Drawing.Color]::FromArgb(0, 95, 230) # 主色调悬停
46 + $primaryPressed = [System.Drawing.Color]::FromArgb(0, 86, 179) # 主色调按下
47 + $successColor = [System.Drawing.Color]::FromArgb(40, 167, 69) # 成功色
48 + $successHover = [System.Drawing.Color]::FromArgb(33, 147, 58) # 成功色悬停
49 + $warningColor = [System.Drawing.Color]::FromArgb(255, 193, 7) # 警告色
50 + $warningHover = [System.Drawing.Color]::FromArgb(230, 173, 6) # 警告色悬停
51 + $infoColor = [System.Drawing.Color]::FromArgb(23, 162, 184) # 信息色
52 + $infoHover = [System.Drawing.Color]::FromArgb(19, 142, 164) # 信息色悬停
53 + $dangerColor = [System.Drawing.Color]::FromArgb(220, 53, 69) # 危险色
54 + $dangerHover = [System.Drawing.Color]::FromArgb(200, 35, 51) # 危险色悬停
55 + $secondaryColor = [System.Drawing.Color]::FromArgb(108, 117, 125) # 次要色
56 + $secondaryHover = [System.Drawing.Color]::FromArgb(92, 99, 106) # 次要色悬停
57 + $lightColor = [System.Drawing.Color]::FromArgb(248, 249, 250) # 浅色背景
58 + $darkColor = [System.Drawing.Color]::FromArgb(52, 58, 64) # 深色背景
59 + $textPrimary = [System.Drawing.Color]::FromArgb(33, 37, 41) # 主要文字
60 + $textSecondary = [System.Drawing.Color]::FromArgb(108, 117, 125) # 次要文字
61 +
62 + # =============================================
63 + # 字体定义
64 + # =============================================
65 + $fontFamily = "微软雅黑"
66 + $fontTitle = New-Object System.Drawing.Font($fontFamily, 14, [System.Drawing.FontStyle]::Bold)
67 + $fontSubtitle = New-Object System.Drawing.Font($fontFamily, 10, [System.Drawing.FontStyle]::Regular)
68 + $fontButton = New-Object System.Drawing.Font($fontFamily, 10, [System.Drawing.FontStyle]::Bold)
69 + $fontButtonRegular = New-Object System.Drawing.Font($fontFamily, 10, [System.Drawing.FontStyle]::Regular)
70 + $fontLabel = New-Object System.Drawing.Font($fontFamily, 9, [System.Drawing.FontStyle]::Regular)
71 + $fontLog = New-Object System.Drawing.Font($fontFamily, 9, [System.Drawing.FontStyle]::Regular)
72 +
73 + # =============================================
74 + # 日志函数 - 修复版:不在控制台显示,只显示到软件界面
75 + # =============================================
76 + function Write-Log {
77 + param (
78 + [string]$Message,
79 + [string]$Type = "Info" # Info, Success, Warning, Error
80 + )
81 +
82 + $timestamp = Get-Date -Format "HH:mm:ss"
83 + $logMessage = "[$timestamp] $Message"
84 +
85 + # 注意:移除了控制台输出,只输出到软件界面
86 +
87 + # 尝试写入文本框
88 + if ($global:logTextBox -ne $null -and $global:logTextBox.IsHandleCreated) {
89 + try {
90 + $global:logTextBox.Invoke([Action]{
91 + # 检查是否需要先添加换行
92 + $currentText = $global:logTextBox.Text
93 + if ($currentText.Length -gt 0) {
94 + $lastChar = $currentText[-1]
95 + if ($lastChar -ne "`n" -and $lastChar -ne "`r") {
96 + $global:logTextBox.AppendText("`r`n")
97 + }
98 + }
99 +
100 + $global:logTextBox.SelectionStart = $global:logTextBox.TextLength
101 + $global:logTextBox.SelectionLength = 0
102 +
103 + switch ($Type) {
104 + "Success" { $global:logTextBox.SelectionColor = $successColor }
105 + "Warning" { $global:logTextBox.SelectionColor = $warningColor }
106 + "Error" { $global:logTextBox.SelectionColor = $dangerColor }
107 + default { $global:logTextBox.SelectionColor = $textPrimary }
108 + }
109 +
110 + $global:logTextBox.AppendText("$logMessage`r`n")
111 + $global:logTextBox.SelectionColor = $global:logTextBox.ForeColor
112 +
113 + # 保持日志文件大小可控
114 + if ($global:logTextBox.Lines.Count -gt 1000) {
115 + $lines = $global:logTextBox.Lines
116 + $startIndex = [math]::Max(0, $lines.Count - 500)
117 + $global:logTextBox.Text = [string]::Join("`r`n", $lines[$startIndex..($lines.Count-1)])
118 + }
119 +
120 + $global:logTextBox.ScrollToCaret()
121 + })
122 + }
123 + catch {
124 + # 如果文本框写入失败,静默处理(不在控制台输出)
125 + }
126 + }
127 + }
128 +
129 + # =============================================
130 + # 进度显示函数
131 + # =============================================
132 + function Update-Progress {
133 + param (
134 + [int]$Value = -1,
135 + [string]$Text = "",
136 + [bool]$Marquee = $false
137 + )
138 +
139 + # 只在文本变化时更新日志
140 + if ($Text -ne $global:lastProgressText -and $Text -ne "") {
141 + $global:lastProgressText = $Text
142 + Write-Log $Text -Type "Info"
143 + }
144 +
145 + # 如果需要,更新进度条
146 + if ($global:progressBar -ne $null -and $global:progressBar.IsHandleCreated) {
147 + $global:progressBar.Invoke([Action]{
148 + if ($Marquee) {
149 + $global:progressBar.Style = 'Marquee'
150 + } else {
151 + $global:progressBar.Style = 'Continuous'
152 + if ($Value -ge 0 -and $Value -le 100) {
153 + $global:progressBar.Value = $Value
154 + }
155 + }
156 + })
157 + }
158 + }
159 +
160 + function Hide-Progress {
161 + $global:lastProgressText = ""
162 + }
163 +
164 + # =============================================
165 + # 高速下载函数
166 + # =============================================
167 + function Get-FileFromWeb {
168 + param (
169 + [Parameter(Mandatory)][string]$URL,
170 + [Parameter(Mandatory)][string]$File,
171 + [string]$DisplayName = "",
172 + [int]$MaxRetries = 3,
173 + [int]$BufferSize = 2097152
174 + )
175 +
176 + if ($DisplayName -eq "") {
177 + $DisplayName = [System.IO.Path]::GetFileName($URL)
178 + }
179 +
180 + # 优化网络设置
181 + [System.Net.ServicePointManager]::DefaultConnectionLimit = 100
182 + [System.Net.ServicePointManager]::Expect100Continue = $false
183 + [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls
184 +
185 + $retryCount = 0
186 + $downloadSuccess = $false
187 +
188 + while ($retryCount -lt $MaxRetries -and -not $downloadSuccess) {
189 + $retryCount++
190 +
191 + if ($retryCount -gt 1) {
192 + Write-Log "第 $retryCount 次重试下载: $DisplayName" -Type "Warning"
193 + Start-Sleep -Seconds 2
194 + }
195 +
196 + try {
197 + # 创建目录
198 + $dir = [System.IO.Path]::GetDirectoryName($File)
199 + if (!(Test-Path $dir)) {
200 + New-Item -ItemType Directory -Path $dir -Force | Out-Null
201 + }
202 +
203 + Write-Log "开始下载: $DisplayName (尝试 $retryCount/$MaxRetries)" -Type "Info"
204 +
205 + # 创建请求
206 + $request = [System.Net.HttpWebRequest]::Create($URL)
207 + $request.Timeout = 30000
208 + $request.ReadWriteTimeout = 300000
209 +
210 + # 支持断点续传
211 + $startPosition = 0
212 + if (Test-Path $File) {
213 + $existingFile = Get-Item $File -ErrorAction SilentlyContinue
214 + if ($existingFile) {
215 + $startPosition = $existingFile.Length
216 + $request.AddRange($startPosition)
217 + Write-Log "检测到已下载部分文件: $([math]::Round($startPosition / 1MB, 2)) MB" -Type "Info"
218 + }
219 + }
220 +
221 + Write-Log "正在连接到服务器..." -Type "Info"
222 +
223 + $response = $request.GetResponse()
224 +
225 + if ($response.StatusCode -eq 401 -or $response.StatusCode -eq 403 -or $response.StatusCode -eq 404) {
226 + throw "远程文件不存在、未授权或禁止访问:'$URL'。"
227 + }
228 +
229 + # 获取文件总大小
230 + [long]$fullSize = $response.ContentLength
231 + if ($startPosition -gt 0) {
232 + $fullSize += $startPosition
233 + }
234 +
235 + Write-Log "开始下载: $DisplayName (大小: $([math]::Round($fullSize / 1MB, 2)) MB)" -Type "Info"
236 +
237 + # 创建缓冲区
238 + [byte[]]$buffer = New-Object byte[] $BufferSize
239 + [long]$total = $startPosition
240 + [long]$count = 0
241 +
242 + $startTime = Get-Date
243 + $reader = $response.GetResponseStream()
244 +
245 + # 以追加模式打开文件
246 + $fileMode = if ($startPosition -gt 0) { 'Append' } else { 'Create' }
247 + $writer = New-Object System.IO.FileStream $File, $fileMode
248 +
249 + # 优化:减少UI更新频率
250 + $lastUpdateTime = Get-Date
251 + $lastUpdateSize = $total
252 + $updateIntervalMB = 10
253 +
254 + do {
255 + $count = $reader.Read($buffer, 0, $buffer.Length)
256 + $writer.Write($buffer, 0, $count)
257 + $total += $count
258 +
259 + # 计算是否需要更新UI
260 + $currentTime = Get-Date
261 + $downloadedSinceLastUpdate = $total - $lastUpdateSize
262 + $timeSinceLastUpdate = ($currentTime - $lastUpdateTime).TotalSeconds
263 +
264 + if ($downloadedSinceLastUpdate -ge (10 * 1024 * 1024) -or $timeSinceLastUpdate -ge 3) {
265 + if ($fullSize -gt 0) {
266 + $percent = $total / $fullSize
267 + $percentComplete = $percent * 100
268 +
269 + $downloadedMB = [math]::Round($total / 1MB, 2)
270 + $totalMB = [math]::Round($fullSize / 1MB, 2)
271 +
272 + # 计算瞬时速度
273 + $speedMBps = 0
274 + if ($timeSinceLastUpdate -gt 0) {
275 + $speedMBps = [math]::Round($downloadedSinceLastUpdate / 1MB / $timeSinceLastUpdate, 2)
276 + }
277 +
278 + # 计算剩余时间
279 + $remainingTime = ""
280 + if ($speedMBps -gt 0 -and $percentComplete -lt 100) {
281 + $remainingMB = ($fullSize - $total) / 1MB
282 + $remainingSeconds = [math]::Round($remainingMB / $speedMBps)
283 + if ($remainingSeconds -gt 0) {
284 + $remainingTime = " | 剩余: " + (New-TimeSpan -Seconds $remainingSeconds).ToString("hh\:mm\:ss")
285 + }
286 + }
287 +
288 + $progressText = "$DisplayName - $($percentComplete.ToString('##0.00').PadLeft(6))% ($downloadedMB MB / $totalMB MB) | 速度: $speedMBps MB/s$remainingTime"
289 + Update-Progress -Value $percentComplete -Text $progressText
290 +
291 + $lastUpdateTime = $currentTime
292 + $lastUpdateSize = $total
293 + }
294 + }
295 +
296 + } while ($count -gt 0)
297 +
298 + $writer.Close()
299 + $reader.Close()
300 + $response.Close()
301 +
302 + $endTime = Get-Date
303 + $downloadTime = ($endTime - $startTime).TotalSeconds
304 +
305 + if ($downloadTime -gt 0) {
306 + $downloadSpeed = [math]::Round(($fullSize / $downloadTime) / 1MB, 2)
307 + Write-Log "✓ 下载完成: $DisplayName" -Type "Success"
308 + Write-Log " 下载时间: $downloadTime 秒 | 平均速度: $downloadSpeed MB/s | 文件大小: $([math]::Round($fullSize / 1MB, 2)) MB" -Type "Info"
309 + } else {
310 + Write-Log "✓ 下载完成: $DisplayName" -Type "Success"
311 + }
312 +
313 + # 验证文件完整性
314 + if (Test-Path $File) {
315 + $downloadedFile = Get-Item $File
316 + if ($downloadedFile.Length -eq $fullSize) {
317 + Write-Log "✓ 文件完整性验证通过" -Type "Success"
318 + $downloadSuccess = $true
319 + } else {
320 + Write-Log "! 文件大小不匹配,可能下载不完整" -Type "Warning"
321 + Write-Log " 期望: $fullSize 字节, 实际: $($downloadedFile.Length) 字节" -Type "Info"
322 +
323 + if ($retryCount -lt $MaxRetries) {
324 + Remove-Item $File -Force -ErrorAction SilentlyContinue
325 + continue
326 + } else {
327 + throw "下载文件不完整,已尝试 $MaxRetries 次"
328 + }
329 + }
330 + }
331 +
332 + Hide-Progress
333 + return $true
334 + }
335 + catch {
336 + Write-Log "✗ 下载失败 (尝试 $retryCount/$MaxRetries): $($_.Exception.Message)" -Type "Error"
337 +
338 + # 清理资源
339 + if ($writer -ne $null) { try { $writer.Close() } catch {} }
340 + if ($reader -ne $null) { try { $reader.Close() } catch {} }
341 + if ($response -ne $null) { try { $response.Close() } catch {} }
342 +
343 + if ($retryCount -eq $MaxRetries) {
344 + Write-Log "✗ 下载失败,已尝试 $MaxRetries 次" -Type "Error"
345 + Hide-Progress
346 + return $false
347 + }
348 + }
349 + }
350 +
351 + Hide-Progress
352 + return $false
353 + }
354 +
355 + # =============================================
356 + # 通用功能函数
357 + # =============================================
358 +
359 + function Show-Info {
360 + param (
361 + [string]$Version = "2023" # 2023 或 2019
362 + )
363 +
364 + if ($Version -eq "2023") {
365 + $info = @"
366 + WPS Office 2023 专业增强版 - V12.8.2.21555
367 +
368 + 版本说明:
369 + WPS2023专业增强版:免激活、去水印、永久授权、完整功能优化增强版
370 + 1. 基于官方WPS2023专业版打包,自动调用安装脚本写入授权序列号
371 + 2. 集成VBA组件、终身授权序列号、安装完毕即WPS激活专业增强版
372 + 3. 去广告优化、去我的电脑WPS云盘、保留账户登陆和云同步上传下载
373 + ﹂去界面左侧:日历、WPS便签、会议、统计表单 (广告)
374 + ﹂去应用中心:分享协作功能网页入口(会议、统计表单)
375 + ﹂彻底去升级:无版本更新提示,检查更新永远都是最新版
376 + 4. 安装过程自动剔除桌面和我的电脑WPS云盘虚拟盘符入口
377 + 5. 安装过程自动禁用WPS办公助手和其资源管理器右键菜单
378 + 6. 安装过程自动删除升级组件并移除检查升级的计划任务项
379 + 7. 安装后删除升级文件,彻底禁用更新
380 + "@
381 + Write-Log "========== WPS Office 2023 版本信息 ==========" -Type "Info"
382 + } else {
383 + $info = @"
384 + WPS Office 2019 专业增强版 - V11.8.2.12300
385 +
386 + 版本说明:
387 + WPS2019专业增强版:免激活、去水印、永久授权、完整功能优化增强版
388 + 1. 基于官方WPS2019专业版打包,自动调用安装脚本写入授权序列号
389 + 2. 集成VBA组件、授权序列号、安装完毕即WPS激活专业增强版
390 + 3. 去广告优化、去我的电脑WPS云盘、保留账户登陆和云同步上传下载
391 + ﹂去界面左侧:日历、WPS便签、会议、统计表单 (广告)
392 + ﹂去应用中心:分享协作功能网页入口(会议、统计表单)
393 + ﹂彻底去升级:无版本更新提示,检查更新永远都是最新版
394 + 4. 安装过程自动剔除桌面和我的电脑WPS云盘虚拟盘符入口
395 + 5. 安装过程自动删除升级组件并移除检查升级的计划任务项
396 + 6. 安装后删除升级文件,彻底禁用更新
397 + "@
398 + Write-Log "========== WPS Office 2019 版本信息 ==========" -Type "Info"
399 + }
400 +
401 + $infoLines = $info -split "`n"
402 + foreach ($line in $infoLines) {
403 + Write-Log $line -Type "Info"
404 + }
405 + }
406 +
407 + function Check-WPSInstalled {
408 + param(
409 + [switch]$Detailed = $false # 是否输出详细信息
410 + )
411 +
412 + Write-Log "检查系统中是否已安装WPS Office..." -Type "Info"
413 +
414 + $found = $false
415 + $installInfo = @{
416 + IsInstalled = $false
417 + Path = ""
418 + Version = ""
419 + DisplayName = ""
420 + RegistryEntries = @()
421 + RegistryCount = 0
422 + }
423 +
424 + # 检查安装目录
425 + $paths = @(
426 + "${env:ProgramFiles}\WPS Office",
427 + "${env:ProgramFiles(x86)}\WPS Office",
428 + "${env:ProgramFiles}\Kingsoft\WPS Office",
429 + "${env:ProgramFiles(x86)}\Kingsoft\WPS Office"
430 + )
431 +
432 + foreach ($path in $paths) {
433 + if (Test-Path $path) {
434 + $found = $true
435 + $installInfo.IsInstalled = $true
436 + $installInfo.Path = $path
437 +
438 + $versionFile = Join-Path $path "office6\cfgs\oem.ini"
439 + if (Test-Path $versionFile) {
440 + $versionInfo = Get-Content $versionFile -Encoding Default -ErrorAction SilentlyContinue |
441 + Where-Object { $_ -match "ProductVersion" }
442 + if ($versionInfo) {
443 + $version = $versionInfo.Split('=')[1].Trim()
444 + $installInfo.Version = $version
445 + break
446 + }
447 + }
448 + break
449 + }
450 + }
451 +
452 + # 搜索所有可能的WPS相关注册表项
453 + $wpsKeys = @()
454 + $regPaths = @(
455 + "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
456 + "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
457 + "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
458 + )
459 +
460 + foreach ($path in $regPaths) {
461 + if (Test-Path $path) {
462 + try {
463 + $keys = Get-ChildItem $path -ErrorAction SilentlyContinue |
464 + ForEach-Object {
465 + try {
466 + Get-ItemProperty $_.PSPath -ErrorAction SilentlyContinue
467 + } catch {
468 + # 跳过无法访问的注册表项
469 + }
470 + } |
471 + Where-Object {
472 + $_.DisplayName -like "*WPS*" -or
473 + $_.DisplayName -like "*Kingsoft*" -or
474 + $_.DisplayName -like "*金山*"
475 + }
476 +
477 + if ($keys) {
478 + $wpsKeys += $keys
479 + $found = $true
480 + $installInfo.IsInstalled = $true
481 + }
482 + }
483 + catch {
484 + # 静默处理注册表访问错误
485 + }
486 + }
487 + }
488 +
489 + # 整理注册表信息
490 + if ($wpsKeys.Count -gt 0) {
491 + $installInfo.RegistryEntries = $wpsKeys | ForEach-Object {
492 + @{
493 + DisplayName = $_.DisplayName
494 + DisplayVersion = $_.DisplayVersion
495 + InstallLocation = $_.InstallLocation
496 + Publisher = $_.Publisher
497 + UninstallString = $_.UninstallString
498 + }
499 + }
500 + $installInfo.RegistryCount = $wpsKeys.Count
501 +
502 + # 如果没有找到目录信息,使用第一个注册表项的安装位置
503 + if ($installInfo.Path -eq "" -and $wpsKeys[0].InstallLocation) {
504 + $installInfo.Path = $wpsKeys[0].InstallLocation
505 + }
506 + if ($installInfo.Version -eq "" -and $wpsKeys[0].DisplayVersion) {
507 + $installInfo.Version = $wpsKeys[0].DisplayVersion
508 + }
509 + if ($installInfo.DisplayName -eq "" -and $wpsKeys[0].DisplayName) {
510 + $installInfo.DisplayName = $wpsKeys[0].DisplayName
511 + }
512 + }
513 +
514 + if ($found) {
515 + if (-not $Detailed) {
516 + # 简洁模式:只在安装流程中使用
517 + if ($installInfo.Path -ne "") {
518 + Write-Log "✓ 发现已安装的WPS Office" -Type "Warning"
519 + Write-Log " 安装目录: $($installInfo.Path)" -Type "Info"
520 + } else {
521 + Write-Log "✓ 发现已安装的WPS Office (位置未知)" -Type "Warning"
522 + }
523 + } else {
524 + # 详细模式:简化输出,只显示核心信息
525 + Write-Log "=== WPS Office 详细安装信息 ===" -Type "Info"
526 +
527 + if ($installInfo.Path -ne "") {
528 + Write-Log "安装目录: $($installInfo.Path)" -Type "Info"
529 + }
530 +
531 + if ($installInfo.DisplayName -ne "") {
532 + Write-Log "名称: $($installInfo.DisplayName)" -Type "Info"
533 + }
534 +
535 + if ($installInfo.Version -ne "") {
536 + Write-Log "版本: $($installInfo.Version)" -Type "Info"
537 + }
538 +
539 + # 显示注册表项数量,但不再显示每个项的详细内容
540 + if ($installInfo.RegistryCount -gt 0) {
541 + Write-Log "找到的注册表项 ($($installInfo.RegistryCount)个):" -Type "Info"
542 + }
543 +
544 + Write-Log "如需重新安装,请先通过控制面板卸载" -Type "Warning"
545 + }
546 +
547 + return $installInfo
548 + } else {
549 + Write-Log "未检测到已安装的WPS Office" -Type "Success"
550 + return @{ IsInstalled = $false }
551 + }
552 + }
553 +
554 + function Clean-LicenseFile {
555 + Write-Log "清理许可证文件..." -Type "Info"
556 +
557 + $licensePath = "C:\ProgramData\Kingsoft\office6\license2.dat"
558 +
559 + if (Test-Path $licensePath) {
560 + try {
561 + # 移除只读、隐藏属性(已注释,仅保留备注)
562 + # $file = Get-Item $licensePath -Force
563 + # $file.Attributes = $file.Attributes -band (-bnot [System.IO.FileAttributes]::ReadOnly)
564 + # $file.Attributes = $file.Attributes -band (-bnot [System.IO.FileAttributes]::Hidden)
565 +
566 + # 删除文件
567 + Remove-Item $licensePath -Force -ErrorAction Stop
568 + Write-Log "✓ 已删除旧的许可证文件" -Type "Success"
569 + }
570 + catch {
571 + Write-Log "! 删除许可证文件失败: $($_.Exception.Message)" -Type "Warning"
572 + }
573 + } else {
574 + Write-Log "未找到许可证文件,无需清理" -Type "Info"
575 + }
576 + }
577 +
578 + function Generate-ConfigFile {
579 + param (
580 + [string]$ConfigPath,
581 + [string]$Version = "2023" # 2023 或 2019
582 + )
583 +
584 + if ($Version -eq "2023") {
585 + $configContent = @'
586 + [support]
587 + OCRTool=true
588 + XiuTang=false
589 + WeiboPlugin=true
590 + EnablePdf2WordV2=true
591 + EnableProcessonMind=true
592 + EnableProcessonFlow=true
593 + IsSupportPhoto2pdf=true
594 + EnableProcessonFlow/Mind=true
595 + WPSPlusVersion=true
596 + NseVisible=true
597 + Update=false
598 + IntranetDrive=false
599 + EnterpriseDocpermission=true
600 + EnablePlainWatermarkInfo=true
601 + Prometheus=false
602 + DisableWPSPdfDeskTopShortcut=true
603 + OnlineWithoutCloudDoc=false
604 + WeChatCustomerService=false
605 + EnableWPSOfficialWebsite=false
606 + UpdateSvrCustomAddress=http://0.0.0.0/wpsoffice/updateserver/update
607 +
608 + [Setup]
609 + SendInstallSource=1
610 + Silent=1
611 + Sn=TJ3GN-9NTGQ-GLF7C-YEN8X-TJWML
612 + SourceDir=oeminfo
613 + UpdateSvrDefaultAddress=http://127.0.0.1
614 +
615 + [Auth]
616 + LicenseServerUrl=http://wps.mycustom.server/account
617 + KSNUpdateServerUrl=http://0.0.0.0
618 +
619 + [Server]
620 + UpdateServer=http://127.0.0.1
621 + InfoCollectServer=http://0.0.0.0/wpsv6internet/infos.ads
622 + IntranetPluginsICServer=http://0.0.0.0/xva/v1/plugin/stat
623 + CustomUpdateServer=http://0.0.0.0/wpsoffice/updateserver/update
624 + '@
625 + } else {
626 + # 2019版本配置
627 + $configContent = @'
628 + [support]
629 + OCRTool=true
630 + XiuTang=false
631 + WeiboPlugin=true
632 + EnablePdf2WordV2=true
633 + EnableProcessonMind=true
634 + EnableProcessonFlow=true
635 + IsSupportPhoto2pdf=true
636 + WPSPlusVersion=true ;显示增强版
637 + NseVisible=true ;安装VB组件工具
638 + Update=false ;禁止版本检测升级
639 + IntranetDrive=true ;不显示我的电脑WPS云盘
640 + EnterpriseDocpermission=true ;启用企业版文档权限
641 + EnablePlainWatermarkInfo=true ;输出打印无水印信息
642 + Prometheus=false ;禁用整合模式(打开应用需要手动新建文件)
643 + IsCreateNewFile=1 ; 整合模式实现(打开应用即进入编辑模式)
644 + Support2016SN=true ;启用序列号(11.8.2.12287 版开始已失效)
645 + DisableWPSPdfDeskTopShortcut=true ;不创建桌面PDF快捷方式
646 + FileDialogDefWpsCloud=false ;保存对话框可自定义WPS云文档
647 + OnlineWithoutCloudDoc=false ;不显示广告 (日历, WPS便签, 会议, 统计表单)
648 + WeChatCustomerService=false ;不显示工具栏WPS客服微信按钮
649 + UpdateSvrCustomAddress=http://0.0.0.0/wpsoffice/updateserver/update
650 + caSM7oYrI4vjjbzpO04RdA..=NsbhfV4nLv_oZGENyLSVZA..
651 + VaRJdz9TqngvP2rCk3bf-dH_HWpeBrG_9hpfdL1oM7Y.=NsbhfV4nLv_oZGENyLSVZA.. ;不显示工具栏WPS客服微信按钮 (最新版)
652 + XAoSmN4Dk8PJG50puLaNe-iIrz2EFPJoaiSwWCbv1_A.=NsbhfV4nLv_oZGENyLSVZA.. ;不显示广告 (日历, WPS便签, 会议, 统计表单)(最新版)
653 + uHggomDKEaVGML_Zzoprjw..=NsbhfV4nLv_oZGENyLSVZA..
654 + PZ8bJ6Ee7I1QgyE_ecJvbiWJqbfBIQrcAzfOWLgeT1c.=WHfH10HHgeQrW2N48LfXrA..
655 + jko0IwcTvUkzCbPvis6rUIxF_x6CEcMTNTiM8kp_W9A.=NsbhfV4nLv_oZGENyLSVZA..
656 +
657 + [Setup]
658 + SendInstallSource=1 ;检测安装包
659 + Silent=0 ;0带界面标准安装,1无界面静默安装
660 + Sn=TJ3GN-9NTGQ-GLF7C-YEN8X-TJWML ;终身授权序列号
661 + SourceDir=oeminfo ;源目录支持读取企业OEM配置文件安装
662 + UpdateSvrDefaultAddress=http://127.0.0.1 ;升级服务默认服务器
663 + Kr0cNR1nKpC7yGiK-_5E2w..=yrogMsuo-CgeSd6yrWZsOBiJUkFphK4lPxYTSyyRgqc.
664 +
665 + [Auth]
666 + ;自定义授权序列号验证和升级服务器
667 + LicenseServerUrl=http://127.0.0.1
668 + KSNUpdateServerUrl=http://0.0.0.0
669 +
670 + [Server]
671 + UpdateServer=http://127.0.0.1
672 + CustomUpdateServer=http://0.0.0.0
673 + AutoNomInfoSvr=http://0.0.0.0/wpscollect
674 + QingUrl=http://0.0.0.0
675 + '@
676 + }
677 +
678 + $configContent | Out-File -FilePath $ConfigPath -Encoding Default
679 + Write-Log "✓ $Version 版本配置文件已生成" -Type "Success"
680 + }
681 +
682 + # =============================================
683 + # 核心修复:安全的路径处理函数
684 + # =============================================
685 + function Safe-TrimPath {
686 + param ([string]$Path)
687 +
688 + # 如果路径为空或null,返回默认路径
689 + if ([string]::IsNullOrWhiteSpace($Path)) {
690 + return "${env:ProgramFiles(x86)}\Kingsoft\WPS Office"
691 + }
692 +
693 + # 确保是字符串
694 + $Path = [string]$Path
695 +
696 + # 安全地调用Trim
697 + try {
698 + return $Path.Trim()
699 + }
700 + catch {
701 + # 如果Trim失败,返回原字符串
702 + return $Path
703 + }
704 + }
705 +
706 + # =============================================
707 + # 改进的查找WPS目录(解决子目录查找问题)
708 + # =============================================
709 + function Find-WPSActualPath {
710 + param ([string]$BasePath)
711 +
712 + Write-Log "查找WPS实际安装目录..." -Type "Info"
713 +
714 + # 安全处理路径
715 + $BasePath = Safe-TrimPath -Path $BasePath
716 +
717 + # 1. 先检查直接路径
718 + $ksomiscPath = Join-Path $BasePath "office6\ksomisc.exe"
719 + if (Test-Path $ksomiscPath) {
720 + return $BasePath
721 + }
722 +
723 + # 2. 检查子目录(改进版本)
724 + if (Test-Path $BasePath) {
725 + try {
726 + # 获取所有子目录
727 + $subDirs = @()
728 + try {
729 + $subDirs = Get-ChildItem -Path $BasePath -Directory -ErrorAction Stop
730 + }
731 + catch {
732 + return $BasePath
733 + }
734 +
735 + # 检查每个子目录
736 + foreach ($subDir in $subDirs) {
737 + $subPath = $subDir.FullName
738 + $subKsomiscPath = Join-Path $subPath "office6\ksomisc.exe"
739 +
740 + if (Test-Path $subKsomiscPath) {
741 + return $subPath
742 + }
743 + }
744 +
745 + # 如果没找到,检查子目录的子目录(嵌套查找)
746 + foreach ($subDir in $subDirs) {
747 + $subPath = $subDir.FullName
748 + $nestedSubDirs = Get-ChildItem -Path $subPath -Directory -ErrorAction SilentlyContinue
749 +
750 + foreach ($nestedDir in $nestedSubDirs) {
751 + $nestedPath = $nestedDir.FullName
752 + $nestedKsomiscPath = Join-Path $nestedPath "office6\ksomisc.exe"
753 +
754 + if (Test-Path $nestedKsomiscPath) {
755 + return $nestedPath
756 + }
757 + }
758 + }
759 +
760 + # 查找任何包含office6的目录
761 + foreach ($subDir in $subDirs) {
762 + $subPath = $subDir.FullName
763 + $office6Path = Join-Path $subPath "office6"
764 +
765 + if (Test-Path $office6Path) {
766 + return $subPath
767 + }
768 + }
769 + }
770 + catch {
771 + Write-Log "! 检查子目录失败: $($_.Exception.Message)" -Type "Warning"
772 + }
773 + } else {
774 + Write-Log "! 基目录不存在: $BasePath" -Type "Warning"
775 + }
776 +
777 + Write-Log "! 未找到注册程序,使用基目录" -Type "Warning"
778 + return $BasePath
779 + }
780 +
781 + # =============================================
782 + # 完全修复的安装后处理(简化版)
783 + # =============================================
784 + function Post-InstallProcessing {
785 + param ([string]$InstallPath)
786 +
787 + Write-Log "开始安装后处理..." -Type "Info"
788 +
789 + $activated = $false # 记录激活状态
790 +
791 + try {
792 + # 1. 安全处理安装路径
793 + $safePath = Safe-TrimPath -Path $InstallPath
794 +
795 + # 2. 查找实际目录
796 + $actualPath = Find-WPSActualPath -BasePath $safePath
797 + $actualPath = Safe-TrimPath -Path $actualPath
798 +
799 + # 3. 执行序列号注册
800 + Write-Log "执行序列号注册..." -Type "Info"
801 + $ksomiscPath = Join-Path $actualPath "office6\ksomisc.exe"
802 +
803 + # 检查许可证文件是否已存在
804 + $licensePath = "C:\ProgramData\Kingsoft\office6\license2.dat"
805 + $licenseExists = Test-Path $licensePath
806 +
807 + if (Test-Path $ksomiscPath) {
808 + # 如果许可证文件已存在,删除旧文件
809 + if ($licenseExists) {
810 + try {
811 + Remove-Item -Path $licensePath -Force -ErrorAction SilentlyContinue
812 + }
813 + catch {}
814 + }
815 +
816 + try {
817 + # 执行命令
818 + Write-Log "执行注册命令" -Type "Info"
819 +
820 + $process = Start-Process -FilePath $ksomiscPath -ArgumentList "-addsn TJ3GN-9NTGQ-GLF7C-YEN8X-TJWML" -Wait -NoNewWindow -PassThru
821 +
822 + # 等待命令执行完成
823 + Start-Sleep -Seconds 3
824 +
825 + # 检查退出代码并验证许可证文件
826 + if (Test-Path $licensePath) {
827 + Write-Log "✓ 序列号注册成功 - 许可证文件已创建" -Type "Success"
828 +
829 + # 设置只读和隐藏属性(已注释,仅保留备注)
830 + # try {
831 + # $file = Get-Item $licensePath -Force
832 + # $file.Attributes = $file.Attributes -bor [System.IO.FileAttributes]::ReadOnly
833 + # $file.Attributes = $file.Attributes -bor [System.IO.FileAttributes]::Hidden
834 + # Write-Log "✓ 已设置许可证文件为只读和隐藏" -Type "Success"
835 + # }
836 + # catch {}
837 + $activated = $true
838 + } else {
839 + Write-Log "! 序列号注册失败 - 许可证文件未创建" -Type "Warning"
840 + }
841 + }
842 + catch {
843 + Write-Log "! 执行注册命令失败" -Type "Warning"
844 + }
845 + } else {
846 + Write-Log "! 未找到注册文件" -Type "Warning"
847 + }
848 +
849 + # 4. 简化更新禁用:直接删除wpsupdate.exe并创建空的只读文件
850 + Write-Log "禁用更新功能..." -Type "Info"
851 +
852 + # 可能的wpsupdate.exe路径
853 + $possiblePaths = @()
854 + $possiblePaths += Join-Path $actualPath "wtoolex\wpsupdate.exe"
855 + $possiblePaths += Join-Path $actualPath "wpsupdate.exe"
856 + $possiblePaths += Join-Path $actualPath "office6\wpsupdate.exe"
857 + $possiblePaths += Join-Path $actualPath "office6\wtoolex\wpsupdate.exe"
858 +
859 + $foundAndDisabled = $false
860 +
861 + foreach ($updatePath in $possiblePaths) {
862 + if (Test-Path $updatePath) {
863 + Write-Log "找到更新文件" -Type "Info"
864 +
865 + try {
866 + # 直接删除原始文件
867 + Remove-Item -Path $updatePath -Force -ErrorAction Stop
868 + Write-Log "✓ 已删除更新文件" -Type "Success"
869 +
870 + # 创建空的只读文件
871 + try {
872 + # 创建空的文件
873 + "" | Out-File -FilePath $updatePath -Encoding ASCII -Force
874 +
875 + # 设置文件属性为只读和隐藏
876 + $file = Get-Item $updatePath -Force
877 + $file.Attributes = $file.Attributes -bor [System.IO.FileAttributes]::ReadOnly
878 + $file.Attributes = $file.Attributes -bor [System.IO.FileAttributes]::Hidden
879 +
880 + Write-Log "✓ 已创建空的只读占位文件" -Type "Success"
881 + $foundAndDisabled = $true
882 + break
883 + }
884 + catch {
885 + Write-Log "! 创建占位文件失败" -Type "Warning"
886 + }
887 + }
888 + catch {
889 + Write-Log "! 处理更新文件失败" -Type "Warning"
890 + }
891 + }
892 + }
893 +
894 + if (-not $foundAndDisabled) {
895 + Write-Log "! 未找到更新文件" -Type "Warning"
896 + } else {
897 + Write-Log "✓ 更新功能已禁用" -Type "Success"
898 + }
899 +
900 + # 根据处理结果输出相应消息
901 + if ($activated) {
902 + Write-Log "✓ WPS Office 已成功激活" -Type "Success"
903 + Write-Log "序列号已写入" -Type "Success"
904 + Write-Log "提示: 请重启WPS Office以完成激活" -Type "Info"
905 + return $true
906 + } else {
907 + Write-Log "! WPS激活失败,请手动激活" -Type "Warning"
908 + return $false
909 + }
910 + }
911 + catch {
912 + Write-Log "✗ 安装后处理出错" -Type "Error"
913 + if ($activated) {
914 + Write-Log "✓ WPS Office 已成功激活" -Type "Success"
915 + } else {
916 + Write-Log "WPS已安装完成,但需要手动激活" -Type "Info"
917 + }
918 + return $activated
919 + }
920 + }
921 +
922 + # =============================================
923 + # 安装函数(优化日志输出)
924 + # =============================================
925 + function Install-WPS {
926 + param (
927 + [string]$Version = "2023" # 2023 或 2019
928 + )
929 +
930 + # 检查是否正在安装
931 + if ($global:isInstalling) {
932 + Write-Log "安装正在进行中,请稍候..." -Type "Warning"
933 + return $false
934 + }
935 +
936 + $global:isInstalling = $true
937 +
938 + # 更新按钮状态
939 + if ($global:installButton -ne $null) {
940 + $global:installButton.Invoke([Action]{
941 + $global:installButton.Enabled = $false
942 + $global:installButton.Text = "安装中..."
943 + $global:installButton.BackColor = $secondaryColor
944 + })
945 + }
946 + if ($global:install2019Button -ne $null) {
947 + $global:install2019Button.Invoke([Action]{
948 + $global:install2019Button.Enabled = $false
949 + $global:install2019Button.Text = "安装中..."
950 + $global:install2019Button.BackColor = $secondaryColor
951 + })
952 + }
953 + [System.Windows.Forms.Application]::DoEvents()
954 +
955 + try {
956 + Write-Log "========== 开始安装 WPS Office $Version ==========" -Type "Info"
957 +
958 + # 1. 检查是否已安装(使用简洁模式)
959 + $installInfo = Check-WPSInstalled
960 +
961 + if ($installInfo.IsInstalled) {
962 + Write-Log "✗ 系统已安装WPS Office,安装已取消" -Type "Error"
963 + Write-Log "如需重新安装,请先通过控制面板卸载现有版本" -Type "Info"
964 +
965 + $global:isInstalling = $false
966 + if ($global:installButton -ne $null) {
967 + $global:installButton.Invoke([Action]{
968 + $global:installButton.Enabled = $true
969 + $global:installButton.Text = "安装 WPS 2023"
970 + $global:installButton.BackColor = $primaryColor
971 + })
972 + }
973 + if ($global:install2019Button -ne $null) {
974 + $global:install2019Button.Invoke([Action]{
975 + $global:install2019Button.Enabled = $true
976 + $global:install2019Button.Text = "安装 WPS 2019"
977 + $global:install2019Button.BackColor = $secondaryColor
978 + })
979 + }
980 + [System.Windows.Forms.Application]::DoEvents()
981 + return $false
982 + }
983 +
984 + Write-Log "✓ 系统未安装WPS Office,可以继续安装" -Type "Success"
985 +
986 + # 2. 清理旧的许可证文件
987 + Clean-LicenseFile
988 +
989 + # 3. 创建临时目录
990 + Write-Log "创建临时目录..." -Type "Info"
991 + $tempDir = Join-Path $env:TEMP "WPS${Version}_Install_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
992 + if (!(Test-Path $tempDir)) {
993 + New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
994 + Write-Log "✓ 临时目录已创建" -Type "Success"
995 + }
996 +
997 + # 4. 下载WPS安装包
998 + Write-Log "下载WPS安装包..." -Type "Info"
999 +
1000 + # 根据版本选择下载URL
1001 + if ($Version -eq "2023") {
1002 + $wpsUrls = @(
1003 + "http://192.168.8.251/api/soft/Wps/Wps2023/WPS_Setup.exe",
1004 + "https://tool.3721611.com/api/soft/Wps/Wps2023/WPS_Setup.exe"
1005 + )
1006 + $displayName = "WPS Office 2023 安装包"
1007 + } else {
1008 + $wpsUrls = @(
1009 + "http://192.168.8.251/api/soft/Wps/Wps2019/WPS_Setup.exe",
1010 + "https://tool.3721611.com/api/soft/Wps/Wps2019/WPS_Setup.exe"
1011 + )
1012 + $displayName = "WPS Office 2019 安装包"
1013 + }
1014 +
1015 + $wpsPath = Join-Path $tempDir "WPS_Setup.exe"
1016 + $wpsDownloaded = $false
1017 +
1018 + # 尝试多个下载源
1019 + foreach ($wpsUrl in $wpsUrls) {
1020 + try {
1021 + $wpsDownloaded = Get-FileFromWeb -URL $wpsUrl -File $wpsPath -DisplayName $displayName
1022 + if ($wpsDownloaded) {
1023 + break
1024 + }
1025 + }
1026 + catch {
1027 + Write-Log "下载源失败" -Type "Warning"
1028 + }
1029 + }
1030 +
1031 + if (-not $wpsDownloaded) {
1032 + Write-Log "✗ WPS安装包下载失败,安装中止" -Type "Error"
1033 + $global:isInstalling = $false
1034 + if ($global:installButton -ne $null) {
1035 + $global:installButton.Invoke([Action]{
1036 + $global:installButton.Enabled = $true
1037 + $global:installButton.Text = "安装 WPS 2023"
1038 + $global:installButton.BackColor = $primaryColor
1039 + })
1040 + }
1041 + if ($global:install2019Button -ne $null) {
1042 + $global:install2019Button.Invoke([Action]{
1043 + $global:install2019Button.Enabled = $true
1044 + $global:install2019Button.Text = "安装 WPS 2019"
1045 + $global:install2019Button.BackColor = $secondaryColor
1046 + })
1047 + }
1048 + [System.Windows.Forms.Application]::DoEvents()
1049 + return $false
1050 + }
1051 +
1052 + # 5. 下载VBA组件(只下载,不安装,主安装包附件)
1053 + Write-Log "下载VBA组件(附件)..." -Type "Info"
1054 +
1055 + if ($Version -eq "2023") {
1056 + $vbaUrls = @(
1057 + "http://192.168.8.251/api/soft/Wps/Wps2023/VBA_Setup.exe",
1058 + "https://tool.3721611.com//api/soft/Wps/Wps2023/VBA_Setup.exe"
1059 + )
1060 + } else {
1061 + $vbaUrls = @(
1062 + "http://192.168.8.251/api/soft/Wps/Wps2019/VBA_Setup.exe",
1063 + "https://tool.3721611.com//api/soft/Wps/Wps2019/VBA_Setup.exe"
1064 + )
1065 + }
1066 +
1067 + $vbaPath = Join-Path $tempDir "VBA_Setup.exe"
1068 + $vbaDownloaded = $false
1069 +
1070 + foreach ($vbaUrl in $vbaUrls) {
1071 + try {
1072 + $vbaDownloaded = Get-FileFromWeb -URL $vbaUrl -File $vbaPath -DisplayName "VBA组件(附件)"
1073 + if ($vbaDownloaded) {
1074 + break
1075 + }
1076 + }
1077 + catch {
1078 + Write-Log "VBA下载源失败" -Type "Warning"
1079 + }
1080 + }
1081 +
1082 + if (-not $vbaDownloaded) {
1083 + Write-Log "! VBA组件下载失败" -Type "Warning"
1084 + } else {
1085 + Write-Log "✓ VBA组件下载成功(附件)" -Type "Success"
1086 + }
1087 +
1088 + # 6. 生成配置文件
1089 + Write-Log "生成配置文件..." -Type "Info"
1090 + $configPath = Join-Path $tempDir "oem.ini"
1091 + Generate-ConfigFile -ConfigPath $configPath -Version $Version
1092 +
1093 + # 7. 将配置文件与安装程序放在一起
1094 + Write-Log "准备安装文件..." -Type "Info"
1095 + $installDir = Split-Path $wpsPath -Parent
1096 + $destConfigPath = Join-Path $installDir "oem.ini"
1097 +
1098 + if ($configPath -ne $destConfigPath) {
1099 + Copy-Item -Path $configPath -Destination $destConfigPath -Force
1100 + Write-Log "✓ 配置文件已放置到安装目录" -Type "Success"
1101 + }
1102 +
1103 + # 8. 安装WPS(静默安装)
1104 + Write-Log "安装WPS Office $Version..." -Type "Info"
1105 +
1106 + if (-not (Test-Path $wpsPath)) {
1107 + Write-Log "✗ WPS安装文件不存在" -Type "Error"
1108 + $global:isInstalling = $false
1109 + if ($global:installButton -ne $null) {
1110 + $global:installButton.Invoke([Action]{
1111 + $global:installButton.Enabled = $true
1112 + $global:installButton.Text = "安装 WPS 2023"
1113 + $global:installButton.BackColor = $primaryColor
1114 + })
1115 + }
1116 + if ($global:install2019Button -ne $null) {
1117 + $global:install2019Button.Invoke([Action]{
1118 + $global:install2019Button.Enabled = $true
1119 + $global:install2019Button.Text = "安装 WPS 2019"
1120 + $global:install2019Button.BackColor = $secondaryColor
1121 + })
1122 + }
1123 + [System.Windows.Forms.Application]::DoEvents()
1124 + return $false
1125 + }
1126 +
1127 + # 使用静默安装
1128 + $installArgs = "/S"
1129 +
1130 + $process = Start-Process -FilePath $wpsPath -ArgumentList $installArgs -PassThru -WindowStyle Hidden
1131 +
1132 + if ($process) {
1133 + Write-Log "安装程序已启动" -Type "Info"
1134 +
1135 + # 等待安装完成
1136 + $startTime = Get-Date
1137 + $timeoutMinutes = 15
1138 + $lastUpdate = $startTime
1139 + $progress = 0
1140 +
1141 + while (-not $process.HasExited) {
1142 + $elapsed = (Get-Date) - $startTime
1143 + $minutes = [math]::Round($elapsed.TotalMinutes, 1)
1144 + $seconds = [math]::Round($elapsed.TotalSeconds)
1145 +
1146 + if ($minutes -gt $timeoutMinutes) {
1147 + Write-Log "! 安装超时,正在终止进程..." -Type "Warning"
1148 + try {
1149 + Stop-Process -Id $process.Id -Force -ErrorAction SilentlyContinue
1150 + } catch {}
1151 + break
1152 + }
1153 +
1154 + # 模拟进度更新
1155 + $progress = [math]::Min(90, $minutes * 20)
1156 + Update-Progress -Value $progress -Text "安装进度: 安装中... 已用时 $minutes 分钟"
1157 +
1158 + # 每10秒更新一次安装状态
1159 + $currentTime = Get-Date
1160 + if (($currentTime - $lastUpdate).TotalSeconds -ge 10) {
1161 + $lastUpdate = $currentTime
1162 + }
1163 +
1164 + [System.Windows.Forms.Application]::DoEvents()
1165 + Start-Sleep -Milliseconds 500
1166 + }
1167 +
1168 + Update-Progress -Value 100 -Text "安装进度: 安装完成"
1169 +
1170 + # 检查退出代码
1171 + if ($process.HasExited) {
1172 + if ($process.ExitCode -eq 0) {
1173 + Write-Log "✓ WPS Office $Version 安装成功" -Type "Success"
1174 + } else {
1175 + Write-Log "✗ WPS安装失败 (退出代码: $($process.ExitCode))" -Type "Error"
1176 + $global:isInstalling = $false
1177 + if ($global:installButton -ne $null) {
1178 + $global:installButton.Invoke([Action]{
1179 + $global:installButton.Enabled = $true
1180 + $global:installButton.Text = "安装 WPS 2023"
1181 + $global:installButton.BackColor = $primaryColor
1182 + })
1183 + }
1184 + if ($global:install2019Button -ne $null) {
1185 + $global:install2019Button.Invoke([Action]{
1186 + $global:install2019Button.Enabled = $true
1187 + $global:install2019Button.Text = "安装 WPS 2019"
1188 + $global:install2019Button.BackColor = $secondaryColor
1189 + })
1190 + }
1191 + [System.Windows.Forms.Application]::DoEvents()
1192 + return $false
1193 + }
1194 + }
1195 + } else {
1196 + Write-Log "✗ 无法启动安装程序" -Type "Error"
1197 + $global:isInstalling = $false
1198 + if ($global:installButton -ne $null) {
1199 + $global:installButton.Invoke([Action]{
1200 + $global:installButton.Enabled = $true
1201 + $global:installButton.Text = "安装 WPS 2023"
1202 + $global:installButton.BackColor = $primaryColor
1203 + })
1204 + }
1205 + if ($global:install2019Button -ne $null) {
1206 + $global:install2019Button.Invoke([Action]{
1207 + $global:install2019Button.Enabled = $true
1208 + $global:install2019Button.Text = "安装 WPS 2019"
1209 + $global:install2019Button.BackColor = $secondaryColor
1210 + })
1211 + }
1212 + [System.Windows.Forms.Application]::DoEvents()
1213 + return $false
1214 + }
1215 +
1216 + # 9. 等待安装完成并验证
1217 + Write-Log "验证安装结果..." -Type "Info"
1218 + Start-Sleep -Seconds 5
1219 +
1220 + $installSuccess = $false
1221 + $actualInstallPath = ""
1222 +
1223 + # 查找实际安装路径
1224 + $defaultPaths = @(
1225 + "${env:ProgramFiles(x86)}\Kingsoft\WPS Office",
1226 + "${env:ProgramFiles}\Kingsoft\WPS Office",
1227 + "${env:ProgramFiles(x86)}\WPS Office",
1228 + "${env:ProgramFiles}\WPS Office"
1229 + )
1230 +
1231 + foreach ($path in $defaultPaths) {
1232 + if (Test-Path $path) {
1233 + $actualInstallPath = $path
1234 + break
1235 + }
1236 + }
1237 +
1238 + if ($actualInstallPath -ne "" -and (Test-Path $actualInstallPath)) {
1239 + $installSuccess = $true
1240 + Write-Log "✓ WPS安装验证成功" -Type "Success"
1241 + Write-Log "安装目录: $actualInstallPath" -Type "Info"
1242 +
1243 + # 10. 执行安装后处理
1244 + Write-Log "执行安装后处理..." -Type "Info"
1245 + $postProcessResult = Post-InstallProcessing -InstallPath $actualInstallPath
1246 +
1247 + if ($postProcessResult) {
1248 + Write-Log "✓ 安装后处理完成" -Type "Success"
1249 + } else {
1250 + Write-Log "! 安装后处理部分失败" -Type "Warning"
1251 + }
1252 + } else {
1253 + Write-Log "✗ 无法确定WPS安装目录" -Type "Error"
1254 + $installSuccess = $false
1255 + }
1256 +
1257 + # 11. 清理临时文件
1258 + Write-Log "清理临时文件..." -Type "Info"
1259 + try {
1260 + if (Test-Path $tempDir) {
1261 + Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
1262 + Write-Log "✓ 临时文件已清理" -Type "Success"
1263 + }
1264 + }
1265 + catch {
1266 + Write-Log "! 清理临时文件失败" -Type "Warning"
1267 + }
1268 +
1269 + if ($installSuccess) {
1270 + Write-Log "========== WPS Office $Version 安装完成! ==========" -Type "Success"
1271 + Write-Log "序列号已注册成功" -Type "Success"
1272 + Write-Log "提示: 更新功能已禁用" -Type "Info"
1273 + Write-Log "VBA组件由WPS主程序自动安装" -Type "Info"
1274 + Write-Log "请重启WPS Office以完成激活" -Type "Info"
1275 + } else {
1276 + Write-Log "✗ WPS安装验证失败,请检查日志" -Type "Error"
1277 + }
1278 +
1279 + $global:isInstalling = $false
1280 + if ($global:installButton -ne $null) {
1281 + $global:installButton.Invoke([Action]{
1282 + $global:installButton.Enabled = $true
1283 + $global:installButton.Text = "安装 WPS 2023"
1284 + $global:installButton.BackColor = $primaryColor
1285 + })
1286 + }
1287 + if ($global:install2019Button -ne $null) {
1288 + $global:install2019Button.Invoke([Action]{
1289 + $global:install2019Button.Enabled = $true
1290 + $global:install2019Button.Text = "安装 WPS 2019"
1291 + $global:install2019Button.BackColor = $secondaryColor
1292 + })
1293 + }
1294 + [System.Windows.Forms.Application]::DoEvents()
1295 + Hide-Progress
1296 +
1297 + return $installSuccess
1298 + }
1299 + catch {
1300 + Write-Log "✗ 安装过程中发生错误" -Type "Error"
1301 + $global:isInstalling = $false
1302 + if ($global:installButton -ne $null) {
1303 + $global:installButton.Invoke([Action]{
1304 + $global:installButton.Enabled = $true
1305 + $global:installButton.Text = "安装 WPS 2023"
1306 + $global:installButton.BackColor = $primaryColor
1307 + })
1308 + }
1309 + if ($global:install2019Button -ne $null) {
1310 + $global:install2019Button.Invoke([Action]{
1311 + $global:install2019Button.Enabled = $true
1312 + $global:install2019Button.Text = "安装 WPS 2019"
1313 + $global:install2019Button.BackColor = $secondaryColor
1314 + })
1315 + }
1316 + [System.Windows.Forms.Application]::DoEvents()
1317 + Hide-Progress
1318 + return $false
1319 + }
1320 + }
1321 +
1322 + # =============================================
1323 + # 创建GUI界面
1324 + # =============================================
1325 +
1326 + function Create-MainForm {
1327 + # 创建主窗体
1328 + $form = New-Object System.Windows.Forms.Form
1329 + $form.Text = "WPS Office 2023/2019 专业增强版安装工具"
1330 + $form.Size = New-Object System.Drawing.Size(950, 717)
1331 + $form.StartPosition = "CenterScreen"
1332 + $form.BackColor = $lightColor
1333 + $form.FormBorderStyle = "FixedSingle"
1334 + $form.MaximizeBox = $false
1335 + $form.MinimizeBox = $true
1336 +
1337 + # 设置窗体图标
1338 + try {
1339 + $form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path)
1340 + } catch {}
1341 +
1342 + # 头部面板
1343 + $headerPanel = New-Object System.Windows.Forms.Panel
1344 + $headerPanel.Location = New-Object System.Drawing.Point(0, 0)
1345 + $headerPanel.Size = New-Object System.Drawing.Size(950, 100)
1346 + $headerPanel.BackColor = $primaryColor
1347 + $form.Controls.Add($headerPanel)
1348 +
1349 + # 标题标签
1350 + $titleLabel = New-Object System.Windows.Forms.Label
1351 + $titleLabel.Location = New-Object System.Drawing.Point(0, 20)
1352 + $titleLabel.Size = New-Object System.Drawing.Size(950, 35)
1353 + $titleLabel.Text = "WPS Office 2023/2019"
1354 + $titleLabel.Font = $fontTitle
1355 + $titleLabel.ForeColor = [System.Drawing.Color]::White
1356 + $titleLabel.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
1357 + $headerPanel.Controls.Add($titleLabel)
1358 +
1359 + # 版本标签
1360 + $versionLabel = New-Object System.Windows.Forms.Label
1361 + $versionLabel.Location = New-Object System.Drawing.Point(0, 60)
1362 + $versionLabel.Size = New-Object System.Drawing.Size(950, 20)
1363 + $versionLabel.Text = "专业增强版 | 集成序列号 | 集成VBA | 去除升级功能"
1364 + $versionLabel.Font = $fontSubtitle
1365 + $versionLabel.ForeColor = [System.Drawing.Color]::FromArgb(220, 230, 255)
1366 + $versionLabel.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
1367 + $headerPanel.Controls.Add($versionLabel)
1368 +
1369 + # 主内容面板
1370 + $mainPanel = New-Object System.Windows.Forms.Panel
1371 + $mainPanel.Location = New-Object System.Drawing.Point(0, 100)
1372 + $mainPanel.Size = New-Object System.Drawing.Size(950, 550)
1373 + $mainPanel.BackColor = $lightColor
1374 + $form.Controls.Add($mainPanel)
1375 +
1376 + # 左侧控制面板
1377 + $controlCard = New-Object System.Windows.Forms.GroupBox
1378 + $controlCard.Location = New-Object System.Drawing.Point(20, 20)
1379 + $controlCard.Size = New-Object System.Drawing.Size(300, 500)
1380 + $controlCard.Text = "安装控制"
1381 + $controlCard.Font = New-Object System.Drawing.Font($fontFamily, 10, [System.Drawing.FontStyle]::Bold)
1382 + $controlCard.ForeColor = $textPrimary
1383 + $controlCard.BackColor = [System.Drawing.Color]::White
1384 + $mainPanel.Controls.Add($controlCard)
1385 +
1386 + # 功能按钮容器
1387 + $buttonContainer = New-Object System.Windows.Forms.Panel
1388 + $buttonContainer.Location = New-Object System.Drawing.Point(25, 40)
1389 + $buttonContainer.Size = New-Object System.Drawing.Size(250, 400)
1390 + $controlCard.Controls.Add($buttonContainer)
1391 +
1392 + # 功能按钮样式函数
1393 + function Create-FeatureButton {
1394 + param(
1395 + [string]$Text,
1396 + [System.Drawing.Color]$Color,
1397 + [System.Drawing.Color]$HoverColor,
1398 + [int]$YPosition,
1399 + [scriptblock]$OnClick
1400 + )
1401 +
1402 + $button = New-Object System.Windows.Forms.Button
1403 + $button.Location = New-Object System.Drawing.Point(0, $YPosition)
1404 + $button.Size = New-Object System.Drawing.Size(250, 40)
1405 + $button.Text = $Text
1406 + $button.Font = $fontButtonRegular
1407 + $button.BackColor = $Color
1408 + $button.ForeColor = [System.Drawing.Color]::White
1409 + $button.FlatStyle = "Flat"
1410 + $button.FlatAppearance.BorderSize = 0
1411 + $button.FlatAppearance.MouseOverBackColor = $HoverColor
1412 + $button.FlatAppearance.MouseDownBackColor = $Color
1413 + $button.Cursor = [System.Windows.Forms.Cursors]::Hand
1414 + $button.Add_Click($OnClick)
1415 +
1416 + return $button
1417 + }
1418 +
1419 + # WPS 2023安装按钮
1420 + $installButton = Create-FeatureButton -Text "安装 WPS Office 2023" -Color $primaryColor -HoverColor $primaryHover -YPosition 0 -OnClick {
1421 + Install-WPS -Version "2023"
1422 + }
1423 + $buttonContainer.Controls.Add($installButton)
1424 + $global:installButton = $installButton
1425 +
1426 + # WPS 2019安装按钮
1427 + $install2019Button = Create-FeatureButton -Text "安装 WPS Office 2019" -Color $secondaryColor -HoverColor $secondaryHover -YPosition 50 -OnClick {
1428 + Install-WPS -Version "2019"
1429 + }
1430 + $buttonContainer.Controls.Add($install2019Button)
1431 + $global:install2019Button = $install2019Button
1432 +
1433 + # 查看2023信息按钮
1434 + $info2023Button = Create-FeatureButton -Text "查看2023版本信息" -Color $successColor -HoverColor $successHover -YPosition 100 -OnClick {
1435 + Show-Info -Version "2023"
1436 + }
1437 + $buttonContainer.Controls.Add($info2023Button)
1438 +
1439 + # 查看2019信息按钮
1440 + $info2019Button = Create-FeatureButton -Text "查看2019版本信息" -Color $infoColor -HoverColor $infoHover -YPosition 150 -OnClick {
1441 + Show-Info -Version "2019"
1442 + }
1443 + $buttonContainer.Controls.Add($info2019Button)
1444 +
1445 + # 检查安装按钮
1446 + $checkButton = Create-FeatureButton -Text "检查现有安装" -Color $warningColor -HoverColor $warningHover -YPosition 200 -OnClick {
1447 + # 直接调用详细模式,函数内部会输出完整信息
1448 + $installInfo = Check-WPSInstalled -Detailed
1449 + }
1450 + $checkButton.ForeColor = [System.Drawing.Color]::Black
1451 + $buttonContainer.Controls.Add($checkButton)
1452 +
1453 + # 清空日志按钮
1454 + $clearButton = Create-FeatureButton -Text "清空日志" -Color $infoColor -HoverColor $infoHover -YPosition 250 -OnClick {
1455 + if ($global:logTextBox -ne $null) {
1456 + $global:logTextBox.Invoke([Action]{
1457 + $global:logTextBox.Clear()
1458 + })
1459 + Write-Log "日志已清空" -Type "Info"
1460 + }
1461 + }
1462 + $buttonContainer.Controls.Add($clearButton)
1463 +
1464 + # 退出按钮
1465 + $exitButton = Create-FeatureButton -Text "退出程序" -Color $dangerColor -HoverColor $dangerHover -YPosition 300 -OnClick {
1466 + $form.Close()
1467 + }
1468 + $buttonContainer.Controls.Add($exitButton)
1469 +
1470 + # 右侧日志面板
1471 + $logCard = New-Object System.Windows.Forms.GroupBox
1472 + $logCard.Location = New-Object System.Drawing.Point(340, 20)
1473 + $logCard.Size = New-Object System.Drawing.Size(590, 500)
1474 + $logCard.Text = "安装日志"
1475 + $logCard.Font = New-Object System.Drawing.Font($fontFamily, 10, [System.Drawing.FontStyle]::Bold)
1476 + $logCard.ForeColor = $textPrimary
1477 + $logCard.BackColor = [System.Drawing.Color]::White
1478 + $mainPanel.Controls.Add($logCard)
1479 +
1480 + # 进度条
1481 + $progressBar = New-Object System.Windows.Forms.ProgressBar
1482 + $progressBar.Location = New-Object System.Drawing.Point(15, 30)
1483 + $progressBar.Size = New-Object System.Drawing.Size(560, 0)
1484 + $progressBar.Visible = $false
1485 + $progressBar.Minimum = 0
1486 + $progressBar.Maximum = 100
1487 + $progressBar.Style = 'Continuous'
1488 + $logCard.Controls.Add($progressBar)
1489 + $global:progressBar = $progressBar
1490 +
1491 + # 日志文本框
1492 + $richTextBox = New-Object System.Windows.Forms.RichTextBox
1493 + $richTextBox.Location = New-Object System.Drawing.Point(15, 35)
1494 + $richTextBox.Size = New-Object System.Drawing.Size(560, 445)
1495 + $richTextBox.BackColor = [System.Drawing.Color]::White
1496 + $richTextBox.ReadOnly = $true
1497 + $richTextBox.Font = $fontLog
1498 + $richTextBox.BorderStyle = 'FixedSingle'
1499 + $richTextBox.ScrollBars = 'Vertical'
1500 + $logCard.Controls.Add($richTextBox)
1501 + $global:logTextBox = $richTextBox
1502 +
1503 + # 底部状态栏
1504 + $statusPanel = New-Object System.Windows.Forms.Panel
1505 + $statusPanel.Location = New-Object System.Drawing.Point(0, 650)
1506 + $statusPanel.Size = New-Object System.Drawing.Size(950, 30)
1507 + $statusPanel.BackColor = $darkColor
1508 + $form.Controls.Add($statusPanel)
1509 +
1510 + # 状态标签
1511 + $statusLabel = New-Object System.Windows.Forms.Label
1512 + $statusLabel.Location = New-Object System.Drawing.Point(10, 0)
1513 + $statusLabel.Size = New-Object System.Drawing.Size(930, 30)
1514 + $statusLabel.Text = "就绪 | 本软件仅供一切非商业性质的个人研究学习使用,任何用于商业或盈利的行为均被严格禁止。"
1515 + $statusLabel.Font = New-Object System.Drawing.Font($fontFamily, 8.5)
1516 + $statusLabel.ForeColor = [System.Drawing.Color]::White
1517 + $statusLabel.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
1518 + $statusPanel.Controls.Add($statusLabel)
1519 +
1520 + # 版权标签
1521 + $copyrightLabel = New-Object System.Windows.Forms.Label
1522 + $copyrightLabel.Location = New-Object System.Drawing.Point(340, 525)
1523 + $copyrightLabel.Size = New-Object System.Drawing.Size(590, 20)
1524 + $copyrightLabel.Text = "© 2023-2024 WPS Office 专业增强版 "
1525 + $copyrightLabel.Font = New-Object System.Drawing.Font($fontFamily, 8)
1526 + $copyrightLabel.ForeColor = $textSecondary
1527 + $copyrightLabel.TextAlign = [System.Drawing.ContentAlignment]::MiddleRight
1528 + $mainPanel.Controls.Add($copyrightLabel)
1529 +
1530 + return $form
1531 + }
1532 +
1533 + # =============================================
1534 + # 主程序
1535 + # =============================================
1536 +
1537 + try {
1538 + # 创建并显示窗体
1539 + $global:form = Create-MainForm
1540 +
1541 + # 初始化日志
1542 + $initialLog = @"
1543 + === WPS Office 2023/2019 安装工具已启动 ===
1544 + 支持版本:
1545 + • 2023专业增强版 (V12.8.2.21555)
1546 + • 2019专业增强版 (V11.8.2.12300)
1547 + 系统信息: Microsoft Windows 11 专业版
1548 + 系统架构: 64位操作系统
1549 + 当前用户: $env:USERNAME
1550 + 系统时间: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
1551 +
1552 + 操作提示:
1553 + • 点击'安装 WPS 2023'按钮进行完整安装
1554 + • 点击'安装 WPS 2019'按钮安装2019版本
1555 + • 自动注册序列号
1556 + • 自动删除软件更新
1557 + 准备就绪,请开始操作...
1558 +
1559 + "@
1560 +
1561 + # 直接设置文本框内容
1562 + $global:logTextBox.Text = $initialLog
1563 +
1564 + # 确保末尾有换行符
1565 + if (-not $global:logTextBox.Text.EndsWith("`r`n")) {
1566 + $global:logTextBox.AppendText("`r`n")
1567 + }
1568 +
1569 + $global:logTextBox.SelectionStart = $global:logTextBox.TextLength
1570 + $global:logTextBox.ScrollToCaret()
1571 +
1572 + # 显示窗体
1573 + [void]$global:form.ShowDialog()
1574 + }
1575 + catch {
1576 + $errorMsg = $_.Exception.Message
1577 + [System.Windows.Forms.MessageBox]::Show("程序启动失败: $errorMsg", "错误", "OK", "Error")
1578 + }
Újabb Régebbi