0

大家加油,

我遇到了最奇怪的问题,我需要你的帮助想法来解决这个问题。

所以,我有一个下载脚本,它使用 Webclient 对象从公司 Intranet 中提取内容。它需要凭据,并且可以在大约 80% 的计算机上运行。该脚本使用提取列表.DownloadString,然后使用.DownloadFile.

在无法工作的机器上,初始.DownloadString挂起,直到它出现超时并返回$null。用户凭据与这些类型的机器无关,这意味着在另一台机器上工作的用户在这台机器上失败。地址,如果输入浏览器返回内容。

用代码说话我是这样尝试的:

$wc = new-object System.Net.WebClient
$wc.Credentials = new-object System.Net.NetworkCredential($user, $pass, $domain)
$old_eap = $ErrorActionPreference
$ErrorActionPreference = "Stop"
try
    {
    $tmp = $wc.DownloadString($url)
    if ([String]::IsNullOrEmpty($tmp))
    {
        throw "Intranet server did not return directory listing"
    }
    Return $tmp #the code is actually part of a function...
    }
catch
    {
    write-error $_.Exception.Message
    Return $null
    }
finally
    {
    $ErrorActionPreference = $old_eap
    }

除了在不同机器之间寻找更改的设置外,我不知道。但是哪些设置可能与 Webclient 这样的行为相关?有任何想法吗?我严重卡住了...

我忘了...为了让事情变得更简单,我坚持使用 2.0 版,我们还不能更新。无赖...

提前感谢亚历克斯

4

1 回答 1

0

也许尝试使用 xmlhttp 作为客户端。下面是使用示例。

$url = "https://example.com/"
$http = New-Object -ComObject Msxml2.XMLHTTP
$user = "Domain\username"
$pwd = "password"

$utf = [System.Text.Encoding]::UTF8


$http.open("GET", $url, $false, $user, $pwd)
$http.send()
$result = $utf.GetString($http.responseBody)
于 2018-10-19T16:42:49.303 回答