如何使用PowerShell从ftp下载多个文件

时间:2012-08-09 16:42:41

标签: powershell

我已经有一个循环列出了FTP目录中的文件摘要。 $ outputBuffer包含

/archive/20120806_141250.txt
/archive/20120806_142114.txt
/archive/20120807_090149.txt
/archive/20120808_090348.txt

我有一个For-EachObject用完整的ftp地址替换/存档:

$outputBuffer | Foreach-Object {
    $_ -replace 'archive/', 'ftp://ftp.test.com/outbound/archive/'
    }

我的结果是:

ftp://test.com/outbound/archive/20120806_141250.txt
ftp://test.com/outbound/archive/20120806_142114.txt
ftp://test.com/outbound/archive/20120807_090149.txt
ftp://test.com/outbound/archive/20120808_090348.txt

在我执行替换后,如何在此ForEach-Object语句中添加一行来下载每个文件?

2 个答案:

答案 0 :(得分:0)

$links = $outputBuffer | Foreach-Object {
    $_ -replace 'archive/', 'ftp://ftp.test.com/outbound/archive/'
    }

然后

foreach($link in $links)
{
Invoke-WebRequest $link
}

这应该在PWD下载文件

答案 1 :(得分:0)

试试这个:

$webclient = New-Object System.Net.WebClient
$webclient.credentials = new-object system.net.networkcredential("user", "password")
$outputBuffer | Foreach-Object {
    $filePath = $_ -replace 'archive/', 'ftp://ftp.test.com/outbound/archive/'
    #Adjust D:\local\ to be an actual folder.
    $fileDest = $_ -replace '/archive/', 'D:\local\'
    $uri = New-Object System.Uri($filePath)
    $webclient.DownloadFile($uri, $fileDest)
}