简体   繁体   中英

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

This is my first ever question on stack exchange, so I ask forbearance in the face of breaking community etiquette. If I can clarify any part of my question, I would be more than happy to do so for you.

I'm learning powerShell for my internship, and currently I'm working on a script that pulls a .csv file from a sharepoint site. My aim is to take the file downloaded, do stuff , and then put it back on the site. Before I can do the other things, I'm trying to download the file, and my root issue lies in .net.webclient's .downloadfile() method. At execution I keep seeing a vague exception that I can't find information for anywhere on the web or among my colleagues. Below I will include the snippet involved, and the error message. Below that I will give you an Idea of troubleshooting steps I've taken.

--------code

  ## 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

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

--------action taken The credentials I provide should be valid because I am able to download the contents of $URIaddress when I enter the URI into my browser.

I've been able to coax .downloadFileAsync(string, string) into creating a file at the specified $downloadPath, but the download never seems to commence, and the file stays empty. When try to .DownloadFile() into a random variable ($foo), $foo | gm reveals to be null.

If any of you have ideas I'd love to give them a shot!

Indeed quite cryptic.

Have a look at the exception and the inner exception these might provide you with more insights on what happened: $Exception = $error[0].Exception $Exception.InnerException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM