私の会社には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
}
感謝。私はまだ何かをする他の方法を学ぶためにいつも素晴らしい PS(独学)を学んでいます。私は私のことを微調整して報告します(今日は仕事が暑くて重いので明日はおそらく)。とても有難い。 – Ilya
だから、@ TheIncorrigible1、あなたのスクリプトは何らかの理由で-7に設定されていても動作しますが、余分なファイルを残すので、毎日8を残します...大きな問題はありません。 7を持っています... 私はそのヒントをいただきありがとうございますlasttyetime対creationtimeを使用すると思ったことはありません。 – Ilya