我使用Powershell从ftp站点下载新文件 [英] I Download new files from ftp site with Powershell

查看:258
本文介绍了我使用Powershell从ftp站点下载新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本用一个ftp站点的数据下载一个硬编码的文件名,但我需要它下载最新的文件和(可能是上次下载的时间戳)。我刚开始使用PowerShell,并寻找线索使我的脚本更复杂,但似乎无法获得大部分IVE发现的代码。你能给我一些方向吗?这是我现在的代码:

My script downloads a hardcoded file name with its data from an ftp site, but I need it to download the newest files and (perhaps by last downloaded timestamp). I just started using powershell and have searched for clues to make my script more sophisticated, but cant seem to get most of the code Ive found to work. Can you give me some direction? This is my current code:

  # Create FTP Connection
$FTPRequest = [System.Net.FtpWebRequest]::Create("ftp://ftp.sitepath/newestfile.txt") 
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential("username", "pw") 
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile

$FTPRequest.UsePassive = $false
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
$folderName = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")            
New-Item -itemType Directory -Path \\server\directory\ -Name $FolderName
$targetfile = New-Object  
IO.FileStream "\\server\directory\$FolderName\newestfilename.txt"),        [IO.FileMode]::Create)
# Get FTP File
$FTPResponse = $FTPRequest.GetResponse()
$ResponseStream = $FTPResponse.GetResponseStream()
$FTPReader = New-Object -typename System.IO.StreamReader -ArgumentList $ResponseStream
[byte[]]$readbuffer = New-Object byte[] 1024

#loop through the download stream and send the data to the target file
do{
    $readlength = $ResponseStream.Read($readbuffer,0,1024)
    $targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)
$FTPReader.Close()

$FTPReader.Close()

Thank you for your help.


推荐答案

使用Powershell FTP模块( http://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb a>) - 然后,您可以专注于业务逻辑,而不是使FTP部分工作。

Use Powershell FTP Module(http://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb) - you can then concentrate on your business logic rather than making "the FTP part" work.

这篇关于我使用Powershell从ftp站点下载新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆