0

The vendor I'm working with uploads zip files to an FTP. I need to download whatever is uploaded there and process, as needed.

Using Powershell, how do I download *.* from an FTP folder?

(In reference to https://social.technet.microsoft.com/Forums/office/en-US/744ee28a-9340-446a-b698-4b96e081b501/download-files-from-ftp-server?forum=winserverpowershell)

# Config
$Username = "user"
$Password = "password"
$LocalFile = "C:\tools\file.zip"
$RemoteFile = "ftp://myftpserver:22/Folder1/Folder/file.csv"

# Create a FTPWebRequest 
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile) 
$FTPRequest.Credentials = New-Object     System.Net.NetworkCredential($Username,$Password) 
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
$FTPRequest.UseBinary = $true 
$FTPRequest.KeepAlive = $false
$ftpRequest.EnableSsl = $true
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse() 
# Get a download stream from the server response 
$ResponseStream = $FTPResponse.GetResponseStream() 
# Create the target file on the local system and the download buffer 
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create) 
[byte[]]$ReadBuffer = New-Object byte[] 1024 
# Loop through the download 
    do { 
        $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024) 
        $LocalFileFile.Write($ReadBuffer,0,$ReadLength) 
    } 
    while ($ReadLength -ne 0)

Is there any way to make $RemoteFile something like ftp://myftpserver:22/Folder1/Folder/*.zip or ftp://myftpserver:22/Folder1/Folder/*.*

My apologies if there is a post the same. I saw some similar, but not close enough to answer the question.

Kenster
  • 23,465
  • 21
  • 80
  • 106
Phoenix14830
  • 360
  • 1
  • 8
  • 24

1 Answers1

0

I have created PowerShell SFTP, FTP, and FTPS script. You will have to download it from my github. I do not use any third party applications, becuase I do not trust them. What I use is REBEX.dll for the SFTP section and .Net WebRequest for FTP and FTPS.

https://github.com/FallenHoot/ZOTECH-PS/blob/master/SFTP.zip

If you have issues understanding the code please let me know. If you are not going to use the Get-SFTP function just comment it out.

Zach Olinske
  • 517
  • 2
  • 14