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

查看:58
本文介绍了使用 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?

推荐答案

我不确定您是否可以 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天全站免登陆