Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After some experimentation I came up with this way to automate a secure FTP download in PowerShell. This script runs off the <a href="http://www.secureftp-test.com/" rel="noreferrer">public test FTP server</a> administered by Chilkat Software. So you can copy and paste this code and it will run without modification.</p> <pre><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() </code></pre> <p>I found a lot of helpful information at these links</p> <ul> <li><a href="http://www.techtalkz.com/microsoft-windows-powershell/190388-ftp-downloads-encoding-problems.html" rel="noreferrer">FTP downloads: encoding problems</a></li> <li><a href="http://www.codeproject.com/KB/IP/SimpleFTPDemo.aspx" rel="noreferrer">Simple FTP demo application</a></li> <li><a href="http://rcovelo.blogspot.com/2008/10/powershell-very-simple-ftp-client.html" rel="noreferrer">Very Simple FTP Client</a></li> </ul> <p>If you want to use an SSL connection you need to add the line</p> <pre><code>$ftprequest.EnableSsl = $true </code></pre> <p>to the script before you call GetResponse(). Sometimes you may need to deal with a server security certificate that is expired (like I unfortunately do). There is a page at the <a href="http://poshcode.org/634" rel="noreferrer">PowerShell Code Repository</a> that has a code snippet to do that. The first 28 lines are the most relevant for the purposes of downloading a file.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload