Sunday 26 September 2010

Get-FtpFile.ps1

  1. # 
  2. .SYNOPSIS 
  3.     This script used FTP to get and display a text file. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN Sample 
  6. .NOTESW 
  7.     File Name  : Get-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://pshscripts.blogspot.com/2010/09/get-ftpfileps1.html 
  13.     MSDN sample posted to: 
  14.         http://msdn.microsoft.com/en-us/library/ms229711.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-FtpFile.ps1' 
  17.     This is Hello.Txt from www.reskit.net 
  18.     Have a great day! 
  19.   
  20.     Download Complete, status: 
  21.     226-Maximum disk quota limited to 100000 Kbytes 
  22.         Used disk quota 78232 Kbytes, available 21767 Kbytes 
  23.     226 Transfer complete.     
  24. #> 
  25.  
  26. # Get the object used to communicate with the server. 
  27. $Request = [System.Net.WebRequest]::Create("ftp://www.reskit.net/hello.txt"); 
  28. $Method =  [System.Net.WebRequestMethods+Ftp]::DownloadFile 
  29.  
  30. # This example assumes the FTP site uses anonymous logon. 
  31. # Username/password not real 
  32. $Request.Credentials = New-Object System.Net.NetworkCredential "Anonymous","tfl@psp.co.uk"
  33. $ResponseStream = $Response.GetResponseStream() 
  34.  
  35. # Read and display the text in the file 
  36. $Reader = new-object System.Io.StreamReader $ResponseStream 
  37. [System.Console]::Writeline($Reader.ReadToEnd()) 
  38.  
  39. # Display Status 
  40. "Download Complete, status:" 
  41. $response.StatusDescription  
  42.  
  43. # Close Reader and Response objects 
  44. $Reader.Close() 
  45. $Response.Close() 

No comments: