Private/MSCatalog/Invoke-CatalogRequest.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
function Invoke-CatalogRequest { [CmdletBinding()] param ( [parameter(Mandatory = $true)] [string] $Uri, [Parameter(Mandatory = $false)] [string] $Method = "Get", [Parameter(Mandatory = $false)] [string] $EventArgument, [Parameter(Mandatory = $false)] [string] $EventTarget, [Parameter(Mandatory = $false)] [string] $EventValidation, [Parameter(Mandatory = $false)] [string] $ViewState, [Parameter(Mandatory = $false)] [string] $ViewStateGenerator ) try { if ($Method -eq "Post") { $ReqBody = @{ "__EVENTARGUMENT" = $EventArgument "__EVENTTARGET" = $EventTarget "__EVENTVALIDATION" = $EventValidation "__VIEWSTATE" = $ViewState "__VIEWSTATEGENERATOR" = $ViewStateGenerator } } if ($Uri -match '%26') { $Params = @{ Uri = $Uri Method = $Method Body = $ReqBody ContentType = "application/x-www-form-urlencoded" UseBasicParsing = $true ErrorAction = "Stop" } } else { $Params = @{ Uri = [Uri]::EscapeUriString($Uri) Method = $Method Body = $ReqBody ContentType = "application/x-www-form-urlencoded" UseBasicParsing = $true ErrorAction = "Stop" } } $Results = Invoke-WebRequest @Params $HtmlDoc = [HtmlAgilityPack.HtmlDocument]::new() $HtmlDoc.LoadHtml($Results.RawContent.ToString()) $NoResults = $HtmlDoc.GetElementbyId("ctl00_catalogBody_noResultText") if ($null -eq $NoResults) { $ErrorText = $HtmlDoc.GetElementbyId("errorPageDisplayedError") if ($ErrorText) { throw "The catalog.update.microsoft.com site has encountered an error. Please try again later." } else { [MsUpCatResponse]::new($HtmlDoc) } } else { throw "$($NoResults.InnerText)$($Uri.Split("q=")[-1])" } } catch { throw $_ } } |