1+ function Write-NativeCommandError {
2+ [CmdletBinding ()]
3+ param (
4+ [System.Management.Automation.ErrorRecord ]
5+ $CurrentError
6+ )
7+
8+ if ($CurrentError.FullyQualifiedErrorId -eq " NativeCommandErrorMessage" ) { return }
9+
10+ $myinv = $CurrentError.InvocationInfo
11+ if ($myinv -and $myinv.MyCommand ) {
12+ switch - regex ( $myinv.MyCommand.CommandType ) {
13+ ([System.Management.Automation.CommandTypes ]::ExternalScript) {
14+ if ($myinv.MyCommand.Path ) {
15+ $myinv.MyCommand.Path + " : "
16+ }
17+ break
18+ }
19+ ([System.Management.Automation.CommandTypes ]::Script) {
20+ if ($myinv.MyCommand.ScriptBlock ) {
21+ $myinv.MyCommand.ScriptBlock.ToString () + " : "
22+ }
23+ break
24+ }
25+ default {
26+ if ($myinv.InvocationName -match ' ^[&\.]?$' ) {
27+ if ($myinv.MyCommand.Name ) {
28+ $myinv.MyCommand.Name + " : "
29+ }
30+ } else {
31+ $myinv.InvocationName + " : "
32+ }
33+ break
34+ }
35+ }
36+ } elseif ($myinv -and $myinv.InvocationName ) {
37+ $myinv.InvocationName + " : "
38+ }
39+ }
40+
41+ function ConvertTo-ErrorCategoryView {
42+ [CmdletBinding ()]
43+ param (
44+ [System.Management.Automation.ErrorRecord ]
45+ $CurrentError
46+ )
47+
48+ $CurrentError.CategoryInfo.GetMessage ()
49+ }
50+
51+
52+ function ConvertTo-ErrorSimpleView {
53+ [CmdletBinding ()]
54+ param (
55+ [System.Management.Automation.ErrorRecord ]
56+ $CurrentError
57+ )
58+
59+ if ($CurrentError.FullyQualifiedErrorId -eq " NativeCommandErrorMessage" ) {
60+ $CurrentError.Exception.Message
61+ } else {
62+ $myinv = $CurrentError.InvocationInfo
63+ if ($myinv -and ($myinv.MyCommand -or ($CurrentError.CategoryInfo.Category -ne ' ParserError' ))) {
64+ $posmsg = $myinv.PositionMessage
65+ } else {
66+ $posmsg = " "
67+ }
68+
69+ if ($posmsg -ne " " ) {
70+ $posmsg = " `n " + $posmsg
71+ }
72+
73+ if ( & { Set-StrictMode - Version 1 ; $CurrentError.PSMessageDetails } ) {
74+ $posmsg = " : " + $CurrentError.PSMessageDetails + $posmsg
75+ }
76+
77+ $indent = 4
78+ $width = $host.UI.RawUI.BufferSize.Width - $indent - 2
79+
80+ $originInfo = & { Set-StrictMode - Version 1 ; $CurrentError.OriginInfo }
81+ if (($null -ne $originInfo ) -and ($null -ne $originInfo.PSComputerName )) {
82+ $indentString = " + PSComputerName : " + $originInfo.PSComputerName
83+ $posmsg += " `n "
84+ foreach ($line in @ ($indentString -split " (.{$width })" )) {
85+ if ($line ) {
86+ $posmsg += (" " * $indent + $line )
87+ }
88+ }
89+ }
90+
91+ if (! $CurrentError.ErrorDetails -or ! $CurrentError.ErrorDetails.Message ) {
92+ $CurrentError.Exception.Message + $posmsg + " `n "
93+ } else {
94+ $CurrentError.ErrorDetails.Message + $posmsg
95+ }
96+ }
97+ }
98+
99+ function ConvertTo-ErrorNormalView {
100+ [CmdletBinding ()]
101+ param (
102+ [System.Management.Automation.ErrorRecord ]
103+ $CurrentError
104+ )
105+
106+ if ($CurrentError.FullyQualifiedErrorId -eq " NativeCommandErrorMessage" ) {
107+ $CurrentError.Exception.Message
108+ } else {
109+ $myinv = $CurrentError.InvocationInfo
110+ if ($myinv -and ($myinv.MyCommand -or ($CurrentError.CategoryInfo.Category -ne ' ParserError' ))) {
111+ $posmsg = $myinv.PositionMessage
112+ } else {
113+ $posmsg = " "
114+ }
115+
116+ if ($posmsg -ne " " ) {
117+ $posmsg = " `n " + $posmsg
118+ }
119+
120+ if ( & { Set-StrictMode - Version 1 ; $CurrentError.PSMessageDetails } ) {
121+ $posmsg = " : " + $CurrentError.PSMessageDetails + $posmsg
122+ }
123+
124+ $indent = 4
125+ $width = $host.UI.RawUI.BufferSize.Width - $indent - 2
126+
127+ $errorCategoryMsg = & { Set-StrictMode - Version 1 ; $CurrentError.ErrorCategory_Message }
128+ if ($null -ne $errorCategoryMsg ) {
129+ $indentString = " + CategoryInfo : " + $CurrentError.ErrorCategory_Message
130+ } else {
131+ $indentString = " + CategoryInfo : " + $CurrentError.CategoryInfo
132+ }
133+ $posmsg += " `n "
134+ foreach ($line in @ ($indentString -split " (.{$width })" )) {
135+ if ($line ) {
136+ $posmsg += (" " * $indent + $line )
137+ }
138+ }
139+
140+ $indentString = " + FullyQualifiedErrorId : " + $CurrentError.FullyQualifiedErrorId
141+ $posmsg += " `n "
142+ foreach ($line in @ ($indentString -split " (.{$width })" )) {
143+ if ($line ) {
144+ $posmsg += (" " * $indent + $line )
145+ }
146+ }
147+
148+ $originInfo = & { Set-StrictMode - Version 1 ; $CurrentError.OriginInfo }
149+ if (($null -ne $originInfo ) -and ($null -ne $originInfo.PSComputerName )) {
150+ $indentString = " + PSComputerName : " + $originInfo.PSComputerName
151+ $posmsg += " `n "
152+ foreach ($line in @ ($indentString -split " (.{$width })" )) {
153+ if ($line ) {
154+ $posmsg += (" " * $indent + $line )
155+ }
156+ }
157+ }
158+
159+ if (! $CurrentError.ErrorDetails -or ! $CurrentError.ErrorDetails.Message ) {
160+ $CurrentError.Exception.Message + $posmsg + " `n "
161+ } else {
162+ $CurrentError.ErrorDetails.Message + $posmsg
163+ }
164+ }
165+ }
0 commit comments