#获取文件列表- $listFTP = [system.net.ftpwebrequest] [system.net.webrequest]::create("ftpurl")
- $listFTP.Credentials = New-Object System.Net.NetworkCredential("username","password")
- $listFTP.Method=[system.net.WebRequestMethods+ftp]::listdirectorydetails
- $response = $listFTP.getresponse()
- $stream = New-Object System.Io.StreamReader($response.getresponsestream(),[System.Text.Encoding]::UTF8)
- while(-not $stream.EndOfStream){
- $stream.ReadLine()
- }
- $stream.Close()
- $response.Close()
复制代码 #上传文件- #ftpurl:ftp路径
- #username:ftp用户名
- #password:ftp密码
- #localfilename:要上传的文件名
- $fileinf=New-Object System.Io.FileInfo("localfilename")
- $upFTP = [system.net.ftpwebrequest] [system.net.webrequest]::create("ftpurl"+$fileinf.name)
- $upFTP.Credentials = New-Object System.Net.NetworkCredential("username","password")
- $upFTP.Method=[system.net.WebRequestMethods+ftp]::UploadFile
- $upFTP.KeepAlive=$false
- $sourceStream = New-Object System.Io.StreamReader($fileInf.fullname)
- $fileContents = [System.Text.Encoding]::UTF8.GetBytes($sourceStream.ReadToEnd())
- $sourceStream.Close();
- $upFTP.ContentLength = $fileContents.Length;
- $requestStream = $upFTP.GetRequestStream();
- $requestStream.Write($fileContents, 0, $fileContents.Length);
- $requestStream.Close();
- $response =$upFTP.GetResponse();
- $response.StatusDescription
- $response.Close();
复制代码 #下载文件- $downFTP = [system.net.ftpwebrequest] [system.net.webrequest]::create("url"+"filename")
- $downFTP.Credentials = New-Object System.Net.NetworkCredential("username","password")
- $response = $downFTP.getresponse()
- $stream=$response.getresponsestream()
- $buffer = new-object System.Byte[] 2048
- $outputStream=New-Object System.Io.FileStream("patch"+"filename","Create")
- while(($readCount = $stream.Read($buffer, 0, 1024)) -gt 0){
- $outputStream.Write($buffer, 0, $readCount)
- }
- $outputStream.Close()
-
- $stream.Close()
- $response.Close()
复制代码 http://hi.baidu.com/shrekzz/item/07007b2b8cd15d8fae48f5fc |