$infographie="C:ArchiveInfographie28052013myfile.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:Scriptstest.ps1:14 char:22
+ $webclient.UploadFile <<<< ($uri,$File)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
当我使用filezila时,它似乎使用代理ixftp.e.i.net:8011并且它有效,我不知道如何在PowerShell中设置代理
我已经成功地使用了这个脚本(来自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)
}
我不熟悉使用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"
尝试在webclient对象上设置它(同时检查$proxy的属性,取决于您的环境,您可能需要设置其中一些):
$proxy = New-Object System.Net.WebProxy http://proxy:8080
$webclient.Proxy = $proxy