Private/Get-WUHResultDescription.ps1

function Get-WUHResultDescription {
    <#
    .SYNOPSIS
    Converts Windows Update HResult codes to human-readable descriptions.
     
    .DESCRIPTION
    This function converts Windows Update HResult error codes (both positive and negative integers,
    as well as hexadecimal strings) into human-readable descriptions that help diagnose installation
    and download failures.
     
    .PARAMETER HResult
    The HResult code to convert. Can be:
    - Negative integer (e.g., -2145099769)
    - Positive integer (e.g., 2149867527)
    - Hexadecimal string (e.g., "0x80246007")
     
    .EXAMPLE
    Get-WUHResultDescription -HResult -2145099769
    Returns: 0x80246007 - WU_E_DM_NOTDOWNLOADED - Update has not been downloaded
     
    .EXAMPLE
    Get-WUHResultDescription -HResult "0x80240022"
    Returns: 0x80240022 - WU_E_ALL_UPDATES_FAILED - Operation failed for all updates
     
    .EXAMPLE
    Get-WUHResultDescription -HResult 2149867527
    Returns: 0x80246007 - WU_E_DM_NOTDOWNLOADED - Update has not been downloaded
     
    .OUTPUTS
    System.String
    Returns a formatted string with the hex code and human-readable description.
     
    .NOTES
    This function is particularly useful for troubleshooting Windows Update installation failures.
    The most common error codes include download failures, installation blocks, and network issues.
     
    Author: CSOLVE Scripts
    Version: 1.0.0
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [object]$HResult
    )
    
    process {
        # Convert HResult to hex format
        if ($HResult -is [string] -and $HResult.StartsWith('0x')) {
            $hexCode = $HResult.ToLower()
        } elseif ($HResult -is [string] -and ($HResult -match '^-?\d+$')) {
            # String containing a number
            try {
                $numericValue = [int]$HResult
                $unsigned = [BitConverter]::ToUInt32([BitConverter]::GetBytes($numericValue), 0)
                $hexCode = "0x{0:x8}" -f $unsigned
            } catch {
                return "Error converting HResult $HResult : $($_.Exception.Message)"
            }
        } elseif ($HResult -is [int] -or $HResult -is [long] -or $HResult -is [int32] -or $HResult -is [int64]) {
            try {
                $unsigned = [BitConverter]::ToUInt32([BitConverter]::GetBytes([int]$HResult), 0)
                $hexCode = "0x{0:x8}" -f $unsigned
            } catch {
                return "Error converting HResult $HResult : $($_.Exception.Message)"
            }
        } else {
            return "Invalid HResult format: $HResult (Type: $($HResult.GetType().Name))"
        }
        
        $errorDescriptions = @{
            # Windows Update Download Manager errors
            '0x80246007' = 'WU_E_DM_NOTDOWNLOADED - Update has not been downloaded'
            '0x80246008' = 'WU_E_DM_FAILTOCONNECTTOBITS - Download manager could not connect to BITS'
            '0x80246010' = 'WU_E_DM_DOWNLOADSANDBOXNOTFOUND - The sandbox directory for the downloaded update was not found'
            
            # Windows Update Agent / WUAPI errors
            '0x80240022' = 'WU_E_ALL_UPDATES_FAILED - Operation failed for all updates'
            '0x8024402f' = 'WU_E_PT_ECP_SUCCEEDED_WITH_ERRORS - External cab processing finished with errors'
            '0x80244007' = 'WU_E_PT_SOAPCLIENT_SOAPFAULT - SOAP fault returned by server'
            '0x80240034' = 'WU_E_DOWNLOAD_FAILED - Update failed to download'
            '0x80240016' = 'WU_E_INSTALL_NOT_ALLOWED - Another install/reboot in progress'
            '0x80240020' = 'WU_E_NO_INTERACTIVE_USER - No logged-on interactive user'
            '0x80240017' = 'WU_E_NOT_APPLICABLE - Update not applicable to this system'
            '0x80240031' = 'WU_E_INVALID_FILE - File is in the wrong format'
            '0x80240032' = 'WU_E_INVALID_CRITERIA - Search criteria string is invalid'
            '0x8024001e' = 'WU_E_SERVICE_STOP - Operation aborted: service or system shutting down'
            '0x8024002e' = 'WU_E_WU_DISABLED - Access to unmanaged server is disallowed by policy'
            '0x80245001' = 'WU_E_REDIRECTOR_LOAD_XML - The redirector XML document could not be loaded'
            '0x80245002' = 'WU_E_REDIRECTOR_S_FALSE - The redirector XML document is missing some required information'
            '0x80245003' = 'WU_E_REDIRECTOR_ID_SMALLER - The redirector ID in the downloaded redirector cab is less than in the cached cab'
            '0x80245004' = 'WU_E_REDIRECTOR_UNKNOWN_SERVICE - The service ID is not supported by the redirector'
            
            # HTTP and network errors
            '0x80244019' = 'WU_E_PT_HTTP_STATUS_NOT_FOUND - HTTP 404 from update server'
            '0x8024401f' = 'WU_E_PT_HTTP_STATUS_SERVER_ERROR - HTTP 500 from update server'
            '0x80244018' = 'WU_E_PT_HTTP_STATUS_FORBIDDEN - HTTP 403 from update server'
            '0x8024401b' = 'WU_E_PT_HTTP_STATUS_PROXY_AUTH_REQ - HTTP 407 proxy auth required'
            '0x80244022' = 'WU_E_PT_HTTP_STATUS_SERVICE_UNAVAIL - HTTP 503 service unavailable'
            '0x8024402c' = 'WU_E_PT_WINHTTP_NAME_NOT_RESOLVED - Name could not be resolved'
            '0x80240438' = 'WU_E_PT_ENDPOINT_UNREACHABLE - No route or network connectivity to the Windows Update endpoint'
            '0x8024402d' = 'WU_E_PT_LOAD_SHEDDING - The server is shedding load (transient; retry later)'
            
            # BITS errors
            '0x80200010' = 'BG_E_INSUFFICIENT_RANGE_SUPPORT - Remote server does not support partial content transfers'
            '0x80200011' = 'BG_E_INSUFFICIENT_HTTP_SUPPORT - Remote server does not support HTTP/1.1'
            '0x80200012' = 'BG_E_MISSING_FILE_SIZE - File size could not be determined'
            '0x80200013' = 'BG_E_PARTIAL_FILE - Partial file transfer failed'
            '0x80200014' = 'BG_E_INVALID_STATE - BITS job is in an invalid state'
            '0x80200015' = 'BG_E_EMPTY - BITS queue is empty'
            
            # Setup and installation errors
            '0x80070643' = 'ERROR_INSTALL_FAILURE - Fatal error during installation'
            '0x80070652' = 'ERROR_INSTALL_ALREADY_RUNNING - Another installation is already in progress'
            '0x80070490' = 'ERROR_NOT_FOUND - Component or element not found (often indicates component-store corruption)'
            '0x800f0982' = 'PSFX_E_MATCHING_COMPONENT_NOT_FOUND - Missing files/component store corruption'
            '0x80073712' = 'ERROR_SXS_COMPONENT_STORE_CORRUPT - The component store has been corrupted'
            '0x80073701' = 'ERROR_SXS_ASSEMBLY_MISSING - The referenced assembly is not installed on your system'
            
            # Feature update specific errors
            '0xc1900200' = 'MOSETUP_E_COMPAT_SCANONLY - PC does not meet minimum requirements'
            '0xc1900208' = 'MOSETUP_E_COMPAT_INSTALLREQ_BLOCK - Upgrade blocked by incompatible app/driver'
            '0xc1900204' = 'MOSETUP_E_COMPAT_MIGCHOICE_BLOCK - Migration choice blocks upgrade'
            '0xc1900210' = 'MOSETUP_E_COMPAT_SYSREQ_BLOCK - System requirements not met'
            
            # Generic system errors
            '0x80070005' = 'ERROR_ACCESS_DENIED - Access is denied'
            '0x8007000e' = 'ERROR_OUTOFMEMORY - Not enough storage is available to complete the operation'
            '0x80070057' = 'ERROR_INVALID_PARAMETER - The parameter is incorrect'
            '0x80070002' = 'ERROR_FILE_NOT_FOUND - The system cannot find the file specified'
            '0x8007045b' = 'ERROR_SHUTDOWN_IN_PROGRESS - Windows is shutting down'
            '0x800706be' = 'RPC_S_CALL_FAILED - The remote procedure call failed'
            '0x80070003' = 'ERROR_PATH_NOT_FOUND - The system cannot find the path specified'
            '0x800700c1' = 'ERROR_BAD_EXE_FORMAT - Not a valid Win32 application'
            '0x80070641' = 'ERROR_INSTALL_SERVICE_FAILURE - Windows Installer service could not be accessed'
            '0x800706ba' = 'RPC_S_SERVER_UNAVAILABLE - The RPC server is unavailable'
            
            # Windows Update service errors
            '0x8024a000' = 'WU_E_AU_NOSERVICE - Automatic Updates cannot service requests'
            '0x8024a002' = 'WU_E_AU_NONLEGACYSERVER - Legacy AU client stopped; WSUS upgraded'
            '0x8024a003' = 'WU_E_AU_LEGACYCLIENTDISABLED - Legacy AU client disabled'
            '0x8024a004' = 'WU_E_AU_PAUSED - Automatic Updates is paused'
            '0x8024a005' = 'WU_E_AU_NO_REGISTERED_SERVICE - No unmanaged service registered with AU'
            
            # Data store errors
            '0x80248007' = 'WU_E_DS_NODATA - Requested information is not in the data store'
            '0x80248008' = 'WU_E_DS_MISSINGDATA - The data store is missing required information'
            '0x80248009' = 'WU_E_DS_MISSINGREF - The data store is missing required information or has a null reference'
            
            # Network connectivity errors
            '0x80072efe' = 'ERROR_WINHTTP_CONNECTION_ERROR - A connection with the server could not be established'
            '0x80072ee2' = 'ERROR_WINHTTP_TIMEOUT - The operation timed out'
            '0x80072f8f' = 'ERROR_WINHTTP_SECURE_FAILURE - A security/SSL error occurred'
            '0x80072ee7' = 'ERROR_WINHTTP_NAME_NOT_RESOLVED - The server name or address could not be resolved'
            '0x80072f76' = 'ERROR_WINHTTP_INVALID_SERVER_RESPONSE - The specified server cannot perform the requested operation'
            '0x80072af9' = 'WSAHOST_NOT_FOUND - Host not found (DNS could not resolve the server name)'
        }
        
        $description = $errorDescriptions[$hexCode]
        if ($description) {
            return "$hexCode - $description"
        } else {
            return "$hexCode - Unknown Windows Update error"
        }
    }
}