FTP自动上传

注:该脚本上传的压缩文件,通过文件资源管理器拿下来后会提示压缩文件损坏。使用 ftp -s:d:\audit_log\ftp_upload.txt 方法可行(http://www.cnblogs.com/dreamer-fish/p/3271590.html)。还未测试通过ps1下载ftp是否还会提示压   缩文件损坏。

=======================

#定义FTP服务器地址
$ftpURL = "ftp://192.168.12.6/"
#定义登录FTP服务器的账户及密码
$username = "testj\adadmin"
$userpass = "boc.123"
#定义需要上传的文件位置
$filePath = "D:\iso\SQLServer2012.iso"
$fileInfo = Get-Item $filePath

$ftpFile = [system.net.ftpwebrequest] [system.net.webrequest]::create($ftpURL+$fileInfo.Name)
$ftpFile.Credentials = New-Object System.Net.NetworkCredential($username,$userpass)
$ftpFile.Method = [system.net.WebRequestMethods+ftp]::UPloadFile
#$ftpFile.UseBinary = $true
$ftpFile.KeepAlive=$false
$fileStream = New-Object System.IO.StreamReader($fileInfo.FullName)
$fileContents = [System.Text.Encoding]::UTF8.GetBytes($fileStream.ReadToEnd())
$fileStream.Close()
$ftpFile.ContentLength = $fileContents.Length
$requestStream = $ftpFile.GetRequestStream()
$requestStream.Write($fileContents, 0, $fileContents.Length)
$requestStream.Close()
$response =$ftpFile.GetResponse()
#$response.StatusDescription
$response.Close()
#$response

posted on 2013-08-14 16:00  momingliu11  阅读(951)  评论(0编辑  收藏  举报