Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active March 7, 2018 19:18
Show Gist options
  • Save Jaykul/7d46df2bd74a45e4ce7379a4575f9f99 to your computer and use it in GitHub Desktop.
Save Jaykul/7d46df2bd74a45e4ce7379a4575f9f99 to your computer and use it in GitHub Desktop.
Minimal effort at error logging
function Verb-Noun {
[CmdletBinding()]
param()
Push-Location -Stack Verb-Noun
# BOILER PLATE ERROR HANDLING
trap { # Standard practice in every script
$ex = $_
do {
$Stack = if($ex.ScriptStackTrace) { $ex.ScriptStackTrace } else { $ex.StackTrace }
$PSCmdlet.WriteInformation(@(
$ex.GetType().Fullname
"Stack Trace:"
($Stack | Out-String -width 100) -split "`n" -replace "^\s*"," "
) -join "`n", ("PSHOST", "Exception"))
} while($ex = if($ex.Exception) { $ex.Exception } else { $ex.InnerException })
Pop-Location -Stack Verb-Noun
throw
} $ErrorActionPreference = "Stop"
# YOUR CODE GOES HERE
}
[CmdletBinding()]
param()
# BOILER PLATE ERROR HANDLING
trap { # Standard practice in every script
$ex = $_
do {
$Stack = if($ex.ScriptStackTrace) { $ex.ScriptStackTrace } else { $ex.StackTrace }
$PSCmdlet.WriteInformation(@(
$ex.GetType().Fullname
"Stack Trace:"
($Stack | Out-String -width 100) -split "`n" -replace "^\s*"," "
) -join "`n", ("PSHOST", "Exception"))
} while($ex = if($ex.Exception) { $ex.Exception } else { $ex.InnerException })
throw
} $ErrorActionPreference = "Stop"
# YOUR CODE GOES HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment