使用Powershell

时间:2017-02-21 19:22:19

标签: powershell ftp

我正在使用的供应商将zip文件上传到FTP。我需要下载在那里上传的内容并根据需要进行处理。

使用Powershell,如何从FTP文件夹下载*.*

(参考https://social.technet.microsoft.com/Forums/office/en-US/744ee28a-9340-446a-b698-4b96e081b501/download-files-from-ftp-server?forum=winserverpowershell

# Config
$Username = "user"
$Password = "password"
$LocalFile = "C:\tools\file.zip"
$RemoteFile = "ftp://myftpserver:22/Folder1/Folder/file.csv"

# Create a FTPWebRequest 
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile) 
$FTPRequest.Credentials = New-Object     System.Net.NetworkCredential($Username,$Password) 
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
$FTPRequest.UseBinary = $true 
$FTPRequest.KeepAlive = $false
$ftpRequest.EnableSsl = $true
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse() 
# Get a download stream from the server response 
$ResponseStream = $FTPResponse.GetResponseStream() 
# Create the target file on the local system and the download buffer 
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create) 
[byte[]]$ReadBuffer = New-Object byte[] 1024 
# Loop through the download 
    do { 
        $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024) 
        $LocalFileFile.Write($ReadBuffer,0,$ReadLength) 
    } 
    while ($ReadLength -ne 0)

有没有办法让$ RemoteFile像ftp://myftpserver:22/Folder1/Folder/*.zipftp://myftpserver:22/Folder1/Folder/*.*

如果有相同的帖子,我道歉。我看到了一些类似但不够接近的问题。

1 个答案:

答案 0 :(得分:0)

我创建了PowerShell SFTP,FTP和FTPS脚本。你必须从我的github下载它。我不使用任何第三方应用程序,因为我不相信他们。我使用的是SFTP部分的REBEX.dll和FTP和FTPS的.Net WebRequest。

https://github.com/FallenHoot/ZOTECH-PS/blob/master/SFTP.zip

如果您在理解代码时遇到问题,请告诉我们。如果您不打算使用Get-SFTP功能,请将其注释掉。