Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Last active October 24, 2020 07:48
Show Gist options
  • Save PrateekKumarSingh/9052560994ece4de1f869f0c64745a0a to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/9052560994ece4de1f869f0c64745a0a to your computer and use it in GitHub Desktop.
Function Get-WindowsErrorcode
{
[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
{
Try
{
$ExtractedString_SystemErrorURLs = Foreach($URL in $SystemErrorURLs)
{
Write-Verbose "Web query and data extraction in progress on URL - $URL"
$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 aa single line String
$ExtractedString_SystemErrorURLs + $ExtractedString_InternetErrorURL |Out-File C:\Temp\TempData.txt
#Saving it in the file and then fetching the data using get content converts it to array of strings separated EOL
$Result = (Get-Content C:\Temp\TempData.txt).trim()
#Parsing the 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 "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='ErrorString';e={$ErrorString}}, @{n='ExitCode';e={$Exitcode}}, @{n='Hex Value';e={$Hex}}, @{n='Description';e={$Description}}
}
$Output = $Output| ?{$_.ErrorString -ne 'ERROR_INTERNET_*'} |sort ExitCode -Descending
}
Catch
{
"Something went wrong please try again"
}
Finally
{
Remove-Item C:\Temp\TempData.txt -Force
}
}
End
{
Write-Verbose "All Exit code information has been extracted successfully"
$Output
}
}
Get-WindowsErrorcode -OutVariable out -Verbose
#$out|Out-GridView |ConvertTo-Json| clip
#
#(iwr -Uri "https://gist.githubusercontent.com/PrateekKumarSingh/ce9a1dda082d52a9c6e1378cb9da0d08/raw/c73ff89d5002da1ebf6f6ba11728252b337dc9ed/MSI_ExitCodes.json").content | ConvertFrom-Json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment