如果使用此命令,一切正常,脚本将数据恢复为我想查看的内容。

$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]

如果我使用此脚本,脚本将返回数据,但是...
$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]

我收到此错误消息:
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

仅添加了-UseBasicParsing。

谢谢!

最佳答案

如果使用-UseBasicParsing,则.parsedhtml不可用

代替 :

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

使用:
$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")

10-07 14:18