Powershell: Net.Webclient - not getting reply from intranet depending on machine

836 Views Asked by At

Cheers everyone,

I am getting the weirdest problem for which I need your helping ideas how to approach the issue.

So, I have a download script that pulls content off a company intranet using Webclient objects. It requires credentials and it is working on about 80% of the computers. The script pulls a listing using .DownloadString and then parses and gets some files using .DownloadFile.

On the machines that won't work the initial .DownloadString hangs until it appears to run into a timeout and returns $null. User credentials are irrelevant on these types of machines meaning a user that works on another machine fails on this one. Addresses, if entered into browser returns content.

Spoken in code I try it this way:

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

I have no idea other than looking for changed settings between different machines. But which settings could be relevant for Webclient behaving like this? Any Ideas? I am seriously stuck...

I forgot... To make things a little easier I am stuck with Version 2.0 and we cant update yet. Bummer...

Thanks in advance Alex

1

There are 1 best solutions below

0
Mike Twc On

Maybe try to use xmlhttp as a client. Below is the usage example.

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