Results 1 to 6 of 6

Thread: Powershell: Best way to automate secure FTP?

  1. #1
    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?

  2. #2
    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!

  3. #3
    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.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    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()
    }

  6. #6
    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).

Similar Threads

  1. How to Automate the bot in FrontierVille
    By Filiberto in forum Video Games
    Replies: 1
    Last Post: 10-02-2011, 04:26 AM
  2. How to automate the Winsetup from USB
    By @bLERINA@ in forum Operating Systems
    Replies: 4
    Last Post: 29-10-2010, 01:11 PM
  3. Using Visual C++ to automate Office
    By Orton in forum Windows Software
    Replies: 5
    Last Post: 17-03-2010, 04:29 AM
  4. Replies: 3
    Last Post: 04-08-2009, 12:18 PM
  5. How to Automate PDF document Using JavaScript
    By VinFanatic in forum Software Development
    Replies: 3
    Last Post: 13-04-2009, 02:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,407,923.48909 seconds with 16 queries