PowerShell script to Query all Windows system error codes for descriptions, and dumps the results to CSV and JSON files, located in the script directory.
##
# Query all Windows system error codes for descriptions and dumps, and dumps the
# results to CSV and JSON files, located in this script's directory.
#
# original code from: https://github.com/PrateekKumarSingh/WinErrorCodeInfo/blob/master/Import-WindowsErrorCode.ps1
# article: https://geekeefy.wordpress.com/2016/06/14/powershell-import-query-all-windows-system-error-codes-for-description/
##
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$outputTextFilePath = Join-Path $scriptDir "WindowsErrorCodes.txt"
$outputCsvFilePath = Join-Path $scriptDir "WindowsErrorCodes.csv"
$outputJsonFilePath = Join-Path $scriptDir "WindowsErrorCodes.json"
function Export-Windows-Error-Codes
{
[cmdletbinding()]
param()
begin
{
$SystemErrorURLs = @(
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx",
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms681388(v=vs.85).aspx",
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms681383(v=vs.85).aspx",
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms681385(v=vs.85).aspx",
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms681386(v=vs.85).aspx",
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms681387(v=vs.85).aspx",
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms681389(v=vs.85).aspx",
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms681390(v=vs.85).aspx",
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms681391(v=vs.85).aspx",
"https://msdn.microsoft.com/en-us/library/windows/desktop/ms681384(v=vs.85).aspx"
)
$InternetErrorURL = "https://msdn.microsoft.com/en-us/library/windows/desktop/aa385465(v=vs.85).aspx"
$ExtractedString_SystemErrorURLs = $ExtractedString_InternetErrorURL = $Output = @()
}
process
{
$ExtractedString_SystemErrorURLs = Foreach($URL in $SystemErrorURLs)
{
$Content = ((Invoke-WebRequest -uri $URL).allelements|?{$_.id -eq "mainsection"}).outertext
(($Content -split "FORMAT_MESSAGE_FROM_SYSTEM flag.")[1] -split "Suggestions?")[0].trim()
}
$Content_interneterrors = ((Invoke-WebRequest -uri "https://msdn.microsoft.com/en-us/library/windows/desktop/aa385465(v=vs.85).aspx").allelements|?{$_.id -eq "mainsection"}).outerText
$ExtractedString_InternetErrorURL = (($Content_interneterrors -split "The following errors are specific to the WinINet functions.")[1] -split "ERROR_INVALID_HANDLE")[0].trim()
Write-Verbose "Creating temporary files and fetching the Content to parse"
# This is a single line String:
$ExtractedString_SystemErrorURLs + $ExtractedString_InternetErrorURL |Out-File "$outputTextFilePath"
# Saving it in the file and then fetching the data using get content converts it to array of strings separated by EOL:
$Result = (Get-Content "$outputTextFilePath").trim()
# Parse exit codes and description:
$Output = for($i =0 ; $i -le ($Result.count-1) ;)
{
$ErrorString = ($Result[$i]).ToString()
$i=$i+1
$Exitcode = ($Result[$i]).trim().split(" ")[0]
$hex = "0x$("{0:X0}" -f [int] $Exitcode)"
$j=$i=$i+1
While($Result[$i] -notlike "" -and $Result[$i] -notlike "ERROR_*" -and $Result[$i] -notlike "WAIT_*" -and $Result[$i] -notlike "ERROR_SETCOUNT_ON_BAD_LB*" -and $Result[$i] -notlike "RPC_*" -and $Result[$i] -notlike "EPT_*" -and $Result[$i] -notlike "SCHED_*" -and $Result[$i] -notlike "FRS_*" -and $Result[$i] -notlike "DNS_*" -and $Result[$i] -notlike "WSASERVICE_*" -and $Result[$i] -notlike "WSATYPE_*" -and $Result[$i] -notlike "WSA_*" -and $Result[$i] -notlike "WSATRY_*" -and $Result[$i] -notlike "WSAHOST_*" -and $Result[$i] -notlike "WSANO_*" -and $Result[$i] -notlike "WARNING_*" -and $Result[$i] -notlike "APPMODEL_*")
{
$i = $i+1
}
$str=''
$Description = $Result[$j..($i-1)] | %{$str="$str$_";$str}
If($Description -like $Result[$j-2])
{
$Description = $Result[$j]
$i=$i+1
}
'' | select @{n='error_string';e={$ErrorString}}, @{n='exit_code';e={[int]$Exitcode}}, @{n='hex_value';e={$Hex}}, @{n='description';e={$Description}}
}
Write-Verbose "All Exit code information has been successfully extracted."
Write-Verbose "Sorting Information according to ExitCode numbers..."
$Output = $Output| ?{$_.ErrorString -ne 'ERROR_INTERNET_*'} |sort ExitCode
# create csv formmated lookup:
$Output | Export-Csv -Path "$outputCsvFilePath" -NoTypeInformation -Delimiter "," -Encoding ASCII
# convert json formatted lookup:
$jsonOutput = ConvertTo-Json -InputObject $Output
Out-File -FilePath $outputJsonFilePath -InputObject $jsonOutput -Encoding ascii
}
end
{
$Output
}
}
Export-Windows-Error-Codes