0
votes

I would like to use WebRequest class upload to remote secure ftp site and have tried using Webclient but getting all sort of exception.

Suffice tosay that I have successfully use Filezilla to connect and upload file the remote but would like to use powershell to automate the process.

Client server has windows 2008 R2 standard - powershell version is 2.0

here is the code I ran and below it is the exception I now get

#setup permission + working dir
$user = "ftpclient"
$password = "****"
$localdir = "C:/Extracts/foldern/"
$ftpdir = "ftps://100.100.100.0:900/Outbound"
###########################

$client = New-Object System.Net.WebClient 
$client.Credentials = New-Object System.Net.NetworkCredential($user,$password) 

foreach($item in Get-ChildItem -recurse $localdir)
{ 
    $filename = [system.io.path]::GetFullPath($item.FullName).SubString([system.io.path]::GetFullPath($localdir).Length + 1)
    Write-Host "Send  $item..."
    $file = New-Object System.Uri($ftpdir+"/"+$filename) 
        $client.UploadFile($file, $item.FullName)
 }

Transfert de EDI_CIG_OrbitExtract.zip...
Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:5 char:31
+             $client.UploadFile <<<< ($file, $item.FullName)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I have read somewhere about using Webrequest instead of Webclient but dont know how.

any assistance is greatly appreciated

1

1 Answers

0
votes

There's a function in PowerShell Pipeworks, a PowerShell module and web language written atop PowerShell, that allows you to upload data to FTP. It's called Push-FTP.

Here's a quick sample.

Push-Ftp -Path c:\Example -Include *.aspx

It's built atop using Net.Webclient, so if you don't like it, you can crack it open to see how it works.