2013-09-27 53 views
8
$ftpServer = "ftp.example.com" 
$username ="validUser" 
$password ="myPassword" 
$localToFTPPath = "C:\ToFTP" 
$localFromFTPPath = "C:\FromFTP" 
$remotePickupDir = "/Inbox" 
$remoteDropDir = "/Outbox" 
$SSLMode = [AlexPilotti.FTPS.Client.ESSLSupportMode]::ClearText 
$ftp = new-object "AlexPilotti.FTPS.Client.FTPSClient" 
$cred = New-Object System.Net.NetworkCredential($username,$password) 
$ftp.Connect($ftpServer,$cred,$SSLMode) #Connect 
$ftp.SetCurrentDirectory($remotePickupDir) 
$ftp.GetFiles($localFromFTPPath, $false) #Get Files 

Đây là tập lệnh tôi nhận để nhập tệp từ máy chủ FTP.
Tuy nhiên tôi không chắc chắn số remotePickupDir là gì và tập lệnh này có chính xác không?PowerShell Kết nối với máy chủ FTP và nhận các tệp

Trả lời

3

đường dẫn thư mục chọn từ xa nên con đường chính xác trên máy chủ ftp bạn đang tryng để truy cập .. đây là kịch bản để tải về tập tin từ máy chủ .. bạn có thể thêm hoặc sửa đổi với SSLMode ..

#ftp server 
$ftp = "ftp://example.com/" 
$user = "XX" 
$pass = "XXX" 
$SetType = "bin" 
$remotePickupDir = Get-ChildItem 'c:\test' -recurse 
$webclient = New-Object System.Net.WebClient 

$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
foreach($item in $remotePickupDir){ 
    $uri = New-Object System.Uri($ftp+$item.Name) 
    #$webclient.UploadFile($uri,$item.FullName) 
    $webclient.DownloadFile($uri,$item.FullName) 
} 
+10

Nếu $ remotepickupdir = get-childitem c: \ -recurse kiểm tra, PowerShell sẽ cố gắng để có được tất cả các mục ở con đường mà khi nó đặt lên biến. Vì vậy, nó không thể lấy các mục từ xa bởi vì tại thời điểm này thậm chí không có một đăng nhập. Mã của bạn có vẻ sai. – BSAFH

+0

Điều này không được đánh dấu là đã được chấp nhận. Điều này không trả lời câu hỏi! –

1

remotePickupDir sẽ là thư mục bạn muốn truy cập trên máy chủ FTP. Theo như "kịch bản này là chính xác", tốt, nó hoạt động? Nếu nó hoạt động thì nó đúng. Nếu nó không hoạt động, sau đó cho chúng tôi biết thông báo lỗi hoặc hành vi không mong muốn bạn đang nhận được và chúng tôi sẽ có thể trợ giúp bạn tốt hơn.

3

Đây là mã làm việc đầy đủ để tải xuống tất cả các tệp (có ký tự đại diện hoặc phần mở rộng tệp) từ trang FTP sang thư mục cục bộ. Đặt giá trị biến.

#FTP Server Information - SET VARIABLES 
    $ftp = "ftp://XXX.com/" 
    $user = 'UserName' 
    $pass = 'Password' 
    $folder = 'FTP_Folder' 
    $target = "C:\Folder\Folder1\" 

    #SET CREDENTIALS 
    $credentials = new-object System.Net.NetworkCredential($user, $pass) 

    function Get-FtpDir ($url,$credentials) { 
     $request = [Net.WebRequest]::Create($url) 
     $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory 
     if ($credentials) { $request.Credentials = $credentials } 
     $response = $request.GetResponse() 
     $reader = New-Object IO.StreamReader $response.GetResponseStream() 
     while(-not $reader.EndOfStream) { 
      $reader.ReadLine() 
     } 
     #$reader.ReadToEnd() 
     $reader.Close() 
     $response.Close() 
    } 

    #SET FOLDER PATH 
    $folderPath= $ftp + "/" + $folder + "/" 

    $files = Get-FTPDir -url $folderPath -credentials $credentials 

    $files 

    $webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
    $counter = 0 
    foreach ($file in ($files | where {$_ -like "*.txt"})){ 
     $source=$folderPath + $file 
     $destination = $target + $file 
     $webclient.DownloadFile($source, $target+$file) 

     #PRINT FILE NAME AND COUNTER 
     $counter++ 
     $counter 
     $source 
    } 
-1

Để truy xuất tệp/thư mục từ FTP qua powerShell Tôi đã viết một số chức năng, bạn thậm chí có thể lấy nội dung ẩn từ FTP.

Ví dụ để có được tất cả các file mà không được giấu trong một thư mục cụ thể:

Get-FtpChildItem -ftpFolderPath "ftp://myHost.com/root/leaf/" -userName "User" -password "pw" -hidden $false -File 

Ví dụ để có được tất cả các thư mục (cũng ẩn) trong một thư mục cụ thể:

Get-FtpChildItem -ftpFolderPath"ftp://myHost.com/root/leaf/" -userName "User" -password "pw" -Directory 

Bạn chỉ có thể sao chép các chức năng từ mô-đun sau mà không cần cài đặt thư viện thứ 3: https://github.com/AstralisSomnium/PowerShell-No-Library-Just-Functions/blob/master/FTPModule.ps1

15

Thư viện AlexFTPS được sử dụng trong hàng đợi stion dường như đã chết (không được cập nhật từ năm 2011).


Hoặc, bạn có thể thử triển khai tính năng này mà không cần bất kỳ thư viện bên ngoài nào. Nhưng thật không may, cả .NET Framework lẫn PowerShell đều không có hỗ trợ rõ ràng để tải xuống tất cả các tệp trong một thư mục (chỉ cho phép tải xuống tệp đệ quy).

Bạn cần phải thực hiện điều đó bản thân:

  • Danh sách thư mục ở xa
  • iterate mục, tập tin tải về (và tùy chọn recursing vào thư mục con - liệt kê chúng lần nữa, vv)

Phần khó hiểu là xác định tệp từ thư mục con. Không có cách nào để thực hiện điều đó theo cách di động với khung công tác .NET (FtpWebRequest hoặc WebClient). Khuôn khổ .NET tiếc là không hỗ trợ lệnh MLSD, đó là cách duy nhất để lấy danh sách thư mục với các thuộc tính tệp trong giao thức FTP. Xem thêm Checking if object on FTP server is file or directory.

lựa chọn của bạn là:

  • Nếu bạn biết rằng thư mục không chứa bất kỳ thư mục con, sử dụng các phương pháp ListDirectory (NLST FTP lệnh) và chỉ cần tải về tất cả các "tên" như tập tin.
  • Thực hiện thao tác trên tên tệp nhất định không thành công đối với tệp và thành công đối với các thư mục (hoặc ngược lại). I E. bạn có thể thử tải xuống "tên".
  • Bạn có thể may mắn và trong trường hợp cụ thể của bạn, bạn có thể nói với một tập tin từ một thư mục bằng một tên tập tin (ví dụ: tất cả các file của bạn có một phần mở rộng, trong khi thư mục con không)
  • Bạn sử dụng một thư mục dài niêm yết (LIST phương thức command = ListDirectoryDetails) và thử phân tích cú pháp một danh sách máy chủ cụ thể. Nhiều máy chủ FTP sử dụng danh sách * nix-style, trong đó bạn xác định một thư mục theo số d ở đầu mục nhập. Nhưng nhiều máy chủ sử dụng một định dạng khác. Ví dụ sau đây sử dụng phương pháp này (giả sử các định dạng * nix)
function DownloadFtpDirectory($url, $credentials, $localPath) 
{ 
    $listRequest = [Net.WebRequest]::Create($url) 
    $listRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails 
    $listRequest.Credentials = $credentials 

    $lines = New-Object System.Collections.ArrayList 

    $listResponse = $listRequest.GetResponse() 
    $listStream = $listResponse.GetResponseStream() 
    $listReader = New-Object System.IO.StreamReader($listStream) 
    while (!$listReader.EndOfStream) 
    { 
     $line = $listReader.ReadLine() 
     $lines.Add($line) | Out-Null 
    } 
    $listReader.Dispose() 
    $listStream.Dispose() 
    $listResponse.Dispose() 

    foreach ($line in $lines) 
    { 
     $tokens = $line.Split(" ", 9, [StringSplitOptions]::RemoveEmptyEntries) 
     $name = $tokens[8] 
     $permissions = $tokens[0] 

     $localFilePath = Join-Path $localPath $name 
     $fileUrl = ($url + $name) 

     if ($permissions[0] -eq 'd') 
     { 
      if (!(Test-Path $localFilePath -PathType container)) 
      { 
       Write-Host "Creating directory $localFilePath" 
       New-Item $localFilePath -Type directory | Out-Null 
      } 

      DownloadFtpDirectory ($fileUrl + "/") $credentials $localFilePath 
     } 
     else 
     { 
      Write-Host "Downloading $fileUrl to $localFilePath" 

      $downloadRequest = [Net.WebRequest]::Create($fileUrl) 
      $downloadRequest.Method = [System.Net.WebRequestMethods+FTP]::DownloadFile 
      $downloadRequest.Credentials = $credentials 

      $downloadResponse = $downloadRequest.GetResponse() 
      $sourceStream = $downloadResponse.GetResponseStream() 
      $targetStream = [System.IO.File]::Create($localFilePath) 
      $buffer = New-Object byte[] 10240 
      while (($read = $sourceStream.Read($buffer, 0, $buffer.Length)) -gt 0) 
      { 
       $targetStream.Write($buffer, 0, $read); 
      } 
      $targetStream.Dispose() 
      $sourceStream.Dispose() 
      $downloadResponse.Dispose() 
     } 
    } 
} 

Sử dụng các chức năng như:

$credentials = New-Object System.Net.NetworkCredential("user", "mypassword") 
$url = "ftp://ftp.example.com/directory/to/download/" 
DownloadFtpDirectory $url $credentials "C:\target\directory" 

Mã này được dịch từ # ví dụ C của tôi trong C# Download all files and subdirectories through FTP.


Nếu bạn muốn tránh rắc rối với phân tích các danh sách thư mục định dạng máy chủ cụ thể, sử dụng một thư viện của bên thứ 3 để hỗ trợ các MLSD lệnh và/hoặc phân tích khác nhau LIST định dạng niêm yết. Và lý tưởng nhất là hỗ trợ tải xuống tất cả các tệp từ thư mục hoặc thậm chí tải xuống đệ quy.

Ví dụ với WinSCP .NET assembly bạn có thể tải toàn bộ thư mục với một cuộc gọi duy nhất để Session.GetFiles:

# Load WinSCP .NET assembly 
Add-Type -Path "WinSCPnet.dll" 

# Setup session options 
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{ 
    Protocol = [WinSCP.Protocol]::Ftp 
    HostName = "ftp.example.com" 
    UserName = "user" 
    Password = "mypassword" 
} 

$session = New-Object WinSCP.Session 

try 
{ 
    # Connect 
    $session.Open($sessionOptions) 

    # Download files 
    $session.GetFiles("/directory/to/download/*", "C:\target\directory\*").Check() 
} 
finally 
{ 
    # Disconnect, clean up 
    $session.Dispose() 
}  

Bên trong, WinSCP sử dụng lệnh MLSD, nếu được hỗ trợ bởi máy chủ. Nếu không, nó sử dụng lệnh LIST và hỗ trợ hàng chục định dạng danh sách khác nhau.

Session.GetFiles method được đệ quy theo mặc định.

(Tôi là tác giả của WinSCP)

Các vấn đề liên quan