【问题标题】:(Powershell) Loop to delete files from an FTP Location(Powershell) 循环从 FTP 位置删除文件
【发布时间】:2013-01-30 08:39:17
【问题描述】:

早上好!

我已经完成了脚本的最后一个(也是相当关键的)阶段,即循环删除目录中的文件。我不会假装我在 Powershell 方面知识渊博(远非如此),所以我有点像是在切碎我在网上找到的代码块,即兴创作并希望它有效。

我希望有人能破译我在这里尝试做的事情,看看我做错了什么!

#   Clear FTP Directory
$DelLoop=1
$server = "www.newsbase.com"
$dir = "/usr/local/tomcat/webapps/newsbasearchive/monitors/asiaelec/"
"open $server
user Canttell Youthis
binary  
cd $dir    
" +(
For ($DelLoop=1; $DelLoop -le 5; 5)
    {
        $FileList[$DelLoop] | %{ "delete ""$_""`n" }
        $DelLoop++
    })| ftp -i -in

我知道“打开连接”部分有效,它只是循环。它只是不断抱怨放错位置的运算符,当我修复这些时,它不会抛出任何错误 - 但它也不会做任何事情。

我昨天花了 4 个小时来研究这个问题,我希望你们中的一个可以帮助我。

提前致谢!

附录:

这里是更多的代码,根据要求:

#   Clear existing .htm file to avoid duplication
Get-ChildItem -path ".\" -recurse -include index.jsp | ForEach-Object {
Clear-Content "index.jsp"
}

#   Set first part of .JSP Body
$HTMLPart1="</br><tr><td colspan=9 align=center><p style=""font-family:Arial"">Here are the links to the last 3 AsiaElec PDFs:</br><ul>"

#   Recurse through directory, looking for 3 most recent .PDF files 3 times
$Directory="C:\PDFs"
$HTMLLinePrefix="<li><a style=""font-family:Arial""href="""
$HTMLLineSuffix="</a></li>"
$HTMLLine=@(1,2,3,4)
$Loop=1
$PDF=@(1,2,3,4)
Get-ChildItem -path $Directory -recurse -include *.pdf | sort-object -Property LastWriteTime -Descending | select-object -First 3 | ForEach-Object {
        $PDF[$Loop]=$_.name
        $HTMLLine[$Loop]=$HTMLLinePrefix + $_.name + """>" + $_.name + $HTMLLineSuffix
        $Loop++
    }

#   Final .JSP File Assembly
Get-Content "header.html" >> "index.jsp"
$HTMLPart1 >> "index.jsp"
$LineParse=""
$Loop2=1
For ($Loop2=1; $Loop2 -le 3; 3)
    {
        $HTMLLine[$Loop2] >> "index.jsp"
        $Loop2++
    }
Get-Content "tail.html" >> "index.jsp"

#   Prepare File List
$FileList=@(1,2,3,4,5)
$FileList[2]=$PDF[2]
$FileList[3]=$PDF[3]
$FileList[4]="index.jsp"

#   Clear FTP Directory
$DelLoop=1
$server = "www.newsbase.com"
$dir = "/usr/local/tomcat/webapps/newsbasearchive/monitors/asiaelec/"
"open $server
user derek bland1ne
binary  
cd $dir    
" +(
For ($DelLoop=1; $DelLoop -le 5; 5)
    {
        $FileList[$DelLoop] | %{ "delete ""$_""`n" }
        $DelLoop++
    })| ftp -i -in

这还不是全部,但我相信它包含所有相关信息。

【问题讨论】:

  • 为什么不看看 PS 模块呢? gallery.technet.microsoft.com/scriptcenter/…
  • 我已经查看并尝试了实际的 PowerShell 方法,但我根本无法理解它,也无法让它工作,尽管所有参数都是正确的。我已经尝试过这段代码,它可以工作,现在我只需要将它合并到我的大部分脚本中就可以工作。
  • kk,只是一个建议。该模块使手动使用 .Net 类变得更加容易:-)
  • .Net 类正是我尝试过的,它们令人困惑:(它们似乎不想做我想做的事
  • 阅读评论。我说模块(功能的集合)使它变得更容易,然后使用.NET。这些功能就像Get-ChildItem (dir) 等一样工作。

标签: powershell ftp


【解决方案1】:

你的 $dir 路径看起来像你在一个 unix 系统上,所以这可能有点不同,但你需要做的就是稍微改变你的最终循环:

For ($DelLoop=1; $DelLoop -le 5; $DelLoop++)
{
    $FileList[$DelLoop] | % { rm $FileList[$DelLoop] }
} 

这是假设 $FileList 包含您要删除的文件,而不仅仅是(我猜是虚拟的)数字。我还建议您下载 @Graimer 提到的模块,然后将其放入 WindowsPowerShell > Modules > %ModuleFolder% > %Module.psm1% 并从您的配置文件中导入。

然后您可以使用PS&gt; Remove-FTPItem -Path "/myFolder" -Recurse 删除您的 FTP 内容。让您的生活更轻松。

调整这篇文章的解决方案也可能有助于Upload files with FTP using PowerShell

例如:

使用$ftp.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile删除文件,

$response = $ftp.GetResponse() 了解事情是否顺利。

编辑

在从这里http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/17a3abbc-6144-433b-aadd-1f776c042bd5 进行了一些研究并改编了上面链接中接受的答案以及@Graimer 谈到的模块中的代码后编写了这个函数。

function deleteFTPSide 
{
    Param(
        [String] $ftpUserName = "muUserName",
        [String] $ftpDomain = "ftp.place.com", # Normal domains begin with "ftp" here
        [String] $ftpPassword = "myPassword",
        [String] $ftpPort = 21, # Leave as the default FTP port
        [String] $fileToDelete = "folder.domain.com/subfolder/file.txt"
    )

    # Create the direct path to the file you want to delete
    [String] $ftpPath = "ftp://"+"$ftpUserName"+":"+"$ftpPassword@$ftpDomain"+":"+"$ftpPort/$fileToDelete"

    # create the FtpWebRequest and configure it
    $ftp = [System.Net.FtpWebRequest]::Create($ftpPath)

    $ftp.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile

    $ftp.Credentials = new-object System.Net.NetworkCredential($ftpUserName,$ftpPassword)

    $ftp.UseBinary = $true
    $ftp.UsePassive = $true

    $response = [System.Net.FtpWebResponse]$ftp.GetResponse()
    $response.Close()
}

诚然,这不是编写的最优雅的解决方案之一,但我已经对其进行了测试,它可以从 FTP 服务器上删除指定的文件。

【讨论】:

  • 啊,谢谢 - 但是你看,脚本将在 30 多台计算机上运行,​​而不仅仅是我的,并且让我所有的人下载并安装(并且很可能必须支持他们)听起来更多像一件苦差事:(
  • 我明白了,在这种情况下,请尝试查看我发布的链接。它为您提供了一些进入和使用 ftp 服务器的代码。只需稍作调整即可从中删除。我给你的小改动会删除你本地的东西。
  • 哈哈,很好笑,这就是我尝试的解决方案,但无法工作... 7票赞成的解决方案。它看起来非常熟悉。
  • True :) 但我指的是接受的答案,有 22 票赞成 :) 你试过那个吗?
  • 我有,这是我试过的。我无法让它工作,它会一直给我 FTP 错误,即使所有参数都是正确的。
猜你喜欢
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 2022-11-14
  • 2018-07-25
  • 1970-01-01
  • 1970-01-01
  • 2013-07-11
  • 1970-01-01
相关资源
最近更新 更多