Skip to content

Instantly share code, notes, and snippets.

@Swoogan
Created May 13, 2015 15:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Swoogan/ddaee2b8af239def6717 to your computer and use it in GitHub Desktop.
Wrapping commandline tools in a scriptblock to improve error handling
function Exec([scriptblock]$cmd) {
$result = & $cmd
if ($LastExitCode -ne 0) {
throw $result[-50..-1] -join "`n"
}
else {
foreach ($line in $result) {
Write-Host $line
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment