Send-FTP 1.0 (modification of post by Joel Bennett view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1625"></script>download | new post
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 …
- 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()
- }
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository