簡體   English   中英

使用PowerShell從FTP服務器刪除文件

[英]Deleting file from FTP server using PowerShell

我寫了一個PowerShell腳本,使用FTP將文件下載到我的本地機器上。

下載文件后,我想從FTP服務器中刪除它。 我也寫了這段代碼。 但不幸的是它沒有用。

任何人都可以幫我指出我的代碼有什么問題嗎? 任何線索都會有所幫助......

這是我的代碼

function Delete-File($Source,$Target,$UserName,$Password)
{

    $ftprequest = [System.Net.FtpWebRequest]::create($Source)
    $ftprequest.Credentials =  New-Object System.Net.NetworkCredential($UserName,$Password)

    if(Test-Path $Source)
    {
       "ABCDEF File exists on ftp server."
       $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
       $ftprequest.GetResponse()

       "ABCDEF File deleted."
    }

}

function Get-FTPFile ($Source,$Target,$UserName,$Password)  
{  

    # Create a FTPWebRequest object to handle the connection to the ftp server  
    $ftprequest = [System.Net.FtpWebRequest]::create($Source)  

    # set the request's network credentials for an authenticated connection  
    $ftprequest.Credentials =  
    New-Object System.Net.NetworkCredential($username,$password)  
    if(Test-Path $targetpath)
    {
        "ABCDEF File exists"
    }
    else 
    { 
        "ABCDEF File downloaded"
         $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile  

         $ftprequest.UseBinary = $true  
         $ftprequest.KeepAlive = $false  
         Delete-File $sourceuri $targetpath $user $pass
    }

    # send the ftp request to the server  
    $ftpresponse = $ftprequest.GetResponse()  

    # get a download stream from the server response  
    $responsestream = $ftpresponse.GetResponseStream()  

    # create the target file on the local system and the download buffer  
    $targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create)  
    [byte[]]$readbuffer = New-Object byte[] 1024  

    # loop through the download stream and send the data to the target 
    file  
    do{  
          $readlength = $responsestream.Read($readbuffer,0,1024)  
          $targetfile.Write($readbuffer,0,$readlength)  
    }  
    while ($readlength -ne 0)  

    $targetfile.close()  
}  

$sourceuri = "ftp://ftpxyz.com/vit/ABCDEF.XML"  
$targetpath = "C:\Temp\M\NEWFOLDER\ABCDEF.XML"  
$user = "*******"  
$pass = "*******"  
Get-FTPFile $sourceuri $targetpath $user $pass 
Delete-File $sourceuri $targetpath $user $pass

每次我執行這個腳本時,我得到的唯一聲明

已下載ABCDEF文件

要么

ABCDEF文件存在

我猜Delete-File根本沒有執行...任何類型的線索都會有所幫助。

我在本地運行你的腳本試試看,發現了一些問題。 我重構了一些事情只是為了讓它更具可讀性(至少在我看來:))。

問題

  • 第13行。 $Source參數有一個ftp://...路徑。 Test-Path將始終返回$false ,並且永遠不會執行刪除請求。
  • Get-FTPFile您沒有引用函數的輸入參數,而是在其外部定義的變量。 我不知道這只是一個復制和粘貼錯誤或故意。 在我看來,你應該使用你發送給函數的參數。 第38,39和50行至少在我的下面的代碼中。

function Delete-File
{  
   param(
       [string]$Source,
       [string]$Target,
       [string]$UserName,
       [string]$Password
   ) 

   $ftprequest = [System.Net.FtpWebRequest]::create($Source)
   $ftprequest.Credentials =  New-Object System.Net.NetworkCredential($UserName,$Password)

   if(Test-Path $Source)
   {
      "ABCDEF File exists on ftp server."
      $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
      $ftprequest.GetResponse()

      "ABCDEF File deleted."
   }

}

function Get-FTPFile
{  
   param(
       [string]$Source,
       [string]$Target,
       [string]$UserName,
       [string]$Password
   ) 

   # Create a FTPWebRequest object to handle the connection to the ftp server  
   $ftprequest = [System.Net.FtpWebRequest]::create($Source)  

   # set the request's network credentials for an authenticated connection  
   $ftprequest.Credentials =  
   New-Object System.Net.NetworkCredential($UserName,$Password)  
   if(Test-Path $Target)
   {
       "ABCDEF File exists"
   }
   else 
   { 
       "ABCDEF File downloaded"
        $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile  

        $ftprequest.UseBinary = $true  
        $ftprequest.KeepAlive = $false  
        Delete-File $Source $Target $UserName $Password
  }

  # send the ftp request to the server  
  $ftpresponse = $ftprequest.GetResponse()  

  # get a download stream from the server response  
  $responsestream = $ftpresponse.GetResponseStream()  

  # create the target file on the local system and the download buffer  
  $targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create)  
  [byte[]]$readbuffer = New-Object byte[] 1024  

  # loop through the download stream and send the data to the target 
  file  
  do{  
        $readlength = $responsestream.Read($readbuffer,0,1024)  
        $targetfile.Write($readbuffer,0,$readlength)  
  }  
  while ($readlength -ne 0)  

  $targetfile.close()  
}  

$sourceuri = "ftp://ftpxyz.com/vit/ABCDEF.XML"  
$targetpath = "C:\Temp\M\NEWFOLDER\ABCDEF.XML"  
$user = "*******"  
$pass = "*******"   
Get-FTPFile $sourceuri $targetpath $user $pass 
#Delete-File $sourceuri $targetpath $user $pass

還有現成的PowerShell cmdlet用於與FTP / SFTP通信,除非您需要,否則無需從頭開始創建所有內容。

無論如何,作為參考,請查看例如

您不能將Test-Path與FTP URL一起使用。 因此,刪除文件的代碼將永遠不會執行。

只需刪除Test-Path條件並嘗試無條件地刪除該文件。 然后檢查錯誤並根據需要處理“文件不存在”錯誤。

$ftprequest = [System.Net.FtpWebRequest]::create($Source)
$ftprequest.Credentials =  New-Object System.Net.NetworkCredential($UserName, $Password)

try
{
   $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
   $ftprequest.GetResponse() | Out-Null

   Write-Host ("File {0} deleted." -f $Source)
}
catch
{
    if ($_.Exception.InnerException.Response.StatusCode -eq 550)
    {
        Write-Host ("File {0} does not exist." -f $Source)
    }
    else
    {
        Write-Host $_.Exception.Message
    }
}

雖然只有在成功下載文件后才嘗試刪除文件,但實際上文件不可能存在。

因此,您可以考慮放棄任何特定的錯誤處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM