使用PowerShell和PowerShell FTP库进行FTP下载

时间:2017-01-06 01:52:24

标签: powershell

我正在试图弄清楚如何使用我从Microsoft下载的这个PSFTP模块。我想以递归方式将整个ftp目录下载到我的本地机器上。有没有人成功做到这一点,你能分享你的脚本吗?谢谢!

编辑:

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             :

脚本:

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

修复代码:

$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

1 个答案:

答案 0 :(得分:1)

这是你想要的吗?

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

<强>测试

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.

您可以在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.