首页 文章

powershell System.Net.WebClient.DownloadFile(String,String)会引发神秘错误

提问于
浏览
0

这是我关于堆栈交换的第一个问题,所以我在面对破坏社区礼仪时要求忍耐 . 如果我能澄清我问题的任何部分,我会非常乐意为你这样做 .

我'm learning powerShell for my internship, and currently I'正在编写一个从sharepoint站点提取.csv文件的脚本 . 我的目标是下载文件,做东西,然后把它放回网站上 . 在我可以做其他事情之前,我是'm trying to download the file, and my root issue lies in .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 显示为null .

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

1 回答

  • 0

    确实很神秘 .

    看一下异常和内部异常,它们可以为您提供有关发生的事情的更多见解:$ Exception = $ error [0] .Exception $ Exception.InnerException

相关问题