1
votes

I need a script to help me for uploading a single file, to a cloud.

I found some answers with the protocol SFTP (SSH), but I cannot find a script working with FTPS (SSL).

I have tried this script, but it doesn't work:

Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.ParseUrl("**ftps**://**login**:**password**@**ipoftheremoteserver**:990/")

$session = New-Object WinSCP.Session
$session.Open($sessionOptions)

$session.PutFiles("D:\QAPPLI\Quadra\DATABASE\PAIE\000001\qpaie.mdb", "/FOLDER1/08h00").Check()

$session.Dispose()

I have this error:

PS C:\Windows\system32> D:\Script\08h00_000001_Client1_to_ftps.ps1 Exception lors de l'appel de « Check » avec « 0 » argument(s) :
« Erreur lors du transfert du fichier 'D:\QAPPLI\Quadra\DATABASE\PAIE\000001\qpaie.mdb'. Server sent passive reply with unroutable address 172.16.59.131, using host address instead.
Copie de fichiers vers le coté distant échouée.
Filename invalid 
Au niveau de D:\08h00_000001_Client1_to_ftps.ps1 : 8 Caractère : 85 + $session.PutFiles("D:\QAPPLI\Quadra\DATABASE\PAIE\000001\qpaie.mdb", "/FOLDER1/08h00").Check <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

Translated to English:

D:Script08h00_000001_Client1_to_ftps.ps1 Exception when calling "Check" with "0" argument (s): «"Error transferring file ' D:QAPPLIQuadraDATABASEPAIE000001qpaie.mdb '. Server sent passive reply with unroutable address 172.16.59.131, using host address instead.
Copying files to the failed remote side.
Filename Invalid
at D:08h00_000001_Client1_to_ftps.ps1:8 character: 85 + $session. PutFiles ( "D:QAPPLIQuadraDATABASEPAIE000001qpaie.mdb", "/FOLDER1/08h00"). Check < < < () + CategoryInfo: NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId: DotNetMethodException

Many thanks for your help.

2
You should translate your error message in English. See also the answer of this SO question regarding sending a passive reply.Jeroen Heier

2 Answers

0
votes

The remotePath (second) argument of Session.PutFiles is:

Full path to upload the file to.

While you seem to pass in a path to a folder only, not full path to a file.

This should be correct:

$session.PutFiles(
    "D:\QAPPLI\Quadra\DATABASE\PAIE\000001\qpaie.mdb", "/FOLDER1/08h00/qpaie.mdb").Check()
-2
votes

Using the .Net Framework's FTPWebRequest class from PowerShell, with the EnableSsl property to enable FTPS

[Net.ServicePointManager]::ServerCertificateValidationCallback={$true} 
    $Dir = "D:\FolderWithBackupFilesToMove" 
    foreach($item in (dir $dir)) 
    { 
        write-output "————————————–" 
        $fileName = $item.FullName 
        write-output $fileName 
        $ftp = [System.Net.FtpWebRequest]::Create("ftp://some.ftp.server.com/someRootFolder/"+$item.Name) 
        $ftp = [System.Net.FtpWebRequest]$ftp 
        $ftp.UsePassive = $true 
        $ftp.UseBinary = $true 
        $ftp.EnableSsl = $true 
        $ftp.Credentials = new-object System.Net.NetworkCredential("UserName","Password
        $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile 
        $rs = $ftp.GetRequestStream() 

        $reader = New-Object System.IO.FileStream ($fileName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read) 
        [byte[]]$buffer = new-object byte[] 4096 
        [int]$count = 0 
        do 
        { 
            $count = $reader.Read($buffer, 0, $buffer.Length) 
            $rs.Write($buffer,0,$count) 
        } while ($count -gt 0) 
        $reader.Close() 
        $rs.Close() 
        write-output "+transfer completed" 

        $item.Delete() 
        write-output "+file deleted" 
    }