PowerShell FTP上传

3
$infographie="C:\Archive\Infographie\28052013\myfile.pdf"

$ftp = "ftp://174.9.102.210/public_html/infographie/myfile.pdf"

$user = "****"

$pass = "*******"

$webclient = New-Object System.Net.WebClient 

$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  

$uri = New-Object System.Uri($ftp)

try{

$webclient.UploadFile($uri, $infographie) 

}
catch
{

Write-Host "`nAn Error occured while uploading file to: $Uri"

Throw    
}

每次我尝试执行脚本时都会遇到问题,我尝试了很多解决方案,但没有一个解决了我的问题。

Exception calling "UploadFile" with "2" argument(s): "An exception occurred during
a ebClient request."
At D:\Scripts\test.ps1:14 char:22
+ $webclient.UploadFile <<<< ($uri,$File)
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

当我使用FileZilla时,它似乎使用代理ixftp.e-i.net:8011并且正常工作。我不知道如何在PowerShell中设置代理。
3个回答

10

我已经成功地使用了这个脚本(来自http://gallery.technet.microsoft.com/scriptcenter/80647f66-139c-40a4-bb7a-04a2d73d423c):

#we specify the directory where all files that we want to upload  
$Dir="C:/Dir"    

#ftp server 
$ftp = "ftp://ftp.server.com/dir/" 
$user = "user" 
$pass = "Pass"  

$webclient = New-Object System.Net.WebClient 

$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  

#list every sql server trace file 
foreach($item in (dir $Dir "*.trc")){ 
    "Uploading $item..." 
    $uri = New-Object System.Uri($ftp+$item.Name) 
    $webclient.UploadFile($uri, $item.FullName) 
 } 

1
这个答案值得比4个“这个答案有用”的投票更多的认可。这段代码非常简洁易懂。感谢Bradley分享。正是这段代码的简单有效性使其如此优雅。 - JeremyCanfield

1

我不熟悉使用System.Net.WebClient,但是你为什么不能使用ftp.exe呢?

function FTP-File($FTPserver, $FTPusername, $FTPpassword, $FTPfile){

  #Build FTP script file and run FTP command

  "open $FTPserver" | Out-File ftp.scr -Encoding ASCII
  $FTPusername | Out-File ftp.scr -Encoding ASCII -Append
  $FTPpassword | Out-File ftp.scr -Encoding ASCII -Append
  "put " + $FTPfile | Out-File ftp.scr -Encoding ASCII -Append
  "quit" | Out-File ftp.scr -Encoding ASCII -Append
  ftp.exe -s:ftp.scr
  Remove-Item ftp.scr
}

FTP-File "10.0.0.250" "username" "password" "c:\testfile.txt"

谢谢回复,我也尝试使用FTP,但是我必须设置代理PowerShell,而我没有成功让它工作?!您能否给我设置代理的指示,以便我可以使用您的功能?!! - user2431419
抱歉,我错过了这个是主要问题。我以前没有处理过使用代理的情况。 - Lisa Dean

0
尝试在webclient对象上设置它(还要检查$proxy的属性,根据您的环境,您可能需要设置其中一些):
$proxy = New-Object System.Net.WebProxy http://proxy:8080
$webclient.Proxy = $proxy

相同的错误,帮帮我!拜托了 - user2431419
打错字了?变量名错误了吗?错误显示为UploadFile($uri,$File),而你的脚本显示为UploadFile($uri, $infographie)。 - Shay Levy
$infographie = "C:\cat.jpg" $ftp = "ftp://ftp.something.com/try/cat.jpg" $user = "" $pass = "" $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($user, $pass) $proxy = New-Object System.Net.WebProxy http://xx.xx.xx.xx:xxxx $webclient.Proxy = $proxy $uri = New-Object System.Uri($ftp) 尝试: { $webclient.UploadFile($uri, $infographie) } 捕获: { Write-Host“上传文件到以下位置时发生错误:$Uri” 抛 }我检查了代码,它只适用于FileZila,我无法让它在CMD、PowerShell和Curl中工作。 - user2431419

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接