Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pulls Windows Update error codes from the Microsoft Docs reference pages
Function Get-WUErrorCode {
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,ParameterSetName='Parameter Set 1',ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[string[]]
$ErrorCode,
[Parameter(Mandatory=$true,ParameterSetName='Parameter Set 2',Position=1)]
[switch]
$AsTable
)
Begin
{
# create a table
$ErrorCodeTable = [System.Data.DataTable]::new()
$ErrorCodeTable.Columns.AddRange(@("ErrorCode","Description","Message","Category"))
################################
## PROCESS THE FIRST WEB PAGE ##
################################
# scrape the web page
$ProgressPreference = 'SilentlyContinue'
$URL = "https://docs.microsoft.com/en-us/windows/deployment/update/windows-update-error-reference"
$tempFile = [System.IO.Path]::GetTempFileName()
Invoke-WebRequest -URI $URL -OutFile $tempFile -UseBasicParsing
$htmlarray = Get-Content $tempFile -ReadCount 0
[System.IO.File]::Delete($tempFile)
# get the headers and data cells
$headers = $htmlarray | Select-String -SimpleMatch "<h2 " | Where {$_ -notmatch "Feedback"}
$dataCells = $htmlarray | Select-String -SimpleMatch "<td>"
# process each header
$i = 1
do {
foreach ($header in $headers)
{
$lineNumber = $header.LineNumber
$nextHeader = $headers[$i]
If ($null -ne $nextHeader)
{
$nextHeaderLineNumber = $nextHeader.LineNumber
$cells = $dataCells | Where {$_.LineNumber -gt $lineNumber -and $_.LineNumber -lt $nextHeaderLineNumber}
}
else
{
$cells = $dataCells | Where {$_.LineNumber -gt $lineNumber}
}
# process each cell
$totalCells = $cells.Count
$t = 0
do {
$Row = $ErrorCodeTable.NewRow()
"ErrorCode","Message","Description" | foreach {
$Row["$_"] = "$($cells[$t].ToString().Replace('<code>','').Replace('</code>','').Split('>').Split('<')[2])"
$t ++
}
$Row["Category"] = "$($header.ToString().Split('>').Split('<')[2])"
[void]$ErrorCodeTable.Rows.Add($Row)
}
until ($t -ge ($totalCells -1))
$i ++
}
}
until ($i -ge $headers.count)
#################################
## PROCESS THE SECOND WEB PAGE ##
#################################
# scrape the web page
$URL = "https://docs.microsoft.com/en-us/windows/deployment/update/windows-update-errors"
$tempFile = [System.IO.Path]::GetTempFileName()
Invoke-WebRequest -URI $URL -OutFile $tempFile -UseBasicParsing
$htmlarray = Get-Content $tempFile -ReadCount 0
[System.IO.File]::Delete($tempFile)
# get the headers and data cells
$headers = $htmlarray | Select-String -SimpleMatch "<h2 " | Where {$_ -notmatch "Feedback"}
$dataCells = $htmlarray | Select-String -SimpleMatch "<td>"
# process each header
$i = 1
do {
foreach ($header in $headers)
{
$lineNumber = $header.LineNumber
$nextHeader = $headers[$i]
If ($null -ne $nextHeader)
{
$nextHeaderLineNumber = $nextHeader.LineNumber
$cells = $dataCells | Where {$_.LineNumber -gt $lineNumber -and $_.LineNumber -lt $nextHeaderLineNumber}
}
else
{
$cells = $dataCells | Where {$_.LineNumber -gt $lineNumber}
}
# process each cell
$totalCells = $cells.Count
$t = 0
do {
$WebErrorCode = $header.ToString().Split('>').Split('<')[2].Replace('or ','').Replace(' ',' ').Split()
If ($WebErrorCode.GetType().BaseType.Name -eq "Array")
{
foreach ($Code in $WebErrorCode)
{
$Row = $ErrorCodeTable.NewRow()
$Row["ErrorCode"] = $Code.Trim()
"Message","Description" | foreach {
$Row["$_"] = "$($cells[$t].ToString().Split('>').Split('<')[2])"
$t ++
}
$Row["Category"] = "Common"
[void]$ErrorCodeTable.Rows.Add($Row)
1..2 | foreach {$t --}
}
1..2 | foreach {$t ++}
}
else {
$Row = $ErrorCodeTable.NewRow()
$Row["ErrorCode"] = $ErrorCode
"Message","Description" | foreach {
$Row["$_"] = "$($cells[$t].ToString().Split('>').Split('<')[2])"
$t ++
}
$Row["Category"] = "Common"
[void]$ErrorCodeTable.Rows.Add($Row)
}
$t ++
}
until ($t -ge ($totalCells -1))
$i ++
}
}
until ($i -ge $headers.count)
$outputArray = @()
}
Process
{
if ($ErrorCode)
{
$ErrorCode | foreach {
$outputArray += ($ErrorCodeTable.Select("ErrorCode='$_'") | Select -First 1)
}
}
}
End
{
if ($ErrorCode)
{
return $outputArray
}
if ($AsTable)
{
return $ErrorCodeTable
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment