Solved

How to download files from the FTP server with PowerShell?

Posted on 2014-10-22
7
7,940 Views
Last Modified: 2015-01-12
I was wondering if someone could help me to create PowerShell script please.

Basically, I need to create scheduled task (once a day at 1am) to run PowerShell script which will do the following:

1.      Connect to folder on FTP server (user+password)
2.      Download files from the FTP folder onto computer running PowerShell script
3.      Delete files from FTP folder only when download was successful
4.      On a computer where downloaded files are stored, keep only two latest copies and the old one needs to be deleted
5.      (Optional) Create a basic report (success/fail) and send it as email

Size of the downloaded file (*.zip) can be around 500MB zip file. I’m guessing that script should have some kind of logic to wait until file was fully downloaded.

Thanks in advance
0
Comment
Question by:Olevo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 1

Author Comment

by:Olevo
ID: 40400801
Thanks Wilder, where in a script I need to put ftp account and password?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40401961
Do you know the exact name of the file on the FTP site, or is it changing?
Is it only one file?
If not, what is the filename mask to apply (or do you need just all files found)?

I assume the file name is changing, like having a date and time portion, else your other request to keep the two most recent copies (including the current one?) only.

https://social.technet.microsoft.com/Forums/security/en-US/ff18a705-eeee-4ba7-bd3e-2fcc9fd5cbee/using-powershell-to-download-from-ftp-site-file-name-has-wildcard
defines a function Get-FtpDir (that is what Wilder_Admin posted), which gets the listing of a FTP site. I've modified the script slightly.
Combining that with what I think you are after:
# Here are the variables to change to your needs:
$ftpPath = 'ftp://ftp.site.com/folder/'
$ftpUser = 'user'
$ftpPass = 'pass'
$localPath = 'C:\Temp\EE\'
# end of changes

# get the file listing of the FTP folder
function Get-FtpDir ($url, $credentials)
{
  $request = [Net.FtpWebRequest]::Create($url)
  if ($credentials) { $request.Credentials = $credentials }
  $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
  $response = $request.GetResponse()
  $reader   = New-Object IO.StreamReader $response.GetResponseStream() 
  $reader.ReadToEnd()
  $reader.Close()
  $response.Close()
}

# delete a FTP file
function Remove-FtpFile ($url, $credentials)
{
  $request = [System.Net.FtpWebRequest]::create($url)
  if ($credentials) { $ftprequest.Credentials =  $credentials }
  $request.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
  $request.GetResponse()
  $request.Close()
}

# Needed for the download
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser,$ftpPass)  
$webclient.BaseAddress = $ftpPath

Get-FTPDir $ftpPath $webclient.Credentials |
  ? { $_ -like '*.zip' } |
  % {
	  $webClient.DownloadFile($_, $localPath)
    # DownloadFile does not provide a success state, so we have to check for the file name
    if (Test-Path $localPath+$_) { Remove-FtpFile $ftpPath+$_ $webclient.Credentials }
  }
  
# Now remove oldest files but three
Get-ChildItem $localPath -filter *.zip |
  sort CreationTime -Descending |
  select -Skip 3 |
  Remove-Item

Open in new window

0
 
LVL 1

Author Comment

by:Olevo
ID: 40412475
Thanks Qlemo for your help. Few *.zip files (system backup) on the FTP folder. When I run your script (I don’t see any errors) nothing has been downloaded locally onto my workstation?!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Qlemo
ID: 40413120
Yes, Get-FTPDir does not work (retrieves no list of files). I'll have to check out why - no clue yet, everything looks correct ...
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40414748
Hmm. You can't trust even on code found on MS forums anymore ...
# Here are the variables to change to your needs:
$ftpPath = 'ftp://ftp.site.com/folder/'
$ftpUser = 'user'
$ftpPass = 'pass'
$localPath = 'C:\Temp\EE\'
# end of changes


# get the file listing of the FTP folder
function Get-FtpDir ($url, $credentials)
{
  $request = [Net.FtpWebRequest]::Create($url)
  if ($credentials) { $request.Credentials = $credentials }
  $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
  (New-Object IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd() -split "`r`n"
}

# delete a FTP file
function Remove-FtpFile ($url, $credentials)
{
  $request = [System.Net.FtpWebRequest]::create($url)
  if ($credentials) { $request.Credentials =  $credentials }
  $request.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
  [Void] $request.GetResponse()
}

# Needed for the download
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser,$ftpPass)  
$webclient.BaseAddress = $ftpPath

Get-FTPDir $ftpPath $webclient.Credentials |
  ? { $_ -like '*.zip' } |
  % {
	  $webClient.DownloadFile($_, $localPath+$_)
    # DownloadFile does not provide a success state, so we have to check for the file name
    if (Test-Path ($localPath+$_)) { Remove-FtpFile ($ftpPath+$_) $webclient.Credentials }
  }
  
# Now remove oldest files but three
Get-ChildItem $localPath -filter *.zip |
  sort CreationTime -Descending |
  select -Skip 3 |
  Remove-Item

Open in new window

0
 
LVL 1

Author Comment

by:Olevo
ID: 40427679
Thanks Qlemo, I'll test it let you know...
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question