使用代理PowerShell调用Web请求

时间:2018-07-25 10:01:27

标签: html powershell proxy

有一个脚本:

$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] 
Write-Host $dathtml

问题如下:

Invoke-WebRequest : ERROR
Cache Access Denied.
The following error was encountered while trying to retrieve the URL: https://www.mcafee.com/*
Cache Access Denied.
Sorry, you are not currently allowed to request https://www.mcafee.com/* from this cache until you have authenticated yourself.
Please contact the cache administrator if you have difficulties authenticating yourself.
Generated Wed, 25 Jul 2018 09:49:32 GMT by xxxxx (xxxx)
At line:1 char:9
+ $html = Invoke-WebRequest -Uri https://www.mcafee.com/enterprise/en-u ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
You cannot call a method on a null-valued expression.
At line:2 char:1
+ $dathtml = ($html.parsedhtml.getelementsbytagname("TR") |% { ( $_.chi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

是否可以将代理服务器身份验证方法集成到脚本(AD auth)中。如果我在计算机上运行此脚本(没有代理,在公共网络等上),它将起作用。但是我必须从另一台使用代理的计算机上运行此脚本,该站点被允许但无法获取我所需要的信息。有什么建议吗? 谢谢

2 个答案:

答案 0 :(得分:1)

Invoke-WebRequest有一个-ProxyCredential参数。

在那里使用凭据对象。 (用户名的格式应为“域名\名称”)

您可以这样做:

$cred = Get-Credential -Message 'Please enter your credentials for the proxy server.'
if ($cred) { 
    $url  = "https://www.mcafee.com/enterprise/en-us/downloads/security-updates.html"
    $html = Invoke-WebRequest -Uri $url -ProxyCredential $cred
    # the rest of your code goes here
}

我无法为您提供所需的实际McAfee命令。为此,您应该搜索McAfee社区,这取决于您要更新的产品。

答案 1 :(得分:0)

您还可以将身份验证标头添加到Web请求中,如here

所述
相关问题