【发布时间】: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