PowerShell Ftp upload

12,786

Solution 1

I've used this script with success (from 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) 
 } 

Solution 2

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"
Share:
12,786
Admin
Author by

Admin

Updated on June 27, 2022

Comments

  • Admin
    Admin almost 2 years
    $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

  • Admin
    Admin almost 11 years
    The Same Error, Helppppppppppp! plz
  • Shay Levy
    Shay Levy almost 11 years
    Typo? Wrong variable name? The error shows UploadFile($uri,$File) and your script shows UploadFile($uri, $infographie)
  • Admin
    Admin almost 11 years
    $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) try { $webclient.UploadFile($uri, $infographie) } catch { Write-Host "An Error occured while uploading file to: $Uri" Throw } I Checked the Code it Works only with Filezila, i didn't manage to get it work with CMD, PowerShell and Curl
  • Admin
    Admin almost 11 years
    Thank's for replying, I Tried to use Ftp also, but i have to set up a proxy powershell and i didn't manage to get it work ?! Can you give me the instructions to set up proxy with your Function please ?!!
  • Lisa Dean
    Lisa Dean almost 11 years
    I'm sorry, I missed that this was the main issue. I haven't had to deal with using a proxy before.
  • JeremyCanfield
    JeremyCanfield over 7 years
    This answer deserves more than just 4 "this answer is useful" votes. This code is so minimal and easy to digest. Thank you for sharing this Bradley. The simple effectiveness of this code is what makes it so elegant.