Skip to content

Instantly share code, notes, and snippets.

@p0w3rsh3ll
Created January 5, 2016 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p0w3rsh3ll/14f8f4d622e6d5e2440f to your computer and use it in GitHub Desktop.
Save p0w3rsh3ll/14f8f4d622e6d5e2440f to your computer and use it in GitHub Desktop.
#Requires -Version 3.0
Function Find-WUErrorCode {
<#
.SYNOPSIS
Deciphers an error code using KB938205.
.DESCRIPTION
Translates an error code to hexadecimal and locates its meaning on https://support.microsoft.com/en-us/kb/938205
.PARAMETER InputObject
It can be an array of string,int32,int64,hex based error codes
.EXAMPLE
Find-WUErrorCode '0x80240017'
.EXAMPLE
0x80240017| Find-WUErrorCode
.EXAMPLE
-2145124329 | Find-WUErrorCode
.EXAMPLE
0xf0800,0xf081C,0x80240006,0x80244005,0x8024400D,0x8024CFFF,0xFFFFFFFF | Find-WUErrorCode
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory,ValueFromPipeline)]
[System.Object[]]$InputObjet
) Begin {
# Get the content of the KB page
try {
$URL = 'https://support.microsoft.com/api/content/kb/938205'
$pagecontent = (new-object Net.Webclient).DownloadString([system.Uri]$($URL))
} catch {
Write-Warning -Message "Failed to read web page content because $($_.Exception.Message)"
break
}
# Sort categories' titles and at what line they resides
$Titles = @()
$pagecontent -split "`n" -split '>' -split '<' | Select-String -Pattern "^H4\sclass=" -Context 0,1 | ForEach-Object {
$Titles += [PSCustomObject]@{
LineNumber = $_.LineNumber + 1
Line = ($_.Context | Select -Expand PostContext) -replace '</H4',''
}
}
}
Process {
$InputObjet | ForEach-Object {
$err = $TV = $Short = $Long = $hex = $int32 = $int64 = $Cat = $null ;
Write-Verbose "Dealing with $($_)"
# Convert input object based on its type
try {
Switch ($_) {
{$_ -is [string]} {
$hex = "{0:X0}" -f ([int32]$_)
$int64 = [Convert]::ToInt64($hex,16)
$int32 = $_
break
}
{$_ -is [int32]} {
$hex = "{0:X0}" -f ([int32]$_)
$int64 = [Convert]::ToInt64($hex,16)
$int32 = $_
break
}
{$_ -is [int64]} {
$hex = "{0:X0}" -f ([int64]$_)
$int64 = $_
$int32 = [Convert]::ToInt32($hex,16)
break
}
default {}
}
$err = [PSCustomObject]@{
Hexadecimal = "0x$hex"
Int32 = $int32
Int64 = $int64
}
} catch {
Write-Warning -Message "Failed to convert input because $($_.Exception.Message)"
}
if ($err.Hexadecimal -match '^0x[a-fA-F\d]{1,8}' ) {
# Get full line
$TV = ($pagecontent -split "`n" -split '>' -split '<') | Select-String -Pattern "^$($err.Hexadecimal)\s"
if ($TV) {
# Short and Long description
$null,$Short,$Long = ([regex]'^0x[a-fA-F\d]{1,8}\s(?<WUShortName>[A-Z_]+)\s(?<WUDesc>.*)').Matches($TV.Line) |
Select-Object -ExpandProperty Groups |
Select -Expand Value
# Category
$Cat = $Titles | Where LineNumber -lt $($TV.LineNumber) | Select -Last 1 -ExpandProperty Line
# Output
[PSCustomObject]@{
Hexadecimal = $err.Hexadecimal ;
Constant = $Short ;
Description = $Long -replace '&lt;','<' -replace '&gt;','>';
Category = $Cat ;
}
} else {
Write-Warning -Message "$($err.Hexadecimal) cannot be found on page https://support.microsoft.com/en-us/kb/938205"
}
} else {
Write-Warning -Message "Don't have a hex value for $_"
}
}
}
End {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment