在打开/关闭文件夹压缩的情况下在我们的 Windows 2003/2008 上运行以下命令时,我看到剩余的零字节文件..有些奇怪..
日志是 Apache Tomcat 日志。它们以 .0 或 1 结尾,看起来像是剩余的零字节文件。
请注意不要使用此功能-更正的解决方案在我下面的答案中!!
function create7zip([String] $aZip, [String] $aDir, $DTNew){
If ((Get-Item "$aDir").length -eq 0) {
Remove-Item -Force $aDir
} else {
[string]$pathToZipExe = "D:\WORK\SCRIPTS\7za.exe";
[Array]$arguments = "a", "-tzip", "$aZip", "$aDir", "-r";
& $pathToZipExe $arguments;
Start-Sleep -s 3
if ($DTNew) {
(dir $aZip).lastwritetime = $DTNew
}
Remove-Item -Force $aDir
}
}
并被“称为”:
# 3 ARCHIVE LOGS
$server = gc env:computername
$date = Get-Date -f yyyy-MM-dd
$ZipFolder = "D:\logs\ArchiveTemp\"
$ZipFolder2 = "D:\logs\ArchiveTemp\AudForen\"
$ZipFolder3 = "D:\logs\ArchiveTemp\Tomcat\"
$company = "COMPANY"
$DataCenter = "ANYTOWN"
# LOAD IN THE FUNCTION TO DETECT TOMCAT INFO
$ScriptDir = Split-Path $MyInvocation.MyCommand.Path
. ( Join-Path $ScriptDir tomcat.ps1 )
. ( Join-Path $ScriptDir sftp.ps1 )
. ( Join-Path $ScriptDir zip.ps1 )
. ( Join-Path $ScriptDir testlock.ps1 )
$returnvalue = TomcatLog
$LogLocation = $returnvalue[2] # LogDir
if (Test-Path -Path $LogLocation)
{
$files = Get-ChildItem $LogLocation
foreach ($file in $files)
{
$FLName = $file.Name
$FLPath = $file.FullName
$CharacterCount = [regex]::matches($FLName, "-").count
if ($CharacterCount -gt 1)
{
$YearVar = $FLName.Split("-")[0]
$YearVar = $YearVar.Substring($YearVar.Length-4, 4)
$MonthVar = $FLName.Split("-")[1]
$DayVar = $FLName.Split("-")[2]
$DateVar = $YearVar + "-" + $MonthVar + "-" + $DayVar
$DateVar = $DateVar.Substring(0, 10)
$DateAdNm = Get-Date (Get-Date $DateVar).AddDays(1) -format yyyyMMdd
$DateAd = Get-Date (Get-Date $DateVar).AddDays(1) -format yyyy-MM-dd
if (!($DateVar -eq $date))
{
if ($FLName -match 'audit|forensic')
{
If (!(TestFileLock $FLPath -eq -true))
{
$AuditLogZip = $ZipFolder2 + "{0:yyyy-MM-dd}_{1}_{2}@{3}.zip" -f $DateAdNm, $company, $server, $DataCenter
#compress $AuditLogZip $FLPath $DateAd
create7zip $AuditLogZip $FLPath $DateAd
SFTP $AuditLogZip
}
}
elseif ($CharacterCount -gt 2)
{
$HourVar = $FLName.Split("-")[3]
if ($HourVar -gt 11)
{
$HourVar = $HourVar - 12
$HourVar = $HourVar.ToString("00")
}
if ($HourVar -gt 0)
{
$Var = $MonthVar + "-" + $DayVar + "-" + $YearVar +"-"+$HourVar+"-00-00-compressed.zip"
}
If (!(TestFileLock $FLPath -eq -true))
{
#$HourlyZip = $ZipFolder + "{0:yyyy-MM-dd}-{1}{2}-00-00-compressed.zip" -f $server, ( Get-Date($DateVar) -format yyyy-MM-dd )
#compress $HourlyZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
#Write-host $HourlyZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
$HourlyZip = $ZipFolder + "{0}-{1}" -f $server, $Var
#compress $HourlyZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
create7zip $HourlyZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
}
}
else
{
If (!(TestFileLock $FLPath -eq -true))
{
$TomCatZip = $ZipFolder3 + "{0}-Tomcat-{1:yyyy-MM-dd}-compressed.zip" -f $server, ( Get-Date($DateVar) -format yyyy-MM-dd )
#compress $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
#Write-host $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
create7zip $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
}
}
}
}
else
{
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.AddHours(-($CurrentDate.Hour))
$CurrentDate = $CurrentDate.AddMinutes(-($CurrentDate.Minute))
$fileaffected = Get-ChildItem $FLPath | select LastWriteTime
If ($fileaffected.LastWriteTime -lt $CurrentDate) {
#write-host "we are going to move the file $FLPath $fileaffected.LastWriteTime"
If (!(TestFileLock $FLPath -eq -true))
{
$TomCatZip = $ZipFolder3 + "{0}-Tomcat-{1:yyyy-MM-dd}-compressed.zip" -f $server, ( Get-Date($DateVar) -format yyyy-MM-dd )
#compress $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
create7zip $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
Write-host $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
}
#} else {
#write-host "we are going to leave the file $FLPath $fileaffected.LastWriteTime"
}
#If ($file.LastWriteTime -lt $DatetoMove) {
# $TomCatZip = $ZipFolder3 + "{0}-Tomcat-{1:yyyy-MM-dd}-compressed.zip" -f $server, ( Get-Date($DateVar) -format yyyy-MM-dd )
# compress $TomCatZip $FLPath ( Get-Date($DateVar) -format yyyy-MM-dd )
# }
}
}
}
尝试了几个版本的 7-Zip.. D:\WORK\Scripts>7z
7-Zip 9.22 beta 版权所有 (c) 1999-2011 Igor Pavlov 2011-04-18
以及:D:\WORK\Scripts>7za
7-Zip (A) 9.20 版权所有 (c) 1999-2010 Igor Pavlov 2010-11-18
注意 - 我们正在运行 Tomcat 而不是 IIS(没有 ISAPI 重定向) - 因此这不相关: http: //support.microsoft.com/kb/817442但我们确实打开了文件夹压缩。
关闭文件夹压缩,我们仍然可以看到零字节文件。
怀疑是 VirusScan 妨碍了我,但我可以在 VBS 中运行类似的代码,而且效果很好。
让我远离 VBS 的主要驱动力是公司已经对代码签名 vbs 提出了要求,而 PowerShell 被排除在外。
谢谢,
肯特