Files
reverse-skill/skills/scripts/lib/HostRuntime.ps1
T
kin9-0rz 0bc4ec960d fix(scripts): 统一 PowerShell 子进程入口并修复 r2 工具索引 fallback
- 新增 lib/HostRuntime.ps1 的 Resolve-ReverseHostExe,子进程调用统一经当前进程优先解析(pwsh→powershell→系统兜底),替代 verify/case-init/test-routing/test-p0-friction 中硬编码的 & powershell / Start-Process powershell.exe,修复仅装 PowerShell 7+ 环境下子进程调用失败
- ToolDiscovery.ps1 的 r2 Fallbacks 补充 r2.bat 与 radare2.exe 路径,修复 radare2 Windows 发行版(无 r2.exe)下 tool-index 误报 no
- verify-routing-coherence.ps1 的 pin gate 改用 hashtable 索引访问 capabilities 可选属性,修复 Set-StrictMode -Latest 下访问不存在属性抛错(此前被 powershell 硬编码失败掩盖)
- 回写 field-journal 并同步 _index.md
2026-08-17 14:20:38 +08:00

59 lines
1.9 KiB
PowerShell

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# Resolves the PowerShell executable to use when a script must launch a child
# PowerShell process (e.g. to run another .ps1 in isolation with -NoProfile).
#
# Why not hard-code "powershell":
# - Modern Windows machines may install only PowerShell 7+ ("pwsh") and not
# ship Windows PowerShell 5.1 ("powershell.exe") on PATH.
# - Conversely, bare "powershell" resolves to 5.1, which mis-parses UTF-8
# scripts without BOM when nested from pwsh.
# Resolution order (highest confidence first):
# 1. Current process executable (the host that launched this script)
# 2. pwsh on PATH
# 3. powershell on PATH
# 4. %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe
# Returns the absolute path to the executable. Throws if nothing usable exists.
function Resolve-ReverseHostExe {
[CmdletBinding()]
[OutputType([string])]
param()
$hostExe = $null
try {
$procPath = (Get-Process -Id $PID -ErrorAction Stop).Path
if ($procPath -and (Test-Path -LiteralPath $procPath)) {
$hostExe = $procPath
}
} catch { }
if (-not $hostExe) {
$cmd = Get-Command pwsh -ErrorAction SilentlyContinue
if ($cmd -and $cmd.Source) {
$hostExe = $cmd.Source
}
}
if (-not $hostExe) {
$cmd = Get-Command powershell -ErrorAction SilentlyContinue
if ($cmd -and $cmd.Source) {
$hostExe = $cmd.Source
}
}
if (-not $hostExe -and $env:SystemRoot) {
$fallback = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe'
if (Test-Path -LiteralPath $fallback) {
$hostExe = $fallback
}
}
if (-not $hostExe) {
throw 'No usable PowerShell host executable found (tried current process, pwsh, powershell, Windows PowerShell fallback).'
}
return $hostExe
}