Last active
February 8, 2023 17:03
-
-
Save SMSAgentSoftware/c4f036d5b00bef0bdace0106f248e9ad to your computer and use it in GitHub Desktop.
Pulls Windows Update error codes from the Microsoft Docs reference pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {$_ -match "error" -or $_ -match "success"} | |
$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 id=""0x" | |
$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