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(); 

2 comments:

Anonymous said...

The scripts you have provided for get ftp directory and delete file have reduced my time required to develop from weeks to hours.

Many thanks!

Anonymous said...

You have just saved me days of work trying to figure this stuff out.

Many thanks!