From d7f30c284150165051cf09e6dafc6a8f6c1d0b3e Mon Sep 17 00:00:00 2001 From: StartAutomating Date: Thu, 20 Jul 2023 07:29:00 +0000 Subject: [PATCH] Adding Posh.Errors Fixes #4, Fixes #18, Fixes #19, Fixes #20, Fixes #21, Fixes #22 --- Posh.types.ps1xml | 132 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/Posh.types.ps1xml b/Posh.types.ps1xml index 3d1aeab..d8a95d1 100644 --- a/Posh.types.ps1xml +++ b/Posh.types.ps1xml @@ -1,6 +1,138 @@ + + Posh.Errors + + + LineLike + + + + LineMatch + + + + ByLine + + <# +.SYNOPSIS + Gets errors by line +.DESCRIPTION + Gets all lines that produced errors +.EXAMPLE + $error.ByLine +#> +$errorsByLine = + [Collections.Generic.Dictionary[ + string, + [Collections.Generic.List[psobject]] + ]]::new([System.StringComparer]::OrdinalIgnoreCase) + + +foreach ($err in $this) { + + $errLine = $err.InvocationInfo.Line + if (-not $errLine) { continue } + if (-not $errorsByLine[$errLine]) { + $errorsByLine[$errLine] = [Collections.Generic.List[psobject]]::new() + } + $null = $errorsByLine[$errLine].Add($err) +} +$errorsByLine + + + + + ByType + + <# + +#> +$errorsByType = + [Collections.Generic.Dictionary[ + type, + [Collections.Generic.List[psobject]] + ]]::new() + + +foreach ($err in $this) { + + $exceptionType = if ($err.Exception) { + $err.Exception.GetType() + } else { + $err.GetType() + } + if (-not $errorsByType[$exceptionType]) { + $errorsByType[$exceptionType] = [Collections.Generic.List[psobject]]::new() + } + $null = $errorsByType[$exceptionType].Add($err) +} +$errorsByType + + + + + History + + <# +.SYNOPSIS + Gets Error History +.DESCRIPTION + Gets the history items associated with PowerShell Errors +.EXAMPLE + $Error.History +#> +$historyIDS = @(foreach ($err in $this) { + if ($err.InvocationInfo.HistoryId -gt 0) { + $err.InvocationInfo.HistoryId + } +}) + +Get-History -Id $historyIDS + + + + Posh.Host