使用PowerShell使用FTP上传文件 [英] Upload files with FTP using PowerShell

查看:212
本文介绍了使用PowerShell使用FTP上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PowerShell通过FTP将文件传输到匿名FTP服务器。我不会使用任何额外的软件包。如何?

I want to use PowerShell to transfer files with FTP to an anonymous FTP server. I would not use any extra packages. How?

脚本不会有挂起或崩溃的风险。

There must be no risk that the script hangs or crashes.

推荐答案

我不确定你能否100%地防止脚本不会挂起或崩溃,因为有些东西是你无法控制的(如果服务器在上传失去能力的情况下会怎样?) - 但这应该提供一个坚实的基础。让你开始:

I am not sure you can 100% bullet proof the script from not hanging or crashing, as there are things outside your control (what if the server loses power mid-upload?) - but this should provide a solid foundation for getting you started:

# 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()

这篇关于使用PowerShell使用FTP上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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