fix(Elysium.Common): relaunch the calling script, not Elysium.Common.ps1

Invoke-RestartWithExecutable built its command line from
$PSCommandPath. Inside a function, that is the file the function is
defined in - Elysium.Common.ps1 - not the script that asked to be
relaunched. So every relaunch ran the helper file (which only defines
functions), got exit code 0, and the caller then exited. The actual
script never ran and nothing reported an error.

Affected whenever a relaunch fires: Update-KHDB, Prepare-KHDBStorage,
Update-LithnetStore and Uninstall started from Windows PowerShell on
a host with pwsh installed (menu options 1, 4 and 5 included), and
Extract-NTHashes / Test-WeakADPasswords started directly from pwsh.
Menu options 2 and 3 were unaffected because the menu already starts
those under powershell.exe, where no relaunch fires.

Restart-WithPwshIfAvailable and Restart-WithWindowsPowerShellIfAvailable
now pass $MyInvocation.PSCommandPath (the script that called them)
down as -ScriptPath.

Verified on pwsh 7 with a fake powershell.exe on PATH that prints its
arguments: the old helper asked it to run Elysium.Common.ps1, the
fixed one runs the calling script with its bound parameters
(-MaxParallelTransfers 8 -Force) forwarded. Not yet run on a real
Windows PowerShell 5.1 host.
This commit is contained in:
2026-11-04 11:41:44 +01:00
parent f69356da45
commit 63d55ef1be
2 changed files with 8 additions and 4 deletions
+1
View File
@@ -13,6 +13,7 @@ Starting with **v2.2.0**, Elysium uses a **unified project version**. All script
- `decrypt.py` could not decrypt any current export: it did not recognize `ELY2` (so v2.4.6 files fell through to the legacy branch), and its `ELY1` branch used pycryptodome's default PBKDF2 PRF (HMAC-SHA1) while `Extract-NTHashes.ps1` has always used HMAC-SHA256. It now reads `ELY2` (verifying the HMAC before decrypting), `ELY1`, and the legacy format; encodes the passphrase as UTF-8 to match .NET; prompts for the passphrase without echo; refuses input that does not end in `.enc` instead of overwriting it; and exits non-zero on failure. New `tests/test_decrypt.py` round-trips through the real PowerShell encryptor.
- `Test-WeakADPasswords.ps1` did not relaunch under Windows PowerShell like `Extract-NTHashes.ps1` does, so a direct run under `pwsh` loaded DSInternals through `-UseWindowsPowerShell` implicit remoting, which returns deserialized objects. It also set the PSGallery repository to `Trusted` on the host as a side effect of installing DSInternals; that is removed (`-Force` on the install already covers the prompt).
- `Update-LithnetStore.ps1` ignored `InstallationPath`/`WeakPasswordsDatabase` and always looked for `khdb.txt` next to the script; it now resolves the same KHDB path as the other scripts. It also writes a transcript to `Reports/logs/` like the other operational scripts and uses the shared settings parser instead of its own copy.
- The `Restart-WithPwshIfAvailable` / `Restart-WithWindowsPowerShellIfAvailable` relaunchers in `Elysium.Common.ps1` relaunched `Elysium.Common.ps1` itself instead of the calling script (`$PSCommandPath` inside a function is the file that defines it), then exited. Whenever a relaunch fired, the script silently did nothing: `Update-KHDB.ps1`, `Prepare-KHDBStorage.ps1`, `Update-LithnetStore.ps1` and `Uninstall.ps1` started from Windows PowerShell on a host with `pwsh` installed (including menu options 1, 4 and 5), and `Extract-NTHashes.ps1` / `Test-WeakADPasswords.ps1` started directly from `pwsh`. They now relaunch the calling script with its parameters.
## [2.4.6] — 2026-07-29
+7 -4
View File
@@ -3,14 +3,15 @@ $script:ElysiumVersion = '2.4.6'
function Invoke-RestartWithExecutable {
param(
[string]$ExecutablePath,
[string]$ScriptPath,
[hashtable]$BoundParameters,
[object[]]$UnboundArguments
)
if (-not $ExecutablePath) { return }
if (-not $PSCommandPath) { return }
if (-not $ScriptPath) { return }
$argList = @('-NoLogo', '-NoProfile', '-File', $PSCommandPath)
$argList = @('-NoLogo', '-NoProfile', '-File', $ScriptPath)
if ($BoundParameters) {
foreach ($entry in $BoundParameters.GetEnumerator()) {
@@ -43,7 +44,9 @@ function Restart-WithPwshIfAvailable {
$pwsh = Get-Command -Name 'pwsh' -ErrorAction SilentlyContinue
if (-not $pwsh) { return }
Write-Host ("PowerShell 7 detected at '{0}'; relaunching script under pwsh..." -f $pwsh.Path)
Invoke-RestartWithExecutable -ExecutablePath $pwsh.Path -BoundParameters $BoundParameters -UnboundArguments $UnboundArguments
# $PSCommandPath here would be this helper file (where the function is defined), not the
# script being relaunched; $MyInvocation.PSCommandPath is the script that called us.
Invoke-RestartWithExecutable -ExecutablePath $pwsh.Path -ScriptPath $MyInvocation.PSCommandPath -BoundParameters $BoundParameters -UnboundArguments $UnboundArguments
}
function Restart-WithWindowsPowerShellIfAvailable {
@@ -68,7 +71,7 @@ function Restart-WithWindowsPowerShellIfAvailable {
return
}
Write-Host ("Windows PowerShell detected at '{0}'; relaunching script under powershell.exe..." -f $powershellPath)
Invoke-RestartWithExecutable -ExecutablePath $powershellPath -BoundParameters $BoundParameters -UnboundArguments $UnboundArguments
Invoke-RestartWithExecutable -ExecutablePath $powershellPath -ScriptPath $MyInvocation.PSCommandPath -BoundParameters $BoundParameters -UnboundArguments $UnboundArguments
}
# ---------------------------------------------------------------------------