2013-02-05 63 views
2

我使用下面的腳本文件(ftp_test_2.ps1)將文件傳輸到FTP服務器連接:PowerShell的:需要建立一個「積極的」 FTP傳輸從PS腳本

#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中將傳輸模式設置爲「active」?我搜索了網站和MS網站尋求幫助,但找不到任何東西。任何幫助不勝感激。

-Dave Ef中

+1

use System.Net.FtpWebRequest?請參閱http://serverfault.com/a/253255/102464 –

+0

你可以在這裏看看ftp客戶機的powershell模塊:http://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb它默認爲主動模式,當你設置conncetion,並且很容易使用 –

+0

@Goyuix雖然這個問題的答案包含在你鏈接的答案中,但我不會稱這個問題爲完全重複的,因爲這個問題特別提到如何使FTP連接處於活動狀態而不是被動狀態,而另一個則只是詢問如何使用FTP上傳文件。 – SpellingD

回答

3

即使你最終去了不同的路線 - 我決定回來,並提供有關如何子類WebClient答案,並確保所有的FTP請求是「主動」的轉移。這需要PowerShell v2及更高版本才能添加新類型。如果您需要在PowerShell v1中執行此操作,則可以將類定義編譯爲程序集並加載它。

另外值得注意的是,您可以使用與UsePassive屬性相同的方式輕鬆地公開FtpWebRequest類的UseBinary屬性 - 如果您需要設置它。

$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") 
相關問題