Solved

How to download files from the FTP server with PowerShell?

Posted on 2014-10-22
7
7,364 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 69

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
Technology Partners: 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 69

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 69

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

696 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