0

I was wondering is it possible to make an FTPS powershell script to upload and download all files with the same file extension? I have looked and i always see ftp scripts.

I been using this script.

$sourceuri = ""
$targetpath = ""
$username = ""
$password = ""


$ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)


$ftprequest.Credentials =
    New-Object System.Net.NetworkCredential($username,$password)

$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $false
$ftprequest.KeepAlive = $false
$ftprequest.EnableSsl = $true


$ftpresponse = $ftprequest.GetResponse()

$responsestream = $ftpresponse.GetResponseStream()

$targetfile = New-Object IO.FileStream ($targetpath,[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024

do{
    $readlength = $responsestream.Read($readbuffer,0,1024)
    $targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)

$targetfile.close()

Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
At H:\-----.ps1:21 char:1
+ $ftpresponse = $ftprequest.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

You cannot call a method on a null-valued expression.
At H:\-------.ps1:24 char:1
+ $responsestream = $ftpresponse.GetResponseStream()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

These are some of the errors I got.

Wtower
  • 18,848
  • 11
  • 103
  • 80
Thun
  • 27
  • 1
  • 9
  • 1
    I tend to use WinSCP for this. Technically, you could use the WinSCP .Net component, but typically I just create a script file for WinSCP and call it from PowerShell. – Bacon Bits Apr 11 '16 at 20:17
  • So it is not possible to establish connection direct with powershell? – Thun Apr 13 '16 at 06:30
  • I'm sure it is (the FtpWebRequest class seems to support it, although it's not clear if it supports implicit FTPS) but it's a bit like inventing the universe in order to make a pie. – Bacon Bits Apr 13 '16 at 13:28

0 Answers0