回答(8)
3 years ago
还有其他一些方法 . 我使用了以下脚本:
$File = "D:\Dev\somefilename.zip";
$ftp = "ftp://username:[email protected]/pub/incoming/somefilename.zip";
Write-Host -Object "ftp url: $ftp";
$webclient = New-Object -TypeName System.Net.WebClient;
$uri = New-Object -TypeName System.Uri -ArgumentList $ftp;
Write-Host -Object "Uploading $File...";
$webclient.UploadFile($uri, $File);
您可以使用以下命令对Windows FTP命令行实用程序运行脚本
ftp -s:script.txt
3 years ago
我不会声称这比最高投票的解决方案更优雅......但这很酷(嗯,至少在我看来LOL)以自己的方式:
$server = "ftp.lolcats.com"
$filelist = "file1.txt file2.txt"
"open $server
user $user $password
binary
cd $dir
" +
($filelist.split(' ') | %{ "put ""$_""`n" }) | ftp -i -in
如您所见,它使用了那个极简的内置Windows FTP客户端 . 更短更直接 . 是的,我实际上已经使用了它,它的确有效!
3 years ago
我不确定你是否可以100%防止脚本不挂起或崩溃,因为有些东西无法控制(如果服务器在上传中途失去了怎么办?) - 但这应该为你开始提供坚实的基础:
# 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")