Skip to content

Instantly share code, notes, and snippets.

@marcinotorowski
Created January 24, 2020 20:06
Show Gist options
  • Save marcinotorowski/8c09fc556469b22a9df421be51e370b2 to your computer and use it in GitHub Desktop.
Save marcinotorowski/8c09fc556469b22a9df421be51e370b2 to your computer and use it in GitHub Desktop.
A simple PowerShell class which converts HRESULT into a human-friendly error
class ActualError {
[int]$HResult;
[string]$HResultHex;
[string]$HResultBin;
[string]$Facility;
[int]$FacilityCode;
[int]$ErrorCode;
[bool]$IsFailure;
[string]$Message;
[bool]$IsCustom;
[bool]$IsServerFailure;
}
function Get-ActualError
{
param([int]$hresult)
$actualError = [ActualError]::new();
$actualError.HResult = $hresult;
$actualError.HResultBin = [System.Convert]::ToString($hresult,2).PadLeft(32,'0');
$actualError.HResultHex = "0x" + $hresult.ToString("X2");
$actualError.Message = (New-Object System.ComponentModel.Win32Exception($hresult)).Message;
$actualError.ErrorCode = $hresult -band 0xFFFF;
$hresult = $hresult -shr 16;
$actualError.FacilityCode = $hresult -band 0x7FF;
$hresult = $hresult -shr 11;
$hresult = $hresult -shr 1;
$hresult = $hresult -shr 1;
$actualError.IsCustom = ($hresult -band 1) -eq 1;
$hresult = $hresult -shr 1;
$actualError.IsServerFailure = ($hresult -band 1) -eq 1;
$hresult = $hresult -shr 1;
$actualError.IsFailure = ($hresult -band 1) -eq 1;
$actualError.Facility = switch ($actualError.FacilityCode)
{
0 { 'FACILITY_NULL'; }
1 { 'FACILITY_RPC'; }
2 { 'FACILITY_DISPATCH'; }
3 { 'FACILITY_STORAGE'; }
4 { 'FACILITY_ITF'; }
7 { 'FACILITY_WIN32'; }
8 { 'FACILITY_WINDOWS'; }
9 { 'FACILITY_SECURITY'; }
9 { 'FACILITY_SSPI'; }
10 { 'FACILITY_CONTROL'; }
11 { 'FACILITY_CERT'; }
12 { 'FACILITY_INTERNET'; }
13 { 'FACILITY_MEDIASERVER'; }
14 { 'FACILITY_MSMQ'; }
15 { 'FACILITY_SETUPAPI'; }
16 { 'FACILITY_SCARD'; }
17 { 'FACILITY_COMPLUS'; }
18 { 'FACILITY_AAF'; }
19 { 'FACILITY_URT'; }
20 { 'FACILITY_ACS'; }
21 { 'FACILITY_DPLAY'; }
22 { 'FACILITY_UMI'; }
23 { 'FACILITY_SXS'; }
24 { 'FACILITY_WINDOWS_CE'; }
25 { 'FACILITY_HTTP'; }
26 { 'FACILITY_USERMODE_COMMONLOG'; }
27 { 'FACILITY_WER'; }
31 { 'FACILITY_USERMODE_FILTER_MANAGER'; }
32 { 'FACILITY_BACKGROUNDCOPY'; }
33 { 'FACILITY_CONFIGURATION'; }
33 { 'FACILITY_WIA'; }
34 { 'FACILITY_STATE_MANAGEMENT'; }
35 { 'FACILITY_METADIRECTORY'; }
36 { 'FACILITY_WINDOWSUPDATE'; }
37 { 'FACILITY_DIRECTORYSERVICE'; }
38 { 'FACILITY_GRAPHICS'; }
39 { 'FACILITY_NAP'; }
39 { 'FACILITY_SHELL'; }
40 { 'FACILITY_TPM_SERVICES'; }
41 { 'FACILITY_TPM_SOFTWARE'; }
42 { 'FACILITY_UI'; }
43 { 'FACILITY_XAML'; }
44 { 'FACILITY_ACTION_QUEUE'; }
48 { 'FACILITY_PLA'; }
48 { 'FACILITY_WINDOWS_SETUP'; }
49 { 'FACILITY_FVE'; }
50 { 'FACILITY_FWP'; }
51 { 'FACILITY_WINRM'; }
52 { 'FACILITY_NDIS'; }
53 { 'FACILITY_USERMODE_HYPERVISOR'; }
54 { 'FACILITY_CMI'; }
55 { 'FACILITY_USERMODE_VIRTUALIZATION'; }
56 { 'FACILITY_USERMODE_VOLMGR'; }
57 { 'FACILITY_BCD'; }
58 { 'FACILITY_USERMODE_VHD'; }
60 { 'FACILITY_SDIAG'; }
61 { 'FACILITY_WEBSERVICES'; }
61 { 'FACILITY_WINPE'; }
62 { 'FACILITY_WPN'; }
63 { 'FACILITY_WINDOWS_STORE'; }
64 { 'FACILITY_INPUT'; }
66 { 'FACILITY_EAP'; }
80 { 'FACILITY_WINDOWS_DEFENDER'; }
81 { 'FACILITY_OPC'; }
82 { 'FACILITY_XPS'; }
83 { 'FACILITY_RAS'; }
84 { 'FACILITY_MBN'; }
84 { 'FACILITY_POWERSHELL'; }
85 { 'FACILITY_EAS'; }
98 { 'FACILITY_P2P_INT'; }
99 { 'FACILITY_P2P'; }
100 { 'FACILITY_DAF'; }
101 { 'FACILITY_BLUETOOTH_ATT'; }
102 { 'FACILITY_AUDIO'; }
109 { 'FACILITY_VISUALCPP'; }
112 { 'FACILITY_SCRIPT'; }
113 { 'FACILITY_PARSE'; }
120 { 'FACILITY_BLB'; }
121 { 'FACILITY_BLB_CLI'; }
122 { 'FACILITY_WSBAPP'; }
128 { 'FACILITY_BLBUI'; }
129 { 'FACILITY_USN'; }
130 { 'FACILITY_USERMODE_VOLSNAP'; }
131 { 'FACILITY_TIERING'; }
133 { 'FACILITY_WSB_ONLINE'; }
134 { 'FACILITY_ONLINE_ID'; }
153 { 'FACILITY_DLS'; }
160 { 'FACILITY_SOS'; }
176 { 'FACILITY_DEBUGGERS'; }
231 { 'FACILITY_USERMODE_SPACES'; }
256 { 'FACILITY_DMSERVER'; }
256 { 'FACILITY_RESTORE'; }
256 { 'FACILITY_SPP'; }
257 { 'FACILITY_DEPLOYMENT_SERVICES_SERVER'; }
258 { 'FACILITY_DEPLOYMENT_SERVICES_IMAGING'; }
259 { 'FACILITY_DEPLOYMENT_SERVICES_MANAGEMENT'; }
260 { 'FACILITY_DEPLOYMENT_SERVICES_UTIL'; }
261 { 'FACILITY_DEPLOYMENT_SERVICES_BINLSVC'; }
263 { 'FACILITY_DEPLOYMENT_SERVICES_PXE'; }
264 { 'FACILITY_DEPLOYMENT_SERVICES_TFTP'; }
272 { 'FACILITY_DEPLOYMENT_SERVICES_TRANSPORT_MANAGEMENT'; }
278 { 'FACILITY_DEPLOYMENT_SERVICES_DRIVER_PROVISIONING'; }
289 { 'FACILITY_DEPLOYMENT_SERVICES_MULTICAST_SERVER'; }
290 { 'FACILITY_DEPLOYMENT_SERVICES_MULTICAST_CLIENT'; }
293 { 'FACILITY_DEPLOYMENT_SERVICES_CONTENT_PROVIDER'; }
305 { 'FACILITY_LINGUISTIC_SERVICES'; }
885 { 'FACILITY_WEB'; }
886 { 'FACILITY_WEB_SOCKET'; }
1094 { 'FACILITY_AUDIOSTREAMING'; }
1536 { 'FACILITY_ACCELERATOR'; }
1793 { 'FACILITY_MOBILE'; }
1996 { 'FACILITY_WMAAECMA'; }
2049 { 'FACILITY_WEP'; }
2050 { 'FACILITY_SYNCENGINE'; }
2168 { 'FACILITY_DIRECTMUSIC'; }
2169 { 'FACILITY_DIRECT3D10'; }
2170 { 'FACILITY_DXGI'; }
2171 { 'FACILITY_DXGI_DDI'; }
2172 { 'FACILITY_DIRECT3D11'; }
2184 { 'FACILITY_LEAP'; }
2185 { 'FACILITY_AUDCLNT'; }
2200 { 'FACILITY_WINCODEC_DWRITE_DWM'; }
2201 { 'FACILITY_DIRECT2D'; }
2304 { 'FACILITY_DEFRAG'; }
2305 { 'FACILITY_USERMODE_SDBUS'; }
2306 { 'FACILITY_JSCRIPT'; }
2561 { 'FACILITY_PIDGENX'; }
}
return $actualError;
}
@marcinotorowski
Copy link
Author

Usage:
Get-ActualError 0x800706BA

Result:
HResult : -2147023174
HResultHex : 0x800706BA
HResultBin : 10000000000001110000011010111010
Facility : FACILITY_WIN32
FacilityCode : 7
ErrorCode : 1722
IsFailure : True
Message : RPC server is unavailable
IsCustom : False
IsServerFailure : False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment