Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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:

like image 552
user1846439 Avatar asked Dec 04 '12 21:12

user1846439


3 Answers

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.

like image 51
David Avatar answered Oct 15 '22 18:10

David


You should give a file name as David said + the folder should exist.

like image 39
CH81 Avatar answered Oct 15 '22 17:10

CH81


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.

like image 2
Fairoz Avatar answered Oct 15 '22 17:10

Fairoz