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
Solved

How to download files from the FTP server with PowerShell?

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).

856 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