Powershell根本无法连接到互联网 [英] 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.
我试图在 Powershell 中运行一个简单的命令来下载一个字符串(实际上,我想下载一个 msi 并运行它 - 但我已经将问题缩小到一个简单的例子).我正在运行的脚本是:
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
我得到的错误是:
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
我在同事的 PC (PSVersion = 3.0) 上运行了此脚本,并且运行良好.所以我知道代码有效.我还在 C# 控制台应用程序中重新创建了此代码,如下所示:
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);
}
}
这适用于我的机器 - 所以我知道这不是 .NET Framework 的问题.奇怪的是,我可以从 Powershell 调用那个 exe
并且它可以工作.
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.
我已将其缩小为 Powershell 的问题,但我终生无法弄清楚是什么.我显然对我的机器做了一些事情,以断开 Powershell 与互联网的连接,但可以与比我更了解幕后发生的事情的人一起做.
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.
请注意这与 PS Remoting 无关.我不这么认为,但我不想使用 PS Remoting.我不认为...
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...
推荐答案
您是否以不同的帐户(例如管理员)运行 PowerShell?也许该帐户的代理设置与您的普通帐户不同?也许在下载之前尝试设置 $client.Proxy = $null 看看是否有帮助.
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.
这篇关于Powershell根本无法连接到互联网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!