【发布时间】:2015-06-14 22:51:37
【问题描述】:
我知道这个问题已经被问过好几次了,但我的要求与我在这里和谷歌上读到的略有不同。
我有以下文件夹结构。
c:\weblogs\Servers\Server1
c:\weblogs\Servers\Server2
Server 1 文件夹可能包含以下文件夹:
W3SVC1
W3SVC2
W3SVC3
服务器 2 将具有类似的文件夹结构。
我正在尝试创建一个脚本,该脚本将查看每个 W3SVC* 文件夹中是否存在超过 30 天的 .log 文件。
然后它将在每个这样的 W3SVC* 子文件夹中创建一个名为 Oldlogs-Server*-W3SVC*.zip 的 zip 文件,并将旧日志移动到该文件中。在第二次运行时,它只会将任何超过 30 天的新文件移动到相应的 zip 文件中。
我不是开发人员,但如果没有可用的现有脚本,任何示例或建议都会有所帮助。
这是我认为最接近我想要实现的脚本:
它当前抛出一个错误:
“需要\WebApps\LogHarvest\pkzipc.exe”
# Alias for pkzip
if (-not (test-path "$env:F:\WebApps\LogHarvest\pkzipc.exe")) {
throw "$env:F:\WebApps\LogHarvest\pkzipc.exe needed"
}
set-alias sz "$env:WebApps\LogHarvest\pkzipc.exe"
############################################
#### Variables
$filePath = "F:\Weblogs\Test"
$log = Get-ChildItem -Recurse -Path $filePath | Where-Object { $_.Extension -eq ".log" }
########### END of VARABLES ##################
foreach ($file in $log) {
$name = $file.name
$directory = $file.DirectoryName
$zipfile = $name.Replace(".log",".7z")
sz a -t7z "$directory\$zipfile" "$directory\$name"
}
########### END OF SCRIPT ##########
【问题讨论】:
-
Kev,我已经添加了我尝试过的脚本,我认为它接近我想要做的。 .
标签: powershell zip 7zip