Skip to content

Instantly share code, notes, and snippets.

@sassdawe
Forked from JustinGrote/ThrowStdOutErrors.ps1
Created November 15, 2022 10:22
Show Gist options
  • Save sassdawe/8921428e0920d053c299908ac972b20b to your computer and use it in GitHub Desktop.
Save sassdawe/8921428e0920d053c299908ac972b20b to your computer and use it in GitHub Desktop.
Catch only specific errors coming from native commands
filter ThrowStdOutErrors($messageFilter,[Parameter(ValueFromPipeline)]$obj) {
if ($obj -is [Management.Automation.ErrorRecord]) {
if ($obj -match $messageFilter) {
throw $obj
} else {
Write-Error $obj
return
}
}
$obj
}
try {
git nomatch 2>&1 | ThrowStdOutErrors 'kab*'
git kablam 2>&1 | ThrowStdOutErrors 'kab*'
} catch {
'Caught kablam!'
}
#Will pass on the error from nomatch and will catch kablam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment