7
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerShellでログファイルの圧縮+ローテート

Last updated at Posted at 2019-08-02

IISとかのログをただローテートするだけだと結構な容量を食うので、ZIP圧縮した上でローテートする。
バッチでやるとzipコマンドのライブラリを入れなければいけないので、やはりPowerShell。

ログ圧縮ローテート.ps1
#ログのパス
$Path = "D:\Logs\IIS\W3SVC1"

#zip圧縮する閾値(日)
$zipCompressTerm = 5

#zipを削除する閾値(日)
$zipDeleteTerm = 180

#zip圧縮対象のログを取得
$Items = Get-ChildItem -Path $Path | ?{$_.LastWriteTime -le (Get-Date).AddDays(-$zipCompressTerm) -and $_.Name -like "*.log"}

#対象のアイテムをZIP圧縮
$Items | %{Compress-Archive -Path $_.FullName -DestinationPath "$($Path)\$($_.Name).zip"}

#ログを削除
$Items | Remove-Item

#zip削除対象を削除
Get-ChildItem -Path $Path | ?{$_.LastWriteTime -le (Get-Date).AddDays(-$zipDeleteTerm) -and $_.Name -like "*.zip"} | Remove-Item
7
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?