2012-08-09 2 views
0

이미 FTP 디렉토리에 파일 요약을 나열하는 루프가 있습니다.powershell을 사용하여 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 

$ outputBuffer는

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

내가 전체 FTP 주소와 EachObject가 들어-교체/아카이브를 포함 이 ForEach-Object 문에서 바꾸기를 한 후에 각 파일을 다운로드 할 수 있습니까?

답변

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

다음

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

이것은이 시도 PWD

+1

가 정말 작동합니까? 이 새로운 V3 cmdlet에 대한 설명은 "웹 서비스에 HTTP 및 HTTPS 요청을 보냅니다." –

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) 
} 
+0

"2"인수를 사용하여 "DownloadFile"을 호출하면 예외가 발생합니다. "WebClient 요청 중에 예외가 발생했습니다. " 줄에 53 문자 : 28 + $ webclient.DownloadFile <<<< ($적인 filePath, $ fileDest) + CategoryInfo : NotSpecified (:) [] MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException – Jacob

+0

와 $ fileDest 줄 바꾸기 '$ fileDest = C : \ test.txt'(마이너스 '')를 확인하고, 모두 완료되면 c : \에 해당 이름의 파일이 있거나 여전히 동일한 오류가 발생하는지 확인하십시오. –

+0

"2"인수가있는 "DownloadFile"예외 : "원격 서버에서 오류를 반환했습니다 : (550) 파일을 사용할 수 없습니다 (예 : 파일을 찾을 수 없음, 액세스 할 수 없음)." – Jacob

관련 문제