Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Ftp upload

$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    
}

I have problem each time, i try to execute the script, i've tried many solutions but none of them solved my problem

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

When i use filezila it seems to use Proxy ixftp.e-i.net:8011 and it works, i don't know how to set up the proxy in PowerShell

like image 457
user2431419 Avatar asked Aug 25 '26 10:08

user2431419


2 Answers

I've used this script with success:

#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) 
 } 
like image 97
Bradley Forney Avatar answered Aug 27 '26 00:08

Bradley Forney


I am not familiar with using System.Net.WebClient, but is there a reason why you can't use 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"
like image 41
Lisa Dean Avatar answered Aug 27 '26 00:08

Lisa Dean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!