标题: [转载代码] PowerShell中使用FTP [打印本页]
作者: DAIC 时间: 2013-9-6 17:51 标题: PowerShell中使用FTP
#获取文件列表- $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
作者: PowerShell 时间: 2013-9-6 18:25
本帖最后由 PowerShell 于 2013-9-6 21:09 编辑
1 此贴只应天上有,人间哪的几回闻?
2 此贴作者乃牛人,吾敬仰之.隐藏的powershell牛人很多的。
3 此贴是powershell调用.net语法之精华.应看10遍以上.完美的教学用的代码。
4 此ftp功能,完全是.net的面向对象语法.用的是属性和方法。而不是给某个ftp客户端软件,如xxftp.exe传字符,传值。也不是传一个值,然后sleep半秒那种。
5 powershell的ftp库,还有nsoftware公司的net cmdlets v4 ,里面网络库太多,不光是ftp.免费命令的介绍(和下载)在这里 http://www.netcmdlets.com/overview/netcmdlets/
6 群友也补充个 http://gallery.technet.microsoft ... FTP-Client-db6fe0cb
作者: hzcxdgioa 时间: 2013-11-30 07:03
支持楼主的帖子。!!!!!!!!!!!
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |