Using PowerShell to Decode HResults

Here is some code that breaks a HResul (the 8 digit hex error codes or long negative decimal number error codes) into its subcomponents, this can make it easier to identify the source of an error…

You can read more about HRESULT here https://msdn.microsoft.com/en-us/library/cc231198.aspx

This requires PowerShell v3.0 or higher for the bitwise operations.

Script can be downloaded here: http://1drv.ms/1NtDBSL

#based on winerror.h from Windows 10 SDK function Get-HresultFailed($hr) { return $hr -lt 0 } function Get-HresultCode($hr) { return $hr -band 0xFFFF } function Get-ErrorMessage($code) { $ex = New-Object System.ComponentModel.Win32Exception($code) return $ex.Message } function Get-HresultFacility($hr) { $facilityCode = (($hr) -shr 16) -band 0x1fff switch ($facilityCode) { 0 { return "FACILITY_NULL" } 1 { return "FACILITY_RPC" } 2 { return "FACILITY_DISPATCH" } 3 { return "FACILITY_STORAGE" } 4 { return "FACILITY_ITF" } 7 { return "FACILITY_WIN32" } 8 { return "FACILITY_WINDOWS" } 9 { return "FACILITY_SSPI" } 9 { return "FACILITY_SECURITY" } 10 { return "FACILITY_CONTROL" } 11 { return "FACILITY_CERT" } 12 { return "FACILITY_INTERNET" } 13 { return "FACILITY_MEDIASERVER" } 14 { return "FACILITY_MSMQ" } 15 { return "FACILITY_SETUPAPI" } 16 { return "FACILITY_SCARD" } 17 { return "FACILITY_COMPLUS" } 18 { return "FACILITY_AAF" } 19 { return "FACILITY_URT" } 20 { return "FACILITY_ACS" } 21 { return "FACILITY_DPLAY" } 22 { return "FACILITY_UMI" } 23 { return "FACILITY_SXS" } 24 { return "FACILITY_WINDOWS_CE" } 25 { return "FACILITY_HTTP" } 26 { return "FACILITY_USERMODE_COMMONLOG" } 27 { return "FACILITY_WER" } 31 { return "FACILITY_USERMODE_FILTER_MANAGER" } 32 { return "FACILITY_BACKGROUNDCOPY" } 33 { return "FACILITY_WIA" } 33 { return "FACILITY_CONFIGURATION" } 34 { return "FACILITY_STATE_MANAGEMENT" } 35 { return "FACILITY_METADIRECTORY" } 36 { return "FACILITY_WINDOWSUPDATE" } 37 { return "FACILITY_DIRECTORYSERVICE" } 38 { return "FACILITY_GRAPHICS" } 39 { return "FACILITY_SHELL" } 39 { return "FACILITY_NAP" } 40 { return "FACILITY_TPM_SERVICES" } 41 { return "FACILITY_TPM_SOFTWARE" } 42 { return "FACILITY_UI" } 43 { return "FACILITY_XAML" } 44 { return "FACILITY_ACTION_QUEUE" } 48 { return "FACILITY_WINDOWS_SETUP" } 48 { return "FACILITY_PLA" } 49 { return "FACILITY_FVE" } 50 { return "FACILITY_FWP" } 51 { return "FACILITY_WINRM" } 52 { return "FACILITY_NDIS" } 53 { return "FACILITY_USERMODE_HYPERVISOR" } 54 { return "FACILITY_CMI" } 55 { return "FACILITY_USERMODE_VIRTUALIZATION" } 56 { return "FACILITY_USERMODE_VOLMGR" } 57 { return "FACILITY_BCD" } 58 { return "FACILITY_USERMODE_VHD" } 60 { return "FACILITY_SDIAG" } 61 { return "FACILITY_WINPE" } 61 { return "FACILITY_WEBSERVICES" } 62 { return "FACILITY_WPN" } 63 { return "FACILITY_WINDOWS_STORE" } 64 { return "FACILITY_INPUT" } 66 { return "FACILITY_EAP" } 80 { return "FACILITY_WINDOWS_DEFENDER" } 81 { return "FACILITY_OPC" } 82 { return "FACILITY_XPS" } 83 { return "FACILITY_RAS" } 84 { return "FACILITY_POWERSHELL" } 84 { return "FACILITY_MBN" } 85 { return "FACILITY_EAS" } 98 { return "FACILITY_P2P_INT" } 99 { return "FACILITY_P2P" } 100 { return "FACILITY_DAF" } 101 { return "FACILITY_BLUETOOTH_ATT" } 102 { return "FACILITY_AUDIO" } 103 { return "FACILITY_STATEREPOSITORY" } 109 { return "FACILITY_VISUALCPP" } 112 { return "FACILITY_SCRIPT" } 113 { return "FACILITY_PARSE" } 120 { return "FACILITY_BLB" } 121 { return "FACILITY_BLB_CLI" } 122 { return "FACILITY_WSBAPP" } 128 { return "FACILITY_BLBUI" } 129 { return "FACILITY_USN" } 130 { return "FACILITY_USERMODE_VOLSNAP" } 131 { return "FACILITY_TIERING" } 133 { return "FACILITY_WSB_ONLINE" } 134 { return "FACILITY_ONLINE_ID" } 153 { return "FACILITY_DLS" } 160 { return "FACILITY_SOS" } 176 { return "FACILITY_DEBUGGERS" } 208 { return "FACILITY_DELIVERY_OPTIMIZATION" } 231 { return "FACILITY_USERMODE_SPACES" } 232 { return "FACILITY_USER_MODE_SECURITY_CORE" } 234 { return "FACILITY_USERMODE_LICENSING" } 256 { return "FACILITY_SPP" } 256 { return "FACILITY_RESTORE" } 256 { return "FACILITY_DMSERVER" } 257 { return "FACILITY_DEPLOYMENT_SERVICES_SERVER" } 258 { return "FACILITY_DEPLOYMENT_SERVICES_IMAGING" } 259 { return "FACILITY_DEPLOYMENT_SERVICES_MANAGEMENT" } 260 { return "FACILITY_DEPLOYMENT_SERVICES_UTIL" } 261 { return "FACILITY_DEPLOYMENT_SERVICES_BINLSVC" } 263 { return "FACILITY_DEPLOYMENT_SERVICES_PXE" } 264 { return "FACILITY_DEPLOYMENT_SERVICES_TFTP" } 272 { return "FACILITY_DEPLOYMENT_SERVICES_TRANSPORT_MANAGEMENT" } 278 { return "FACILITY_DEPLOYMENT_SERVICES_DRIVER_PROVISIONING" } 289 { return "FACILITY_DEPLOYMENT_SERVICES_MULTICAST_SERVER" } 290 { return "FACILITY_DEPLOYMENT_SERVICES_MULTICAST_CLIENT" } 293 { return "FACILITY_DEPLOYMENT_SERVICES_CONTENT_PROVIDER" } 305 { return "FACILITY_LINGUISTIC_SERVICES" } 885 { return "FACILITY_WEB" } 886 { return "FACILITY_WEB_SOCKET" } 1094 { return "FACILITY_AUDIOSTREAMING" } 1536 { return "FACILITY_ACCELERATOR" } 1793 { return "FACILITY_MOBILE" } 1967 { return "FACILITY_SQLITE" } 1989 { return "FACILITY_UTC" } 1996 { return "FACILITY_WMAAECMA" } 2049 { return "FACILITY_WEP" } 2050 { return "FACILITY_SYNCENGINE" } 2168 { return "FACILITY_DIRECTMUSIC" } 2169 { return "FACILITY_DIRECT3D10" } 2170 { return "FACILITY_DXGI" } 2171 { return "FACILITY_DXGI_DDI" } 2172 { return "FACILITY_DIRECT3D11" } 2173 { return "FACILITY_DIRECT3D11_DEBUG" } 2174 { return "FACILITY_DIRECT3D12" } 2175 { return "FACILITY_DIRECT3D12_DEBUG" } 2184 { return "FACILITY_LEAP" } 2185 { return "FACILITY_AUDCLNT" } 2200 { return "FACILITY_WINCODEC_DWRITE_DWM" } 2201 { return "FACILITY_DIRECT2D" } 2304 { return "FACILITY_DEFRAG" } 2305 { return "FACILITY_USERMODE_SDBUS" } 2306 { return "FACILITY_JSCRIPT" } 2339 { return "FACILITY_XBOX" } 2561 { return "FACILITY_PIDGENX" } } return "FACILITY_UNKNOWN_" + $facilityCode } $hresult = 0x800700C1 Get-HResultFailed($hresult) Get-HResultFacility($hresult) Get-HResultCode($hresult) # will return error text for FACILITY_WIN32 Get-ErrorMessage($hresult)

About chentiangemalc

specializes in end-user computing technologies. disclaimer 1) use at your own risk. test any solution in your environment. if you do not understand the impact/consequences of what you're doing please stop, and ask advice from somebody who does. 2) views are my own at the time of posting and do not necessarily represent my current view or the view of my employer and family members/relatives. 3) over the years Microsoft/Citrix/VMWare have given me a few free shirts, pens, paper notebooks/etc. despite these gifts i will try to remain unbiased.
This entry was posted in PowerShell and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s