powershell - 使用 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
任何帮助将不胜感激
解决方案
推荐阅读
- c++ - 初始化时无法将“日期”转换为“int”作为错误
- pandas - 如何合并一行的两列,就像 Pandas 中的“在 excel 中合并和居中”一样?
- scala - Spark 操作员停止使用 Kubernetes
- python - 如何匹配一行中第一个字符后的字符串
- javascript - phonegap 应用程序的后端远程服务器/数据库
- python - 选择性地抑制与部署无关的 VSCode 警告/错误
- javascript - 如何将数组拆分为 m 和 n 大小的不同重复块
- ffmpeg - 每 x 秒移动覆盖位置
- google-app-engine - Google Cloud Storage 的速度是否足以将图像提供给移动应用程序?
- node.js - 从 JSON API 获取 JSON 对象?