Sunday, 26 September 2010

Get-FTPDirectory.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script used FTP to get and display the root of an FTP site. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample. 
  6. .NOTESW 
  7.     File Name  : Get-FtpDirectory.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         htthttp://pshscripts.blogspot.com/2010/09/get-ftpdirectoryps1.html 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/ms229716.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-FtpDirectory.ps1 
  17.     drwxrwxrwx   1 user     group           0 Dec  4  2005 pcpro 
  18.     drwxrwxrwx   1 user     group           0 Sep 23 15:18 PowerShell 
  19.     ... {Listing truncated} 
  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. # Get the object used to communicate with the server. 
  26. $Request = [System.Net.WebRequest]::Create("ftp://www.reskit.net"
  27. $Request.Method =  [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails 
  28.  
  29. # This example assumes the FTP site uses anonymous logon. 
  30. # Username/password not real 
  31. $Request.Credentials = New-Object System.Net.NetworkCredential "Anonymous",tfl@psp.co.uk 
  32.  
  33. $Response = $Request.GetResponse() 
  34. $ResponseStream = $Response.GetResponseStream() 
  35.  
  36. # Read and display the text in the file 
  37. $Reader = new-object System.Io.StreamReader $Responsestream 
  38. [System.Console]::Writeline($Reader.ReadToEnd()) 
  39.  
  40. # Display Status 
  41. "Download Complete, status:" 
  42. $response.StatusDescription  
  43.  
  44. # Close Reader and Response objects 
  45. $Reader.Close() 
  46. $Response.Close() 

2 comments:

Unknown said...

any ideas why this doesn´t work for iis7.5

Thomas Lee said...

No idea. I tested this out on a couple of servers and all was well. Take a look at a netmon trace of the conversation - I suspect the issue is with the FTP server in Windows and its setup, vs an error in .NET or my script. If you provide me with a few more details I can take a deeper look.

Thanks for the post!