Showing posts with label System.Net.FtpWebRequest. Show all posts
Showing posts with label System.Net.FtpWebRequest. Show all posts

Monday, 4 October 2010

Remove-FtpFile.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script deletes a file from an FTP Server 
  4. .DESCRIPTION 
  5.     This script is a rewrite of an MSDN Sample 
  6. .NOTES 
  7.     File Name  : Remove-FtpFile.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Remove-FtpFile.ps1 
  17.     Delete status: 250 DELE command successful. 
  18. #> 
  19. $ServerUri = New-Object System.Uri "ftp://www.reskit.net/powershell/foo.txt" 
  20. # The serverUri parameter should use the ftp:// scheme. 
  21. # It contains the name of the server file that is to be deleted. 
  22. # Example: ftp://contoso.com/someFile.txt. 
  23.  
  24. if ($ServerUri.Scheme -ne [system.Uri]::UriSchemeFtp) { 
  25.         " Bad URI"; return 
  26.  
  27. # Get the object used to communicate with the server. 
  28. $request = [system.Net.FtpWebRequest]::Create($serverUri
  29. $request.Method = [System.Net.WebRequestMethods+ftp]::Deletefile 
  30. $Request.Credentials = New-Object System.Net.NetworkCredential "anonymous","tfl@psp.co.uk" 
  31.  
  32. $response = $request.GetResponse() 
  33. "Delete status: {0}" -f $response.StatusDescription 
  34. $response.Close(); 

Sunday, 3 October 2010

Copy-FileToFtp.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script Uploads a text file to an FTP Server using PowerShell.  
  4. .DESCRIPTION 
  5.     This script first creates an FTP 'web' request to upload a file. Then the  
  6.     source file is read from disk and written up to the FTP Server. A response 
  7.     is then displayed. This is a rewrite of an MSDN Sample. 
  8. .NOTES 
  9.     File Name  : Copy-FileToFtp.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://pshscripts.blogspot.com/2010/10/copy-filetoftpps1.html
  15.     MSDN sample posted tot: 
  16.         http://msdn.microsoft.com/en-us/library/ms229715.aspx
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .Copy-FileToFtp.ps1 
  19.     Upload File Complete, status 226
  20.     226 Transfer complete. 
  21. #> 
  22.   
  23. # Get the object used to communicate with the server. 
  24. $Request = [System.Net.FtpWebRequest]::Create("ftp://www.reskit.net/powershell/Greetings.Txt"
  25. $Request.Method = $Request.Method = [System.Net.WebRequestMethods+ftp]::UploadFile 
  26.  
  27. # This example assumes the FTP site uses anonymous logon. 
  28. $Request.Credentials = New-Object System.Net.NetworkCredential "anonymous","tfl@psp.co.uk" 
  29.  
  30. # Copy the contents of the file to the request stream. 
  31. $FileContents = [System.IO.File]::ReadAllBytes("C:\foo\scriptlib.zip"
  32. $Request.ContentLength = $fileContents.Length
  33. $RequestStream = $request.GetRequestStream() 
  34. $RequestStream.Write($FileContents, 0, $FileContents.Length) 
  35. $RequestStream.Close()
  36. $Response = $Request.GetResponse() 
  37. "Upload File Complete, status {0}" -f $Response.StatusDescription 
  38. $Response.Close()