0

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

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

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

$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 语句中添加一行以下载每个文件?

4

2 回答 2

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)
}
于 2012-08-09T17:16:23.417 回答
0
$links = $outputBuffer | Foreach-Object {
    $_ -replace 'archive/', 'ftp://ftp.test.com/outbound/archive/'
    }

接着

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

这应该下载 PWD 中的文件

于 2012-08-09T17:06:31.000 回答