我使用下面的腳本文件(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中
use System.Net.FtpWebRequest?請參閱http://serverfault.com/a/253255/102464 –
你可以在這裏看看ftp客戶機的powershell模塊:http://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb它默認爲主動模式,當你設置conncetion,並且很容易使用 –
@Goyuix雖然這個問題的答案包含在你鏈接的答案中,但我不會稱這個問題爲完全重複的,因爲這個問題特別提到如何使FTP連接處於活動狀態而不是被動狀態,而另一個則只是詢問如何使用FTP上傳文件。 – SpellingD