Powershell ftp download failed

大憨熊 提交于 2021-02-11 16:51:31

问题


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

Here's the code:

#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

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


回答1:


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.

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

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

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


来源:https://stackoverflow.com/questions/22888414/powershell-ftp-download-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!