首页 > 解决方案 > 如何用powershell下载文件?

问题描述

过去我确实使用过:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$url = "https://nodejs.org/dist/v14.17.6/node-v14.17.6-x64.msi"
$folder = "C:\Users\test\Downloads"

$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($url, $folder)

当我今天尝试时,它显示此错误

  Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
  At line:1 char:5
  +     $WebClient.DownloadFile($url, $folder)
  +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : WebException

我试图用找到的建议来修复,但没有一个奏效。

标签: powershell

解决方案


您需要提取文件名,以便获得 outfile 的完整路径,但这将起作用:

$url = "https://nodejs.org/dist/v14.17.6/node-v14.17.6-x64.msi"
$fullPath = "C:\Users\test\Downloads\node-v14.17.6-x64.msi"
Invoke-WebRequest $url -outfile $fullPath

推荐阅读