PoshCode Archive  Artifact Content

Artifact bfb7a703d813577209bf5dbc9b6ce1bc1f163fced35c9734a85301b2dcbaa889:

  • File Send-FTP.ps1 — part of check-in [8e2e052801] at 2018-06-10 12:59:22 on branch trunk — A little Send-FTP script to support uploading files. This version is simple: it can’t create folders nor allow you to specify whether to overwrite files … (user: Joel Bennett size: 2538)

# encoding: ascii
# api: powershell
# title: Send-FTP
# description: A little Send-FTP script to support uploading files. This version is simple: it can’t create folders nor allow you to specify whether to overwrite files …
# version: 1.0
# type: function
# author: Joel Bennett
# license: CC0
# function: Send-FTP
# x-poshcode-id: 1625
# x-archived: 2014-01-03T10:19:56
# x-published: 2011-02-03T02:15:00
#
#
   function Send-FTP {
      Param(
         $Server = "ilncenter.net"
      ,  $Credentials = $(Get-Credential)
      ,  [Parameter(ValueFromPipeline=$true)]
         $LocalFile
      ,  $Path = "/"
      ,  $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()
         }
      }
   }