forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrite-Exception.ps1
37 lines (30 loc) · 877 Bytes
/
Write-Exception.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Function Write-Exception
{
<#
.SYNOPSIS
Internal function. Writes exception to disk (my docs\dbatools-exceptions.txt) for later analysis.
.PARAMETER e
Exception
.EXAMPLE
Write-Exception $_
Writes inner exception to disk
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[object]$e
)
$docs = [Environment]::GetFolderPath("mydocuments")
$errorlog = "$docs\dbatools-exceptions.txt"
$message = $e.Exception
$invocation = $e.InvocationInfo
$position = $invocation.PositionMessage
$scriptname = $invocation.ScriptName
if ($e.Exception.InnerException -ne $null) { $message = $e.Exception.InnerException }
$message = $message.ToString()
Add-Content $errorlog $(Get-Date)
Add-Content $errorlog $scriptname
Add-Content $errorlog $position
Add-Content $errorlog $message
Write-Warning "See error log $(Resolve-Path $errorlog) for more details."
}