2017-07-27 2 views
1

私の会社には15分ごとに実行される古いバッチスクリプトがあり、2台のサーバーからイベントログを収集し、devsがトリアージ目的でアクセスできる場所に配置しました。ファイルの削除は7日間のファイルを残しておく必要がありますが、5を残すだけです...なぜですか?

それは私が最近、バッチからのPowerShellにそれを再書い

など、作成され、全体の日のために各サーバーのイベントログファイル(.evtx)を維持し、次の日に新しいものを始めましたいくつかのフォルダのクリーンアップ、ファイルの圧縮などを追加しましたが、それは部分的にしか動作しません。

問題:

スクリプトの作成日に基づいて、最後の7つのファイルをチェックし、それらを無視しなければなりません。それは、8th、9th、10thなどのスポットにあるものはすべて削除する必要があります。代わりに、1つのフォルダ(1つのサーバー用)と4つのファイル(別のサーバー用)に5つのファイルしか残しません。どうしてか分かりません。私が1回か2回気づいた別の問題は、リストの4番目のファイルを削除し、5番目を無視して6番目などを削除することです。

ジップについてはわかりません私のスクリプトは約20〜25日間しか実行されていないため、60日間に設定されています。

コード:

# 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フラグを使用できます。私はスクリプトをより読みやすく機能的に書き直しました。目的の機能のために書き換えられたことは、(zipファイルのイベントログの7日、または60日)あなたのしきい値に基づいて、ファイルの最後に書き込まれた時間とフィルターを見


スクリプト:TheIncorrigible1 @

# 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

感謝。私はまだ何かをする他の方法を学ぶためにいつも素晴らしい PS(独学)を学んでいます。私は私のことを微調整して報告します(今日は仕事が暑くて重いので明日はおそらく)。とても有難い。 – Ilya

+0

だから、@ TheIncorrigible1、あなたのスクリプトは何らかの理由で-7に設定されていても動作しますが、余分なファイルを残すので、毎日8を残します...大きな問題はありません。 7を持っています... 私はそのヒントをいただきありがとうございますlasttyetime対creationtimeを使用すると思ったことはありません。 – Ilya

関連する問題