2017-07-27 15 views
1

我的公司有一箇舊的批處理腳本,每15分鐘運行一次,並從兩臺服務器收集事件日誌,並將其放置在開發人員可以訪問它的位置進行分類。文件刪除應該離開7天的文件,但只剩下5 ...爲什麼?

它創造並維持每個服務器的事件日誌文件(採用.evtx)一整天,然後開始一個新的第二天,等

我最近重新寫的批與PowerShell的並添加了一些文件夾清理,文件壓縮等,但這只是部分工作。

問題:

腳本應該檢查在過去7檔的基礎上,創建日期,而忽略他們。它應該刪除第8,9,10等處的任何東西。相反,它只在一個文件夾(對於一臺服務器)中保留5個文件,在另一個文件夾(對於另一個服務器)中保留4個文件。我不知道爲什麼。我注意到的一個或兩個問題的另一個問題是,它會刪除列表中的第四個位置的文件,但忽略第5個,然後刪除第6個,等等。

我不確定拉鍊部分設置爲60天,因爲我的劇本只運行了大約20-25天。

代碼:

# Cleaning up LogFile folder 

Write-Output "Cleaning up existing *.evtx, *.zip and *.7z files to ensure efficient disk space usage..." | Out-File $HistFile -append 
Write-Output " " | Out-File $HistFile -append 

# Now cleaning up event logs at an interval of 7 days 

$EventLogsCount = Get-ChildItem $Path -Recurse -Exclude *.zip, *.7z, *.ps1, *.txt | Where {-not $_.PsIsContainer} | Sort CreationTime -desc | Select -Skip 7 | %{$_.Count} 

if ($EventLogsCount -eq $null) 
{ 
    Write-Output "No event logs to remove..." | Out-File $HistFile -append 
    Write-Output " " | Out-File $HistFile -append 
} 
else 
{ 
    Write-Output "Removing the following event log files:" | Out-File $HistFile -append 
    Write-Output " " | Out-File $HistFile -append 
    Get-ChildItem $Path -Recurse -Exclude *.zip, *.7z, *.ps1, *.txt | Where {-not $_.PsIsContainer} | Sort CreationTime -desc | Select -Skip 7 | foreach { 
    $_ | Remove-Item -Recurse 
     if (Test-Path $_) 
     { 
      "Failed to remove $_" 
     } 
     else 
     { 
      "$_" 
     } 
    } | Out-File $HistFile -append 
    Write-Output " " | Out-File $HistFile -append 
} 

# Cleaning up zips at a greater interval of 60 days 

$ZipsCount = Get-ChildItem $Path -Recurse -Exclude *.evtx, *.ps1, *.txt | Where {-not $_.PsIsContainer} | Sort CreationTime -desc | Select -Skip 60 | %{$_.Count} 

if ($ZipsCount -eq $null) 
{ 
    Write-Output "No zipped files to remove..." | Out-File $HistFile -append 
} 
else 
{ 
    Write-Output "Removing the following zipped files:" | Out-File $HistFile -append 
    Write-Output " " | Out-File $HistFile -append 
    Get-ChildItem $Path -Recurse -Exclude *.evtx, *.ps1, *.txt | Where {-not $_.PsIsContainer} | Sort CreationTime -desc | Select -Skip 60 | foreach { 
    $_ | Remove-Item -Recurse 
     if (Test-Path $_) 
     { 
      "Failed to remove $_" 
     } 
     else 
     { 
      "$_" 
     } 
    } | Out-File $HistFile -append 
    Write-Output " " | Out-File $HistFile -append 
} 

回答

1

你的邏輯有點靠不住。目前,您正在收集文件列表,並根據其排序的創建時間跳過x個文件作爲整體。您可以使用Get-ChildItem-Include標誌,而不是排除其他所有內容。我已經重寫了腳本,使其更易於閱讀和實用。它着眼於根據您的門檻文件的最後寫的時間和過濾器(7天爲事件日誌,或60天爲zip文件)


劇本改寫預期功能:

# Log script functionality 
"Cleaning up existing *.evtx, *.zip and *.7z files to ensure efficient disk space usage...`r`n" >> $HistFile 

# Now cleaning up event logs at an interval of 7 days 
$EventLogs = GCI $Path -Include *.evtx -Recurse | 
       ? { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } 

If (!$EventLogs) { 
    "No event logs to remove...`r`n" >> $HistFile 
} Else { 
    "Removing the following event log files:`r`n" >> $HistFile 
    $EventLogs | 
     % { 
      Try { 
       Remove-Item $_ -EA Stop 
       $_.FullName >> $HistFile 
      } Catch { 
       "Failed to remove $($_.FullName)" >> $HistFile 
      } 
     } 
} 

# Cleaning up zips at a greater interval of 60 days 
$ZipFiles = GCI $Path -Include *.zip,*.7z -Recurse | 
       ? { $_.LastWriteTime -lt (Get-Date).AddDays(-60) } 
If (!$ZipFiles) { 
    "No zipped files to remove..." >> $HistFile 
} Else { 
    "Removing the following zipped files:`r`n" >> $HistFile 
    $ZipFiles | 
     % { 
      Try { 
       Remove-Item $_ -EA Stop 
       $_.FullName >> $HistFile 
      } Catch { 
       "Failed to remove $($_.FullName)" >> $HistFile 
      } 
     } 
} 
+0

感謝@ TheIncorrigible1 。我仍然在學習PS(自學),所以總是很好 學習其他方式做某事。我會調整我的情況並報告回來(可能明天因爲工作今天很熱,很重)。非常感激。 – Ilya

+0

因此,@TheIncorrigible1,你的劇本結束了工作,雖然它由於某種原因設置爲-7,所以留下了一個額外的文件,所以我每天只剩8個......沒什麼大不了的,我可以將它改爲-6,有7 ... 從來沒有想過使用lastwritetime vs創作時間,所以我明白這一點。 – Ilya

相關問題