powershell System.Net.WebClient.DownloadFile(String, String) 抛出神秘错误

powershell System.Net.WebClient.DownloadFile(String, String) throws cryptic errors

这是我第一个关于堆栈交换的问题,所以面对违反社区礼仪的问题,我请求宽容。如果我可以澄清我的问题的任何部分,我将非常乐意为您这样做。

我正在为实习学习 powerShell,目前我正在编写一个从共享点站点提取 .csv 文件的脚本。我的目标是获取下载的文件,做一些事情,然后将其放回网站上。在我可以做其他事情之前,我尝试下载文件,而我的根本问题在于 .net.webclient 的 .downloadfile() 方法。在执行过程中,我一直看到一个模糊的异常,即我无法在网络上或同事中的任何地方找到信息。下面我将包括所涉及的代码段和错误消息。下面我将向您介绍我已采取的故障排除步骤。

--------代码

  ## Download summary file from Sharepoint downloads new serverlist
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls
    $webclient = New-Object -TypeName System.Net.WebClient
    $webclient.Credentials = $spCreds ## defined earlier with import-clixml - also tried get-credential 
    $webclient.DownloadFile($URIaddress, $DownloadPath) ## defined earlier: $DownloadPath = "c:\temp\Summary.csv"

--------异常

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

--------采取的行动 我提供的凭据应该是有效的,因为当我在浏览器中输入 URI 时,我能够下载 $URIaddress 的内容。

我已经能够哄骗 .downloadFileAsync(string, string) 在指定的 $downloadPath 处创建一个文件,但下载似乎从未开始,并且文件保持为空。当尝试将 .DownloadFile() 转换为随机变量 ($foo) 时,$foo | gm 显示为空。

如果你们有任何想法,我很乐意给他们一个机会!

确实很玄乎

查看异常和内部异常,它们可能会让您更深入地了解所发生的事情: $Exception = $error[0].Exception $Exception.InnerException