* 以管理员身份启动应用(仅 Windows)
(appPath: string, name?: string)
| 525 | * 以管理员身份启动应用(仅 Windows) |
| 526 | */ |
| 527 | private launchAsAdmin(appPath: string, name?: string): void { |
| 528 | if (process.platform !== 'win32') { |
| 529 | throw new Error('仅支持 Windows 平台') |
| 530 | } |
| 531 | |
| 532 | try { |
| 533 | const escapedPath = appPath.replace(/'/g, "''") |
| 534 | let psCommand: string |
| 535 | |
| 536 | if (appPath.toLowerCase().endsWith('.lnk')) { |
| 537 | // .lnk 快捷方式:先解析目标路径,再对目标可执行文件进行提权启动 |
| 538 | // 注意:$args 是 PowerShell 保留变量,必须用其他变量名 |
| 539 | psCommand = [ |
| 540 | `$lnk = (New-Object -ComObject WScript.Shell).CreateShortcut('${escapedPath}');`, |
| 541 | `$sp = @{ FilePath = $lnk.TargetPath; Verb = 'RunAs' };`, |
| 542 | `if ($lnk.Arguments) { $sp.ArgumentList = $lnk.Arguments };`, |
| 543 | `Start-Process @sp` |
| 544 | ].join(' ') |
| 545 | } else { |
| 546 | // 非 .lnk 文件:直接提权启动 |
| 547 | psCommand = `Start-Process -FilePath '${escapedPath}' -Verb RunAs` |
| 548 | } |
| 549 | |
| 550 | console.log(`[Commands] 以管理员身份启动: ${appPath}`) |
| 551 | console.log(`[Commands] PowerShell 命令: ${psCommand}`) |
| 552 | |
| 553 | execFile( |
| 554 | 'powershell.exe', |
| 555 | ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', psCommand], |
| 556 | (error, _stdout, stderr) => { |
| 557 | if (error) { |
| 558 | console.error('[Commands] 管理员启动失败:', error.message) |
| 559 | } |
| 560 | if (stderr) { |
| 561 | console.error('[Commands] 管理员启动 stderr:', stderr) |
| 562 | } |
| 563 | } |
| 564 | ) |
| 565 | |
| 566 | console.log(`[Commands] 以管理员身份启动: ${appPath}`) |
| 567 | |
| 568 | // 添加到历史记录 |
| 569 | this.addToHistory({ path: appPath, type: 'direct', name, cmdType: 'text' }) |
| 570 | |
| 571 | // 隐藏当前插件视图(如果有) |
| 572 | this.pluginManager?.hidePluginView() |
| 573 | // 通知渲染进程应用已启动 |
| 574 | this.notifyRenderer('app-launched') |
| 575 | this.mainWindow?.hide() |
| 576 | } catch (error) { |
| 577 | console.error('[Commands] 管理员启动失败:', error) |
| 578 | throw error |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * 添加到历史记录 |
no test coverage detected