Solved

How to download files from the FTP server with PowerShell?

Posted on 2014-10-22
7
6,265 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

11 Experts available now in Live!

Get 1:1 Help Now