Solved

How to download files from the FTP server with PowerShell?

Posted on 2014-10-22
7
5,945 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
  • 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 68

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 68

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 68

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now