Powershell ftp下载失败 [英] Powershell ftp download failed

查看:81
本文介绍了Powershell ftp下载失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从Powershell中的ftp下载文件时遇到问题,此脚本尝试建立连接,搜索一些文件(我理解了这一部分),然后将其下载到工作目录中,但没有问题知道为什么,请帮忙!

I'm having issues downloading files from an ftp in powershell, this script tries to setup the connection, search for some files (I got this part right) and then download it in the working directory, I got issues don't know why, please help!!

代码如下:

#IP address of DNS of the target % protocol
$protocol="ftp"
$target = "XXXX"
$connectionString = $protocol+"://"+$target
#Method to connect
$Request = [System.Net.WebRequest]::Create($connectionString) 
$Request.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails 


# Set Credentials "username",password
$username = "XXXXXXX"
$password = "XXXXXX"


# Read Username/password 
$Request.Credentials = New-Object System.Net.NetworkCredential $username,$password

$Response = $Request.GetResponse() 
$ResponseStream = $Response.GetResponseStream() 

# Select Pattern to search  
$pattern = "CCS"

# Set directory for download Files
$directory = [IO.Directory]::GetCurrentDirectory()


# Read and display the text in the file 
$Reader = new-object System.Io.StreamReader $Responsestream 
$files = ($Reader.ReadToEnd()) -split "`n" | Select-String "$pattern" | foreach { $_.ToString().split(" ")[28]}
$uri = (New-Object System.Uri($connectionString+"/"+$file))
$download = New-Object System.Net.WebRequestMethods+Ftp


foreach ($file in $files) {
    $destinationFile = $directory+"\"+$file  
    $sourceFile = $uri.OriginalString
    $download.DownloadFile($sourceFile, $destinationFile)
}


# Close Reader and Response objects 
$Reader.Close() 
$Response.Close()

当我运行它时,我得到以下输出:

When I run it I got this output:

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At C:\CRIF\BatchScripts\FTPCHECK\01.FTP_Check.ps1:44 char:5
+     $download.DownloadFile($sourceFile, $destinationFile)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException

我正在Powershell 3.0(Windows Server 2012)上运行它.请帮忙!

I'm running this on Powershell 3.0 (Windows Server 2012). Please help!

推荐答案

此通用异常的内部异常中隐藏了有关该问题的详细信息.您应该对错误进行更深入的了解,以找出真正的问题所在.

Details about the problem are hidden in the inner exception of this generic exception. You should dig a bit deeper in the error to find out what the real problem is.

由于PowerShell错误存储在$ error中,因此您可以在收到错误后立即尝试使用以下命令来检查上一个错误的内部异常

Since PowerShell errors are stores into $error you could, immediatly after getting the error, try the following command to check out the inner exception of the last error

$error[0].Exception.InnerException

要充分利用错误消息,您可以使用人们编写的函数,例如 Resolve-Error .

To get the most out of error messages you could use functions people wrote like Resolve-Error.

如果您希望脚本在这种情况下始终显示更好的错误消息,则可以使用try catch块来捕获错误并更好地显示错误.像这样:

If you want your script in this case to always display a better error message, you could use a try catch block to catch the error and display it better. Something like this:

try {
  $download.DownloadFile($sourceFile, $destinationFile)
}
catch [System.Net.WebException] {
  if ($_.Exception.InnerException) {
    Write-Error $_.Exception.InnerException.Message
  } else {
    Write-Error $_.Exception.Message
  }
}

这篇关于Powershell ftp下载失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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