# create the FtpWebRequest and configure it
$ftp = [System.Net.FtpWebRequest]::Create("ftp://localhost/me.png")
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential("anonymous","anonymous@localhost")
$ftp.UseBinary = $true
$ftp.UsePassive = $true
# read in the file to upload as a byte array
$content = [System.IO.File]::ReadAllBytes("C:\me.png")
$ftp.ContentLength = $content.Length
# get the request stream, and write the bytes into it
$rs = $ftp.GetRequestStream()
$rs.Write($content, 0, $content.Length)
# be sure to clean up after ourselves
$rs.Close()
$rs.Dispose()
#Directory where to find pictures to upload
$Dir= 'c:\fff\medias\'
#Directory where to save uploaded pictures
$saveDir = 'c:\fff\save\'
#ftp server params
$ftp = 'ftp://10.0.1.11:21/'
$user = 'user'
$pass = 'pass'
#Connect to ftp webclient
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
#Initialize var for infinite loop
$i=0
#Infinite loop
while($i -eq 0){
#Pause 1 seconde before continue
Start-Sleep -sec 1
#Search for pictures in directory
foreach($item in (dir $Dir "*.jpg"))
{
#Set default network status to 1
$onNetwork = "1"
#Get picture creation dateTime...
$pictureDateTime = (Get-ChildItem $item.fullName).CreationTime
#Convert dateTime to timeStamp
$pictureTimeStamp = (Get-Date $pictureDateTime).ToFileTime()
#Get actual timeStamp
$timeStamp = (Get-Date).ToFileTime()
#Get picture lifeTime
$pictureLifeTime = $timeStamp - $pictureTimeStamp
#We only treat pictures that are fully written on the disk
#So, we put a 2 second delay to ensure even big pictures have been fully wirtten in the disk
if($pictureLifeTime -gt "2") {
#If upload fails, we set network status at 0
try{
$uri = New-Object System.Uri($ftp+$item.Name)
$webclient.UploadFile($uri, $item.FullName)
} catch [Exception] {
$onNetwork = "0"
write-host $_.Exception.Message;
}
#If upload succeeded, we do further actions
if($onNetwork -eq "1"){
"Copying $item..."
Copy-Item -path $item.fullName -destination $saveDir$item
"Deleting $item..."
Remove-Item $item.fullName
}
}
}
}
8 回答
还有其他一些方法 . 我使用了以下脚本:
您可以使用以下命令对Windows FTP命令行实用程序运行脚本
(查看this article)
关于SO的以下问题也回答了这个问题:How to script FTP upload and download?
我不会声称这比最高投票的解决方案更优雅......但这很酷(嗯,至少在我看来LOL)以自己的方式:
如您所见,它使用了那个极简的内置Windows FTP客户端 . 更短更直接 . 是的,我实际上已经使用了它,它的确有效!
我不确定你是否可以100%防止脚本不挂起或崩溃,因为有些东西无法控制(如果服务器在上传中途失去了怎么办?) - 但这应该为你开始提供坚实的基础:
这是我的超酷版本,因为它有一个进步条:-)
我知道,这是一个完全无用的功能,但它看起来仍然很酷\ m / \ m /
PS . 当我想知道“它是否停止工作,还是仅仅是我的ASDL连接缓慢?”时,有很多帮助?
Goyuix's solution效果很好,但是如上所述它给了我这个错误:"The requested FTP command is not supported when using HTTP proxy."
在
$ftp.UsePassive = $true
之后添加此行为我解决了问题:使用PowerShell将二进制文件上传到FTP服务器的最简单方法是使用WebClient.UploadFile:
如果您需要更大的控件,
WebClient
不提供(如TLS / SSL加密等),请使用FtpWebRequest . 简单的方法是使用Stream.CopyTo将FileStream
复制到FTP流:如果您需要监控上传进度,则必须自己按块复制内容:
我最近为powershell编写了几个与FTP通信的函数,请参阅https://github.com/AstralisSomnium/PowerShell-No-Library-Just-Functions/blob/master/FTPModule.ps1 . 下面的第二个功能,您可以将整个本地文件夹发送到FTP . 在该模块中甚至可以递归地删除/添加/读取文件夹和文件 .
}
您可以通过PowerShell简单地处理文件上传,就像这样 . 完整项目可在Github上找到https://github.com/edouardkombo/PowerShellFtp