如何以管理员权限运行 PowerShell 脚本但不以管理员权限启动子进程
数据小陈
结论:提权后的 PowerShell 不要直接用 Start-Process 启动游戏。提权进程创建的子进程默认会继承提权令牌,因此应让非提权父进程代启。最简单的办法是交给 explorer.exe;更稳妥的是用计划任务,并设置 -RunLevel Limited、-LogonType Interactive。若需要可靠地取得退出码,可让非提权包装脚本写结果文件,再由提权脚本读取。
原因
UAC 提权后,PowerShell 持有管理员令牌。Start-Process 默认从当前进程继承令牌,所以 skse64_loader.exe 也会以管理员身份运行。这里不应使用 -Credential:它需要密码,会创建新的登录会话,容易影响游戏 Overlay、音频、DRM、存档路径等。
方案一:用 Explorer 代启(改动最小)
Explorer 通常运行在普通用户令牌下,让它代为启动游戏即可。
$gameFile = 'C:\Program Files (x86)\GOG Galaxy\Games\Skyrim Anniversary Edition\skse64_loader.exe'
$processName = 'skse64_loader'
Start-Process -FilePath 'explorer.exe' -ArgumentList $gameFile
# 不要对 explorer.exe 用 -Wait;轮询等待目标进程出现
$deadline = (Get-Date).AddMinutes(2)
$gameProc = $null
do {
Start-Sleep -Milliseconds 500
$gameProc = Get-Process -Name $processName -ErrorAction SilentlyContinue |
Select-Object -First 1
} until ($gameProc -or (Get-Date) -gt $deadline)
if (-not $gameProc) {
throw "未能通过 Explorer 启动 $processName"
}
$gameProc.WaitForExit()
这种方式代码少,但等待进程和获取退出码都不如计划任务可靠,只适合关心能否启动的场景。
方案二:计划任务 + RunLevel Limited(推荐)
具体做法是注册一个只在当前用户登录会话中运行、权限级别为 Limited 的任务,然后触发它。游戏会以当前交互用户的非提权令牌运行。
$taskName = 'GTS_NonElevated_Launch'
$gameFile = 'C:\Program Files (x86)\GOG Galaxy\Games\Skyrim Anniversary Edition\skse64_loader.exe'
$workDir = Split-Path -Parent $gameFile
$userName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$action = New-ScheduledTaskAction -Execute $gameFile -WorkingDirectory $workDir
$principal = New-ScheduledTaskPrincipal -UserId $userName -LogonType Interactive -RunLevel Limited
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit ([TimeSpan]::Zero)
Register-ScheduledTask -TaskName $taskName -Action $action -Principal $principal -Settings $settings -Force | Out-Null
Start-ScheduledTask -TaskName $taskName
# 等待目标进程出现;如果 loader 很快退出,应改为等待实际游戏进程
$deadline = (Get-Date).AddMinutes(2)
$gameProc = $null
do {
Start-Sleep -Milliseconds 500
$gameProc = Get-Process -Name 'skse64_loader' -ErrorAction SilentlyContinue |
Select-Object -First 1
} until ($gameProc -or (Get-Date) -gt $deadline)
if (-not $gameProc) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
throw '计划任务已触发,但没有等到 skse64_loader 进程。'
}
$gameProc.WaitForExit()
# 任务结果可作参考;若需要严格退出码,使用下面的包装脚本方案
$info = Get-ScheduledTaskInfo -TaskName $taskName
$gameExitCode = $info.LastTaskResult
if ($gameExitCode -ne 0) {
Stop-Process -Name 'skse64_loader' -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
throw "GTS 启动失败,LastTaskResult=$gameExitCode"
}
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
需要可靠退出码时:非提权包装脚本
这时可以让计划任务运行一个非提权的 PowerShell 包装脚本;包装脚本用 Start-Process -Wait 启动游戏,并把退出码写入临时文件。提权脚本只等待这个结果文件。
包装脚本 Run-GTS-NonElevated.ps1:
param(
[Parameter(Mandatory=$true)][string]$GameFile,
[Parameter(Mandatory=$true)][string]$ResultFile
)
try {
$p = Start-Process -FilePath $GameFile -PassThru -Wait -ErrorAction Stop
$p.ExitCode | Out-File -FilePath $ResultFile -Encoding ascii
}
catch {
"ERR: $($_.Exception.Message)" | Out-File -FilePath $ResultFile -Encoding utf8
exit 1
}
提权脚本中触发并等待:
$taskName = 'GTS_NonElevated_Launch'
$gameFile = 'C:\Program Files (x86)\GOG Galaxy\Games\Skyrim Anniversary Edition\skse64_loader.exe'
$wrapper = 'C:\Scripts\Run-GTS-NonElevated.ps1'
$resultFile = Join-Path $env:TEMP "gts_exit_$([guid]::NewGuid().ToString('N')).txt"
$userName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$arg = "-NoProfile -ExecutionPolicy Bypass -File `"$wrapper`" -GameFile `"$gameFile`" -ResultFile `"$resultFile`""
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument $arg -WorkingDirectory (Split-Path $wrapper)
$principal = New-ScheduledTaskPrincipal -UserId $userName -LogonType Interactive -RunLevel Limited
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit ([TimeSpan]::Zero)
Register-ScheduledTask -TaskName $taskName -Action $action -Principal $principal -Settings $settings -Force | Out-Null
Start-ScheduledTask -TaskName $taskName
$deadline = (Get-Date).AddHours(2)
while (-not (Test-Path $resultFile) -and (Get-Date) -lt $deadline) {
Start-Sleep -Seconds 2
}
if (-not (Test-Path $resultFile)) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
throw '等待游戏退出超时。'
}
$raw = Get-Content -Path $resultFile -Raw
Remove-Item $resultFile -Force -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
if ($raw -match '^ERR:') {
Stop-Process -Name 'skse64_loader' -ErrorAction SilentlyContinue
throw "GTS 启动失败:$raw"
}
$gameExitCode = [int]$raw.Trim()
if ($gameExitCode -ne 0) {
Stop-Process -Name 'skse64_loader' -ErrorAction SilentlyContinue
throw "GTS 启动失败,退出码 $gameExitCode"
}
注意事项
- -RunLevel Limited 用来给计划任务降权,-LogonType Interactive 让任务在当前登录会话中运行。
- 不要用 Start-Process -Credential 作为降权手段,它需要密码且会创建新登录会话。
- 对 explorer.exe 使用 -Wait 不可靠,通常要轮询目标进程。
- 如果 skse64_loader 启动后很快退出、实际游戏进程是 SkyrimSE.exe,请把等待和包装逻辑改为等待实际游戏进程,否则后续等待可能等错对象。
- 计划任务名应唯一,异常路径要清理任务;生产环境建议加超时、日志和重复运行保护。
备注:内容仅供参考。
