首页 > 解决方案 > 使用 PowerShell 从 Internet 下载文件时出错

问题描述

我收到以下代码的授权错误。当我将下载位置更改为 C:\temp 时,我没有收到此错误。请参阅下面的代码和错误。

我需要将文件下载到 D 盘,因为它是共享文件夹

更改为 C 盘有效,但我需要将其下载到 D 盘

$File64 = "https://go.microsoft.com/fwlink/?LinkID=121721&arch=x64"
$Location64 = "D:\temp\mpam-fe64Bit.exe"
$File32 = "https://go.microsoft.com/fwlink/?LinkID=121721&arch=x86"
$Location32 = "D:\temp\mpam-fe32Bit.exe"

$Client = New-Object System.Net.WebClient
$Client.DownloadFile($File64, $Location64)  

$Client.DownloadFile($File32, $Location32)

我收到的错误如下

使用“2”参数调用“DownloadFile”的异常:“远程服务器返回错误:(401)未经授权。” 在 C:\PSScripts\DownloadDefs.ps1:9 char:1 + $Client.DownloadFile($File32, $Location32) + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~

  • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
  • 完全限定错误 ID:WebException

我尝试添加以下代码,但仍然无法正常工作

$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential($user,$pwd)
$credCache.Add($source, "Basic", $creds)
$wc.Credentials = $credCache

任何帮助将不胜感激

标签: powershell

解决方案


推荐阅读