我使用以下脚本文件(ftp_test_2.ps1)将文件传输到ftp服务器:
#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)
}
当$ ftp设置为本地主机并连接到另一个ftp测试服务器时,这可以正常工作。但是,在生产ftp服务器上,服务器拒绝上载Powershell错误:
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
在生产服务器上运行FileZilla服务器。服务器UI中的消息“似乎”不显示任何错误:
(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.
从笔记本电脑上的FileZilla ftp客户端,我可以在生产ftp服务器上连接并放置/获取文件 IF 我将“传输模式”设置为“活动”(在FileZilla客户端中)用于连接到生产ftp服务器。
由于我无法更改生产服务器,有没有办法在上面的脚本中的某个位置从Powershell中将传输模式设置为“活动”?我在网站和MS网站上搜索了这方面的帮助,但找不到任何东西。任何帮助非常感谢。
-Dave Ef
答案 0 :(得分:3)
即使您最终走的是另一条路线 - 我决定回来并提供有关如何子类WebClient的答案,并确保所有FTP请求都是“活动”传输。这需要PowerShell v2及更高版本来添加新类型。如果需要在PowerShell v1中执行此操作,可以将类定义编译为程序集并加载它。
另外值得注意的是,您可以使用与UseBinary
属性相同的方式轻松公开FtpWebRequest类的UsePassive
属性 - 如果您还需要设置它。
$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")