简体   繁体   中英

“Exception calling ”DownloadFile“ with ”2“ argument(s): ”An exception occurred during a WebClient request."

I am trying to setup to download need files to automate and install process. I keep getting errors ever time I run the script and I have changed it seven ways from sunday and it still gives me errors.

The script is:

  if (test-path $java_path)

   {
   Write-Output "Java already installed. Skipping script"
   exit 0
    }

  else 

    {
      $source = "http://our.server.com/java-installer.zip"
      $destination = "c:\CHPACS"
      $client = new-object System.Net.WebClient
      $client.DownloadFile($source, $destination)

      }

The error message that I am getting is

 Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
 At C:\ps_script\testjava.ps1:41 char:31
 +           $client.DownloadFile <<<< ($source, $destination)
 + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
 + FullyQualifiedErrorId : DotNetMethodException

Do I need to create a function to make this work properly?

Thank you:

If you look at the MSDN documentation for the DownloadFile method, you'll notice that the second parameter is a filename , not a directory. So, if you re-define $destination to something like:

$destination = "c:\CHPACS\java-installer.zip"

then it should work.

Check secondly that the file that you're trying to download isn't open or being executed at the moment. This exception will be raised if the file is in use.

你应该像大卫所说的那样给出一个文件名+文件夹应该存在。

In my case the URL wasn't reachable on the machine I was trying to execute the script. Had to enable proxy rules to scope it to the machine to download the file.

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