4

I have looked everywhere for an answer to this, but I'm thinking maybe my Google ninja skills are lacking.

I am trying to run a simple command in Powershell that downloads a string (in reality, I want to download an msi and run it - but I've narrowed down the problem to a simple example). The script I am running is:

$client = New-Object System.Net.WebClient
$client.DownloadString("http://google.com") | Out-File google.html

The error I get is:

Exception calling "DownloadString" with "1" argument(s): "Unable to connect to the remote server"
At line:1 char:1
+ $client.DownloadString("http://google.com") | Out-File google.html
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

PSVersion = 4.0

I have ran this script on a colleague's PC (PSVersion = 3.0) and it works perfectly. So I know the code works. I have also re-created this code in a C# Console Application, which looks like:

static void Main(string[] args)
{
    using(var client = new System.Net.WebClient())
    {
        var content = client.DownloadString("http://google.com");
        File.WriteAllText("D:\\google.html", content);
    }
}

And that works on my machine - so I know it's not the .NET Framework that's the problem. And weirdly, I can make a call to that exe from Powershell and it works.

I have narrowed it down to being a problem with Powershell, but I cannot for the life of me work out what. I have obviously done something to my machine in order break Powershell's connection to the internet, but could do with someone who knows more than me about what happens behind the scenes.

Please note This has nothing to do with PS Remoting. I don't think, but I'm not trying to use PS Remoting. I don't think...

4
  • Is it possible that a personal firewall (one that limits access per process) is interfering with network communications?
    – PeterK
    Apr 29, 2015 at 12:06
  • What about Invoke-WebRequest -Uri 'http://www.google.com/' -OutFile 'google.html'?
    – Bacon Bits
    Apr 29, 2015 at 12:10
  • @RafałSaltarski If you add that comment as an answer, I'll mark it as answered. Great shout. Although, what would be causing that? AFAIK, my user is the admin user, so when I run Powershell as Administrator, shouldn't that just work?
    – Simon
    Apr 29, 2015 at 12:35
  • @BaconBits Out of curiosity I tried your solution, but alas it threw the same error.
    – Simon
    Apr 29, 2015 at 13:18

1 Answer 1

7

Are you running PowerShell as a different account (for example an admin)? Maybe that account's proxy settings are different that your normal account's? Maybe try setting $client.Proxy = $null before downloading and see if it helps.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.