我到处寻找答案,但我想也许我的谷歌忍者技能缺乏.
我试图在Powershell中运行一个简单的命令,下载一个字符串(实际上,我想下载一个msi并运行它 - 但我已经将问题缩小到一个简单的例子).我正在运行的脚本是:
$client = New-Object System.Net.WebClient
$client.DownloadString("http://google.com") | Out-File google.html
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
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
Run Code Online (Sandbox Code Playgroud)
PSVersion = 4.0
我在同事的PC上运行了这个脚本(PSVersion = 3.0),它运行得很好.所以我知道代码有效.我还在C#控制台应用程序中重新创建了此代码,如下所示:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于我的机器 - 所以我知道这不是.NET Framework的问题.奇怪的是,我可以exe
从Powershell …