在 Windows 7 上使用 powershell 2.0 下载文件

Pau*_*aul 4 download powershell-2.0

这是我下载文件的脚本,我替换了要下载的 url,因为它是一个私有云站点,我到处搜索,他们都说这是在旧版本的 PS 中下载的正确方法,但我一直收到错误

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request." At line:60 char:25 + $client.DownloadFile <<<< ($url, $targetFile) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

$url = "(download url)" 
$path = "C:\temp\jre-7u71-windows-i586.exe" 
# param([string]$url, [string]$path) 

if(!(Split-Path -parent $path) -or !(Test-Path -pathType Container (Split-Path -parent $path))) { 
  $targetFile = Join-Path $pwd (Split-Path -leaf $path) 
} 

"Downloading [$url]`nSaving at [$path]" 
$client = new-object System.Net.WebClient 
$client.DownloadFile($url, $targetFile) 
#$client.DownloadData($url, $targetFile) 

$path
Run Code Online (Sandbox Code Playgroud)

Sti*_*ler 6

我遇到了同样的问题,您声明 $client 变量的方式似乎存在问题。

旧帖子,但这在 Windows 7、PowerShell 2.0 中对我有用

请注意 URL 和 Path 已更改。

$url = "http://www.7-zip.org/a/7z1604.exe" 
$path = "C:\temp\7z1604.exe" 
# param([string]$url, [string]$path) 

if(!(Split-Path -parent $path) -or !(Test-Path -pathType Container (Split-Path -parent $path))) { 
$targetFile = Join-Path $pwd (Split-Path -leaf $path) 
} 

(New-Object Net.WebClient).DownloadFile($url, $path) 
$path
Run Code Online (Sandbox Code Playgroud)