(Powershell)循环从FTP位置删除文件

(Powershell)循环从FTP位置删除文件,powershell,ftp,Powershell,Ftp,早上好 我已经进入了脚本的最后一个阶段(相当关键),即循环删除目录中的文件。我不会假装我对Powershell很在行(远远不是),所以我会把我在网上找到的代码块切碎,即兴创作,希望它能工作 我希望有人能破译我在这里试图做什么,看看我做错了什么 # Clear FTP Directory $DelLoop=1 $server = "www.newsbase.com" $dir = "/usr/local/tomcat/webapps/newsbasearchive/monitors/asiae

早上好

我已经进入了脚本的最后一个阶段(相当关键),即循环删除目录中的文件。我不会假装我对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
我知道“开放连接”部分是有效的,它只是一个循环。它只是不断抱怨运算符放错了位置,当我修复这些运算符时,它不会抛出任何错误,但它也不会做任何事情

昨天我花了四个小时的大部分时间研究这个,我希望你们中的一个能帮我

提前谢谢

增编:

以下是根据要求提供的更多代码:

#   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
#清除现有的.htm文件以避免重复
获取ChildItem-path“\”-recurse-include index.jsp | ForEach对象{
清除内容“index.jsp”
}
#设置.JSP正文的第一部分
$HTMLPart1=“

以下是指向最近3个AsiaElec PDF的链接:

    ” #在目录中递归,3次查找3个最新的.PDF文件 $Directory=“C:\PDFs” $htmlineprefix=“
  • ”+$\ux.name+$htmlinesuffix $Loop++ } #JSP文件汇编 获取内容“header.html”>>“index.jsp” $HTMLPart1>>“index.jsp” $LineParse=“” $2=1 对于($Loop2=1;$Loop2-le 3;3) { $HTMLINE[$Loop2]>>“index.jsp” $2++ } 获取内容“tail.html”>>“index.jsp” #准备文件列表 $FileList=@(1,2,3,4,5) $FileList[2]=$PDF[2] $FileList[3]=$PDF[3] $FileList[4]=“index.jsp” #清除FTP目录 $DelLoop=1 $server=“www.newsbase.com” $dir=“/usr/local/tomcat/webapps/newsbasearchive/monitors/asiaelec/” “打开$server 用户derek bland1ne 二元的 cd$dir " +( 对于($DelLoop=1;$DelLoop-le 5;5) { $FileList[$DelLoop]|%{“delete”“$\u”“` n”} $DelLoop++ })|ftp-i-in

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

您的$dir路径看起来像是在unix系统上,因此这可能有点不同,但您需要做的只是稍微更改一下最终循环:

For ($DelLoop=1; $DelLoop -le 5; $DelLoop++)
{
    $FileList[$DelLoop] | % { rm $FileList[$DelLoop] }
} 
这是假设$FileList包含要删除的文件,而不仅仅是(我猜是虚拟的)数字。我还建议您下载@Graimer提到的模块,然后将其放入WindowsPowerShell>Modules>%ModuleFolder%>%Module.psm1%中,并从您的个人资料中导入

然后,您可以使用
PS>Remove FTPItem-Path”/myFolder-Recurse
删除FTP内容。让你的生活更轻松

调整此帖子的解决方案也可能有所帮助

e、 g:

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

$response=$ftp.GetResponse()
,以了解事情是否进展顺利

编辑

在这里做了一些研究,并根据上面链接中接受的答案以及@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服务器上删除指定的文件。

为什么不看看PS模块呢?我已经查看并尝试了实际的PowerShell方法,但我无法理解它,也无法让它工作,尽管所有参数都是正确的。我已经尝试过这段代码,它很有效,现在我只需要将它整合到我的大部分脚本中就行了。kk,只是一个建议。该模块比手动使用.Net类容易得多:-)该.Net类正是我所尝试的,它们让人非常困惑:(它们似乎不想做我想做的事。我说的是该模块(一组函数)使使用.NET变得更容易。这些函数的工作方式类似于
Get ChildItem
(dir)等。啊,谢谢你-但是你看,脚本将在30多台计算机上运行,而不仅仅是我的计算机,让我所有的人下载并安装(很可能必须支持它们)听起来更像是一件家务事:(我明白了,在这种情况下,试着看看我发布的链接。它为你提供了一些进入和使用ftp服务器的代码。从中删除只需稍作调整。我给你的小更改将删除你本地的内容。哈哈,这很有趣,这是我尝试过的解决方案,但无法开始工作…看看解决方案有7票赞成。这看起来非常熟悉。对:)但我指的是被接受的答案,有22票赞成:)你试过那个吗?我试过了,就是那个。我无法让它工作,它会一直给我FTP错误,即使所有参数都是正确的。