Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell unable to connect to internet at all

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...

like image 319
simonlchilds Avatar asked Mar 17 '23 06:03

simonlchilds


1 Answers

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.

like image 128
Rafał Saltarski Avatar answered Apr 01 '23 15:04

Rafał Saltarski