bbcompent1
bbcompent1

Reputation: 494

FTP Download using PowerShell and PowerShell FTP Library

I am trying to figure out how to use this PSFTP module I downloaded from Microsoft. I want to download the entire ftp directory recursively with all directories to my local machine. Has anyone had success doing this and can you share your script? Thanks!


Edit:

ContentLength           : -1
Headers                 : {}
SupportsHeaders         : True
ResponseUri             : ftp://ftp.xxxxxxxxxxxxxxxxxxx.com/
StatusCode              : ClosingData
StatusDescription       : 226-Options: -a -l 
                          226 140 matches total

LastModified            : 1/1/0001 12:00:00 AM
BannerMessage           : 220---------- Welcome to Pure-FTPd     
[privsep]     [TLS] ----------
                      220-You are user number 1 of 500 allowed.
                      220-Local time is now 19:22. Server port: 21.
                      220-This is a private system - No anonymous login
                      220 You will be disconnected after 3 minutes of 
inactivity.

WelcomeMessage          : 230 OK. Current restricted directory is /

ExitMessage             : 221-Goodbye. You uploaded 0 and downloaded 0 
kbytes.
                          221 Logout.                          
IsFromCache             : False
IsMutuallyAuthenticated : False
ContentType             :

And the script:

Import-Module PSFTP
$FTPServer = 'ftp.xxxxxxxxxxxxxxxx.com'
$FTPUsername = 'xxxxxxxxxxxx'
$FTPPassword = 'xxxxxxxxxxxxxxxxxxxx'
$FTPSecurePassword = ConvertTo-SecureString -String $FTPPassword -asPlainText -Force
$FTPCredential = New-Object System.Management.Automation.PSCredential($FTPUsername,$FTPSecurePassword)
Set-FTPConnection -Credentials $FTPCredential -Server $FTPServer -Session MySession -UsePassive 
$Session = Get-FTPConnection -Session MySession 
Get-FTPChildItem -Session $Session -Path / -Recurse

Remediated Code:

$Session = Get-FTPConnection -Session MySession 
$FTPCredential = New-Object System.Management.Automation.PSCredential($FTPUsername,$FTPSecurePassword)
Set-FTPConnection -Credentials $FTPCredential -Server $FTPServer -Session
$Session -UsePassive 
Get-FTPChildItem -path $FTPServer -Session $Session -Recurse | Get-FTPItem -localpath C:\sitebackups\ -RecreateFolders -Verbose

Upvotes: 1

Views: 3166

Answers (1)

Will
Will

Reputation: 200

Is this what you want?

Get-FTPChildItem -path ftp://test.net/ftpfolder/test -Recurse | Get-FTPItem -localpath C:\Users\test\Downloads\ -RecreateFolders -Verbose

Test:

PS C:\Users\test> Get-FTPChildItem -path ftp://test.net/ftpfolder/test -Recurse | Get-FTPItem -localpath C:\Users\test\Downloads\ -RecreateFolders -Verbose
VERBOSE: Performing the operation "Download item:
'ftp://test.net/ftpfolder/test/folder1'" on target "".
VERBOSE: Performing the operation "Download item:
'ftp://test.net/ftpfolder/test/folder2'" on target "".
VERBOSE: Performing the operation "Download item:
'ftp://test.net/ftpfolder/test/file1.zip'" on
target "".
VERBOSE: Creating folder: ftpfolder\test\
226 Transfer complete.

VERBOSE: Performing the operation "Download item:
'ftp://test.net/ftpfolder/test/folder1/event_page.zip'"
on target "C:\Users\test\Downloads\event_page.zip".
VERBOSE: Creating folder: ftpfolder\test\folder2
226 Transfer complete.

You can find more examples from PSFTP/Get-FTPItem.ps1

.EXAMPLE    
    PS P:\> Get-FTPChildItem -path folder/subfolder1 -Recurse | Get-FTPItem -localpath p:\test -RecreateFolders -Verbose
    VERBOSE: Performing operation "Download item: 'ftp://ftp.contoso.com/folder/subfolder1/test.xlsx'" on Target "p:\test\folder\subfolder1".
    VERBOSE: Creating folder: folder\subfolder1
    226 File send OK.

    VERBOSE: Performing operation "Download item: 'ftp://ftp.contoso.com/folder/subfolder1/ziped.zip'" on Target "p:\test\folder\subfolder1".
    226 File send OK.

    VERBOSE: Performing operation "Download item: 'ftp://ftp.contoso.com/folder/subfolder1/subfolder11/ziped.zip'" on Target "p:\test\folder\subfolder1\subfolder11".
    VERBOSE: Creating folder: folder\subfolder1\subfolder11
    226 File send OK.

Upvotes: 1

Related Questions