在Powershell脚本中使用$ client.DownloadFile时出错 [英] Error using $client.DownloadFile in Powershell script

查看:3301
本文介绍了在Powershell脚本中使用$ client.DownloadFile时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我试图设置下载需要的文件,以自动化和安装过程。我一直在收到错误,我一直在运行脚本,我从星期天更改了七种方式,但仍然给我错误。

 脚本是

if(test-path $ java_path)

{
写入 - 输出Java已经安装。跳过脚本
exit 0
}

else

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

}

我收到的错误消息是

 使用2参数调用DownloadFile的异常:在WebClient请求期间发生异常。 
在C:\ps_script\testjava.ps1:41 char:31
+ $ client.DownloadFile<<<<< ($ source,$ destination)
+ CategoryInfo:NotSpecified:(:) [],ParentContainsErrorRecordException
+ FullyQualifiedErrorId:DotNetMethodException

我需要创建一个函数来使其正常工作吗?



谢谢:

解决方案

如果您查看 DownloadFile 方法的MSDN文档,您会注意到第二个参数是一个文件名,而不是一个目录。因此,如果您将 $ destination 重新定义为:

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

那么它应该工作。



其次,您尝试下载的文件目前尚未打开或正在执行。如果文件正在使用,将会引发此异常。


I have a script that 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.

这篇关于在Powershell脚本中使用$ client.DownloadFile时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆