Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-02-2009
Member
 
Join Date: May 2008
Posts: 71
Powershell: Best way to automate secure FTP?

Hello,

I want to know what is the best way to automate the secure FTP download for a database backup file? In powershell or even in .net framework?
Reply With Quote
  #2  
Old 19-02-2009
Member
 
Join Date: Jun 2008
Posts: 97
Re: Powershell: Best way to automate secure FTP?

I have read this blog where i saw some cool tips for the question you are asking!

http://scriptolog.blogspot.com/2007/...utomation.html

Hope this helps you!
Reply With Quote
  #3  
Old 19-02-2009
Member
 
Join Date: Jun 2008
Posts: 144
Re: Powershell: Best way to automate secure FTP?

Using Windows PowerShell Server and NetCmdlets, System Administrators can easily script common network management tasks including router configuration, switch port monitoring, directory access, VLAN administration, remote execution, etc.
Reply With Quote
  #4  
Old 19-02-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: Powershell: Best way to automate secure FTP?

After some experimentation I came up with this way to automate a secure FTP download in PowerShell. This script runs off the public test FTP server administered by Chilkat Software. So you can copy and paste this code and it will run without modification.

Code:
$sourceuri = "ftp://ftp.secureftp-test.com/hamlet.zip"
$targetpath = "C:\hamlet.zip"
$username = "test"
$password = "test"

# Create a FTPWebRequest object to handle the connection to the ftp server
$ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)

# set the request's network credentials for an authenticated connection
$ftprequest.Credentials =
    New-Object System.Net.NetworkCredential($username,$password)

$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $true
$ftprequest.KeepAlive = $false

# send the ftp request to the server
$ftpresponse = $ftprequest.GetResponse()

# get a download stream from the server response
$responsestream = $ftpresponse.GetResponseStream()

# create the target file on the local system and the download buffer
$targetfile = New-Object IO.FileStream ($targetpath,[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024

# loop through the download stream and send the data to the target file
do{
    $readlength = $responsestream.Read($readbuffer,0,1024)
    $targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)

$targetfile.close()
Helpful webpage!
http://www.codeproject.com/KB/IP/SimpleFTPDemo.aspx
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 19-02-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Powershell: Best way to automate secure FTP?

I knew there was a .Net class that implemented an FTP client so I googled and bit and found it. After reading the examples shown and a little try-and-error, I came up with this extremely simple FTP client in PowerShell below. Note that by "extremely simple" I mean "with very few features and no error detection code". This only gets one file from the FTP server. I probably wouldn't use this in production without a few changes. But it's still a nice example on how to use the .Net framework with PowerShell and this class in particular

Code:
$localfile = "c:\file.txt"
$remotefile = "/folder/file.txt"
$ftphost = "ftp://ftpserver"
$URI = $ftphost + $remotefile
$username="username"
$password="password"


function Get-FTPFile ($URI,$localfile,$username,$password){

$credentials=New-Object System.Net.NetworkCredential($username,$password)



$ftp=[System.Net.FtpWebRequest]::Create($URI)

$ftp.Credentials=$credentials
$ftp.UseBinary=1
$ftp.KeepAlive=0


$response=$ftp.GetResponse()
$responseStream = $response.GetResponseStream()

$file = New-Object IO.FileStream ($localfile,[IO.FileMode]::Create)

[byte[]]$buffer = New-Object byte[] 1024

$read = 0
do{
$read=$responseStream.Read($buffer,0,1024)
$file.Write($buffer,0,$read)
}
while ($read -ne 0)
$file.close()
}
Reply With Quote
  #6  
Old 18-06-2009
Member
 
Join Date: Jun 2009
Posts: 1
Re: Powershell: Best way to automate secure FTP?

You could at least link back to the blog where you found that information (including the introduction).
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Powershell: Best way to automate secure FTP?"
Thread Thread Starter Forum Replies Last Post
How to Automate the bot in FrontierVille Filiberto Video Games 1 10-02-2011 04:26 AM
How to automate the Winsetup from USB @bLERINA@ Operating Systems 4 29-10-2010 01:11 PM
Using Visual C++ to automate Office Orton Windows Software 5 17-03-2010 04:29 AM
Error Message : 'This Page Contains Both Secure and Non-Secure Items' Chrisch Technology & Internet 3 04-08-2009 12:18 PM
How to Automate PDF document Using JavaScript VinFanatic Software Development 3 13-04-2009 02:12 PM


All times are GMT +5.5. The time now is 08:48 AM.