3

If I use this, everything ok, the script got the data back what i want to see.

$Wcl = new-object System.Net.WebClient
$Wcl.Headers.Add(“xxxxx”, $xxxxx)
$Wcl.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$html = Invoke-WebRequest -Uri https://www.mcafee.com/enterprise/en-us/downloads/security-updates.html
$dathtml = ($html.parsedhtml.getelementsbytagname("TR") |% { ( $_.children | ?{ $_.tagName -eq "td"} | % innerText ) } | Select-Object -First 1).Split('xdat')[0] 

If I use this, the script got the data back but...

$Wcl = new-object System.Net.WebClient
$Wcl.Headers.Add(“xxxxx”, $xxxxxx)
$Wcl.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$html = Invoke-WebRequest -Uri https://www.mcafee.com/enterprise/en-us/downloads/security-updates.html -UseBasicParsing
$dathtml = ($html.parsedhtml.getelementsbytagname("TR") |% { ( $_.children | ?{ $_.tagName -eq "td"} | % innerText ) } | Select-Object -First 1).Split('xdat')[0] 

I get this error message:

You cannot call a method on a null-valued expression.
At line:9 char:1
+ $dathtml = ($html.parsedhtml.getelementsbytagname("TR") |% { ( $_.children | ?{  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Only added the -UseBasicParsing.

Thanks!

damHU
  • 51
  • 2
  • 2
  • 11
  • 2
    Just did some tests and it looks like the `-UseBasicParsing` isn't showing the ParsedHTML comobject in my tests. Check to make sure it is shown in your `$html` – Drew Jul 26 '18 at 10:16
  • `-UseBasicParsing` doesn't offer a `parsedhtml` Element. Either don't use `-UseBasicParsing` or try to filter the `Content` Element. – Paxz Jul 26 '18 at 10:40
  • I have to use -UseBasicParsing because If I won't I'm not able to connect to the site just with TrustedConnection and I got this message: https://i.stack.imgur.com/T0F2y.jpg – damHU Jul 26 '18 at 10:47
  • Or.. you know.. you could do what the error states and add the page to the trusted list. – Paxz Jul 26 '18 at 10:52
  • Already has been added, but the issue is the same. – damHU Jul 26 '18 at 10:54
  • 1
    Well then try to filter the content directly from `$html.content`. – Paxz Jul 26 '18 at 10:57
  • @D.Attila I think you're running into an [X Y Problem](https://meta.stackexchange.com/a/66378/394809) here, the real question you're looking for is how to get around the issue of not being able to specify `-UseBasicParsing` - if you don't parse the page you can't get the `ParsedHtml` - you may wish to look at something like HtmlAgilityPack if you need a more reliable method of parsing web data. – colsw Jul 26 '18 at 13:06
  • Thanks for the answers, they were very useful. I will try to find the best solutions for finish this. – damHU Jul 26 '18 at 13:27

1 Answers1

3

If you use -UseBasicParsing, then .parsedhtml is unavailable

Instead of :

$html = Invoke-WebRequest -Uri https://www.mcafee.com/enterprise/en-us/downloads/security-updates.html -UseBasicParsing
$dathtml = $html.parsedhtml.getelementsbytagname("TR") # produces error

Use:

$page = Invoke-WebRequest -Uri https://www.mcafee.com/enterprise/en-us/downloads/security-updates.html -UseBasicParsing

# Create HTML file Object
$HTML = New-Object -Com "HTMLFile"
# Write HTML content according to DOM Level2 
$HTML.IHTMLDocument2_write($page.Content)

$dathtml = $HTML.getElementsByTagName("TR")
Karol Zlot
  • 2,887
  • 2
  • 20
  • 37