fix(pwsh): fix crash on error in shell with old pwsh (#1861)

Handle missing Get-Error in powershell gracefully.
This commit is contained in:
David Knaack 2020-11-12 17:42:03 +01:00 committed by GitHub
parent d648bb0e00
commit a05be18447
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,10 +21,10 @@ function global:prompt {
# In case we have a False on the Dollar hook, we know there's an error.
if (-not $origDollarQuestion) {
# We retrieve the InvocationInfo from the most recent error.
$lastCmdletError = Get-Error | Where-Object {$_ -ne $null} | Select-Object -expand InvocationInfo
$lastCmdletError = try { Get-Error | Where-Object { $_ -ne $null } | Select-Object -expand InvocationInfo } catch { $null }
# We check if the las command executed matches the line that caused the last error , in which case we know
# it was an internal Powershell command, otherwise, there MUST be an error code.
$lastExitCodeForPrompt = if($lastCmd.CommandLine -eq $lastCmdletError.Line){1} else {$origLastExitCode}
$lastExitCodeForPrompt = if ($null -ne $lastCmdletError -and $lastCmd.CommandLine -eq $lastCmdletError.Line) { 1 } else { $origLastExitCode }
}
$duration = [math]::Round(($lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime).TotalMilliseconds)