1

早上好!

我已经完成了脚本中的最后一个(也是相当关键的)阶段,即循环从目录中删除文件。我不会假装我在 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

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

4

1 回答 1

2

你的 $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 内容。让您的生活更轻松。

调整这篇文章的解决方案也可能有助于使用 PowerShell 通过 FTP 上传文件

例如:

用于$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进行一些研究后编写了这个函数,并从 Accepted在上面的链接以及@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 服务器上删除指定的文件。

于 2013-01-30T10:16:22.360 回答