Powershell:需要创建一个“活动”从PS脚本传输ftp连接 [英] Powershell: need to create an "active" ftp transfer connection from a PS script

查看:202
本文介绍了Powershell:需要创建一个“活动”从PS脚本传输ftp连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的脚本文件(ftp_test_2.ps1)将文件传输到ftp服务器:

  #we指定我们想要上传的所有文件的目录都包含
$ Dir =C:/ Users / you-who / Documents / textfiles /
#ftp服务器

$ ftp =ftp://192.168.100.101:21/
$ user =aP_RDB
$ pass =pw
$ webclient = New-Object System.Net.WebClient
$ webclient.Credentials = New-Object System.Net.NetworkCredential($ user,$ pass)

#列出每个txt文件
foreach($ item in(dir $ Dir ($ ftp + $ item.Name)
$ uri
尝试上传... $ item
$ webclient.UploadFile($ uri,$ item.FullName)
}

当$ ftp设置为本地主机并且连接到另一个ftp测试服务器时,此工作正常。但是,在生产ftp服务器上,服务器拒绝使用Powershell错误进行上载:

 异常情况下调用UploadFile为2 参数:远程服务器返回错误:227进入被动模式(192,168,101,99,228,67)。 

在C:\ Users \ you-who \Documents\SandBox\ftp_test_2.ps1:17 char:24

\ + $ webclient.UploadFile <<<< ($ uri,$ item.FullName)
\ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException
\ + FullyQualifiedErrorId:DotNetMethodExceptionPP
pre>

在生产服务器上运行FileZilla服务器。在服务器用户界面中的消息不会显示任何错误:

 (000029)2/5/2013 5:上午58:53  - (未登录)(192.168.100.101)> USER aP_RDB 
(000029)2/5/2013 5:58:53 AM - (未登录)(192.168.100.101)> 331 ap_rdb
所需密码(000029)2/5/2013 5:58:53 AM - (未登录)(192.168.100.101)> PASS *******
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)> 230登录
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)> OPTS utf8 on
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)>启用200 UTF8模式
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)> PWD
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)> 257/是当前目录。
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)> CWD /
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)> 250 CWD成功。 /是当前目录。
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)> TYPE I
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)> 200类型设置为I
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)> PASV
(000029)2/5/2013 5:58:53 AM - ap_rdb(192.168.100.101)> 227进入被动模式(192,168,101,99,228,74)
(000029)2/5/2013 5:59:14 AM - ap_rdb(192.168.100.101)>断开。

从我的笔记本电脑上的FileZilla ftp客户端,我可以在生产ftp上连接并获取文件server IF 我将传输模式设置为活动(在FileZilla客户端中)以连接到生产ftp服务器。



由于我无法更改生产服务器,有没有办法在我的脚本中的某个位置的PowerShell中将传输模式设置为活动?我搜索了网站和MS网站寻求帮助,但找不到任何东西。任何帮助不胜感激。



-Dave Ef

解决方案

不同的路线 - 我决定回来,并提供如何子类的答案 WebClient 并确保所有FTP请求都是活动传输。这需要PowerShell v2及更高版本才能添加新类型。如果您需要在PowerShell v1中执行此操作,您可以将类定义编译为程序集并加载它。



还要注意,您可以轻松地公开 UseBinary FtpWebRequest类的属性,方法与 UsePassive 属性相同 - 如果您需要设置它。

  $ def = @
公共类ActiveFtpWebClient:System.Net.WebClient
{
保护覆盖System.Net.WebRequest GetWebRequest(System.Uri地址)
{
System.Net.WebRequest request = base.GetWebRequest(address);
if(request is System.Net.FtpWebRequest)
{
(请求为System.Net.FtpWebRequest).UsePassive = false;
}
返回请求;
}
}
@

Add-Type -TypeDefinition $ def
$ ftp = New-Object ActiveFtpWebClient
$ ftp.DownloadString(ftp://server/file.txt)


I am using the following script file (ftp_test_2.ps1) to transfer files to an ftp server:

#we specify the directory where all files that we want to upload are contained 
$Dir="C:/Users/you-who/Documents/textfiles/"
#ftp server

$ftp = "ftp://192.168.100.101:21/"
$user = "aP_RDB"
$pass = "pw"
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  

#list every txt file
foreach($item in (dir $Dir "*.txt"))
{ 
    "creating URI..."
    $uri = New-Object System.Uri($ftp+$item.Name) 
    $uri
    "attempting upload... $item"
    $webclient.UploadFile($uri, $item.FullName) 
 } 

This works fine when the $ftp is set to the local host and also connecting to another ftp test server. However, on the production ftp server, the server rejects the upload with the Powershell error:

Exception calling "UploadFile" with "2" argument(s): "The remote server returned an error: 227 Entering Passive Mode (192,168,101,99,228,67)."

At C:\Users\you-who\Documents\SandBox\ftp_test_2.ps1:17 char:24

\+$webclient.UploadFile <<<< ($uri, $item.FullName)
     \+CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
     \+ FullyQualifiedErrorId : DotNetMethodExceptionPP

On the production server there is a FileZilla server running. The messages in the server UI do not "seem" to show any errors:

(000029)2/5/2013 5:58:53 AM - (not logged in) (192.168.100.101)> USER aP_RDB
(000029)2/5/2013 5:58:53 AM - (not logged in) (192.168.100.101)> 331 Password required for ap_rdb
(000029)2/5/2013 5:58:53 AM - (not logged in) (192.168.100.101)> PASS *******
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> 230 Logged on
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> OPTS utf8 on
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> 200 UTF8 mode enabled
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> PWD
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> 257 "/" is current directory.
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> CWD /
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> 250 CWD successful. "/" is current directory.
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> TYPE I
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> 200 Type set to I
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> PASV
(000029)2/5/2013 5:58:53 AM - ap_rdb (192.168.100.101)> 227 Entering Passive Mode   (192,168,101,99,228,74)
(000029)2/5/2013 5:59:14 AM - ap_rdb (192.168.100.101)> disconnected.

From my FileZilla ftp client on my laptop, I can connect and put/get files on the production ftp server IF I set the "transfer mode" to "active" (in the FileZilla client) for the connection to the production ftp server.

Since I cannot change the production server, is there a way to set the transfer mode to "active" from within Powershell somewhere in my script above? I searched the web and MS sites for help on this, but could not find anything on . Any help greatly appreciated.

-Dave Ef

解决方案

Even though you ended up going a different route - I decided to come back and provide the answer on how to sub class WebClient and ensure that all FTP requests are "active" transfers. This requires PowerShell v2 and above to add a new type. If you need to do this in PowerShell v1, you can compile the class definition to an assembly and load it.

Also of note, you can easily expose the UseBinary property of the FtpWebRequest class in the same manner as the UsePassive property - if you need to set it as well.

$def = @"
public class ActiveFtpWebClient : System.Net.WebClient
{
  protected override System.Net.WebRequest GetWebRequest(System.Uri address)
  {
    System.Net.WebRequest request = base.GetWebRequest(address);
    if (request is System.Net.FtpWebRequest)
    {
      (request as System.Net.FtpWebRequest).UsePassive = false;
    }
    return request;
  }
}
"@

Add-Type -TypeDefinition $def
$ftp = New-Object ActiveFtpWebClient
$ftp.DownloadString("ftp://server/file.txt")

这篇关于Powershell:需要创建一个“活动”从PS脚本传输ftp连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆