首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用PowerShell从FTP服务器删除文件

使用PowerShell从FTP服务器删除文件
EN

Stack Overflow用户
提问于 2018-04-05 12:01:33
回答 2查看 6.2K关注 0票数 4

我编写了一个PowerShell脚本,使用FTPto本地机器下载文件。

下载文件后,我想从FTP服务器中删除它。我也写了这段代码。但不幸的是它不起作用。

有人能帮我指出我的代码出了什么问题吗?任何线索都会有帮助..。

这里是我的代码

代码语言:javascript
运行
复制
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根本没有执行..。任何类型的线索都会有帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-06 05:23:04

不能将Test-Path与FTP URL一起使用。因此,删除文件的代码将永远不会执行。

只需删除Test-Path条件并尝试无条件删除该文件。然后检查错误,并视需要处理“文件不存在”错误。

代码语言:javascript
运行
复制
$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
    }
}

尽管在成功下载文件之后才尝试删除该文件,但文件不可能不存在。

因此,您可以考虑放弃任何特定的错误处理。

票数 3
EN

Stack Overflow用户

发布于 2018-04-05 18:31:42

我在本地运行了您的脚本来试用它,并发现了一些问题。我还重构了一些东西,只是为了使它更易读(至少在我看来:)。

问题

  • 第13行. $Source参数有一个ftp://...路径。Test-Path将始终在这里返回$false,并且永远不会执行删除请求。
  • Get-FTPFile中,您不是引用函数的输入参数,而是引用函数外部定义的变量。我不知道这是一个复制粘贴错误还是故意的。在我看来,您应该使用发送给函数的参数。至少在下面的代码中,第38、39和50行。

代码

代码语言:javascript
运行
复制
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对话,不需要从头创建所有东西,除非您需要。

总之,作为参考,你可以去看看。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49671897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档