- <#
- .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://pshscripts.blogspot.com/2010/10/copy-filetoftpps1.html
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/ms229715.aspx
- .EXAMPLE
- PSH [C:\foo]: .Copy-FileToFtp.ps1
- Upload File Complete, status 226
- 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.
- $FileContents = [System.IO.File]::ReadAllBytes("C:\foo\scriptlib.zip")
- $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()
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Sunday, 3 October 2010
Copy-FileToFtp.ps1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment