none
Upload/Download de fichier sur FTP Free RRS feed

  • Discussion générale

  • Bonjour.

    Je possède un espace de stockage chez FREE. Pour y accéder j'utilise un utilitaire type FileZilla. J'entre l'adresse du ftp (ftpperso.free.fr) et le nom d'utilisateur/mot de passe.

    J'aimerais ne pas avoir à utiliser ça et utiliser uniquement PowerShell pour l'envoi de fichier ZIP.

    J'ai ce script mais qui ne fonctionne pas. Est-ce possible de faire ça déjà et si oui, une solution serais appréciée !

    Un grand merci d'avance !!!

       function Global:Send-FTP {
          
          $Server = "ftpperso.free.fr"
          $Credentials = $(Get-Credential)
          
          $LocalFile ="C:\test.zip"
          $Path = "C:\test.zip"
          $RemoteFile = $(Split-Path $LocalFile -Leaf)
          $ParentProgressId = -1 ## Just ignore this ;)
          $ProgressActivity = "Uploading $LocalFile"
          
          Process {
             ## Assert the existence of the file in question
             if( -not (Test-Path $LocalFile) ) {
                Throw "File '$LocalFile' does not exist!"
             }
    
             ## Create the server string (and make sure it uses forward slashes and ftp://)
             $upload = "ftp://" + $Server + ( Join-Path (Join-Path "/" $Path) $RemoteFile ) -replace "\\", "/"
             #$Upload = $upload
             $total = (gci $LocalFile).Length
    
             Write-Debug $upload
             ## Create FTP request
             $request = [Net.FtpWebRequest]::Create($upload)
    
             ## NOTE: we do not create a folder here...
             # [System.Net.WebRequestMethods+Ftp]::MakeDirectory
             $request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
             $request.Credentials = $Credentials.GetNetworkCredential()
             $request.UsePassive = $true
             $request.UseBinary = $true
             $request.KeepAlive = $false
    
             try {
                ## Load the file
                $read = [IO.File]::OpenRead( (Convert-Path $LocalFile) )
                $write = $request.GetRequestStream();
                
                $buffer = new-object byte[] 20KB
                $offset = 0
                $progress = 0
    
                do {
                   $offset = $read.Read($buffer, 0, $buffer.Length)
                   $progress += $offset
                   $write.Write($buffer, 0, $offset);
                   Write-Progress $ProgressActivity "Uploading" -Percent ([int]($progress/$total * 100)) -Parent $ParentProgressId
                } while($offset -gt 0)
            
             } finally {
                Write-Debug "Closing Handles"
                $read.Close()
                $write.Close()
             }
          }
       }
    

     


    lundi 17 octobre 2011 13:03

Toutes les réponses

  • Bonjour Arnal,

    Vous avez consulté : http://msdn.microsoft.com/en-us/library/ms229715.aspx, la partie Power Shell :

    <#
    .SYNOPSIS
    This script Uploads a text file to an FTP Server using PowerShell.
    .DESCRIPTION
    This script first creates an FTP 'web' request to upload a file. Then the
    source file is read from disk and written up to the FTP Server. A response
    is then displayed. This is a rewrite of an MSDN Sample.
    .NOTES
    File Name : Copy-FileToFtp.ps1
    Author : Thomas Lee - tfl@psp.co.uk
    Requires : PowerShell Version 2.0
    .LINK
    This script posted to:
    http://www.pshscripts.blogspot.com
    MSDN sample posted tot:
    http://
    .EXAMPLE
    PSH [C:\foo]: .Copy-FileToFtp.ps1
    Upload File Complete, status 226-Maximum disk quota limited to 100000 Kbytes
    Used disk quota 78237 Kbytes, available 21762 Kbytes
    226 Transfer complete.
    #>

    # Get the object used to communicate with the server.
    $Request = [System.Net.FtpWebRequest]::Create("ftp://www.reskit.net/powershell/Greetings.Txt")
    $Request.Method = $Request.Method = [System.Net.WebRequestMethods+ftp]::UploadFile

    # This example assumes the FTP site uses anonymous logon.
    $Request.Credentials = New-Object System.Net.NetworkCredential "anonymous","tfl@psp.co.uk"

    # Copy the contents of the file to the request stream.
    $SourceStream = New-Object System.IO.StreamReader "c:\foo\ftpgreetings.txt"
    $FileContents = [System.Text.Encoding]::UTF8.Getbytes($sourceStream.ReadToEnd())
    $SourceStream.Close();
    $Request.ContentLength = $fileContents.Length;

    $RequestStream = $request.GetRequestStream()
    $RequestStream.Write($FileContents, 0, $FileContents.Length);
    $RequestStream.Close();
    $Response = $Request.GetResponse()
    "Upload File Complete, status {0}" -f $Response.StatusDescription
    $Response.Close();

    Cordialement,

    Roxana


    Roxana PANAIT, MSFT       Join TechNetFr on Viadeo   Votez! Appel à la contribution
    Nous vous prions de considérer que dans le cadre de ce forum on n’offre pas de support technique et aucune garantie de la part de Microsoft ne peut être offerte.

    mercredi 26 octobre 2011 06:53