0
0

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 1 year has passed since last update.

ChatGPTにIISログをバックアップするpowershellを書かせてみた

Last updated at Posted at 2023-02-15

質問:先月のiisログを圧縮するpowershellを書いて

回答:
以下は、PowerShellを使用して先月のIISログを圧縮するスクリプト例です。このスクリプトでは、IISログがデフォルトの場所に保存されていることを前提としています。

# 先月の日付を取得
$lastMonth = (Get-Date).AddMonths(-1)

# IISログの保存場所
$logPath = "C:\inetpub\logs\LogFiles\W3SVC1"

# 先月のログファイル名
$logFileName = "u_ex" + $lastMonth.ToString("yyMMdd") + ".log"

# 圧縮後のファイル名
$zipFileName = $logFileName + ".zip"

# 圧縮するログファイルのパス
$logFilePath = Join-Path $logPath $logFileName

# 圧縮後のファイルの保存先
$zipFilePath = Join-Path $logPath $zipFileName

# 圧縮する
Compress-Archive -Path $logFilePath -DestinationPath $zipFilePath -Force

# 圧縮したファイルを削除する
Remove-Item $logFilePath

このスクリプトは、以下の手順に従ってログファイルを圧縮します。

1.Get-Date コマンドレットを使用して、先月の日付を取得します。
2.$logPath 変数に、IISログが保存されている場所のパスを格納します。
3.$logFileName 変数に、先月のログファイル名を格納します。
4.$zipFileName 変数に、圧縮後のファイル名を格納します。
5.$logFilePath 変数に、圧縮するログファイルのパスを格納します。
6.$zipFilePath 変数に、圧縮後のファイルの保存先を格納します。
7.Compress-Archive コマンドレットを使用して、ログファイルを圧縮します。-Path パラメーターに圧縮するファイルのパス、-DestinationPath パラメーターに圧縮後のファイルの保存先、-Force パラメーターに圧縮後のファイルがすでに存在する場合に上書きするよう指定します。
8.Remove-Item コマンドレットを使用して、圧縮したログファイルを削除します。

このスクリプトを実行すると、先月のIISログが圧縮され、元のログファイルが削除されます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?