IIS Log Compress and Clean

Explanation:

  1. $sevenZipPath: The path to 7-Zip, ensuring the 7z.exe executable path is correct. If 7-Zip is installed in a different directory, modify this variable accordingly.
  2. $logPath: The path to the IIS log files, usually C:\inetpub\logs\LogFiles.
  3. $archivePath: The directory where compressed log files will be stored. If it doesn’t exist, the script will automatically create it.
  4. $daysToKeep: Defines how many days of log files to retain. The script compresses log files older than $daysToKeep (e.g., if set to 30 days, it compresses logs older than 30 days).
  5. Compression Command: Uses the 7z.exe command-line tool to compress log files into .7z format.
  6. Delete Original Log Files: After successful compression, the script deletes the original log files. If compression fails, the original files are retained.

Automating the Script with Task Scheduler (Optional)

To automatically compress and clean log files on a regular basis, you can set up a scheduled task using Windows Task Scheduler. Here’s how:

  1. Open Task Scheduler (taskschd.msc).
  2. Click on Create Task.
  3. In the General tab, set a name for the task, such as IIS Log Compress and Clean.
  4. In the Triggers tab, create a trigger to run the task periodically (e.g., daily or weekly).
  5. In the Actions tab, add a new action, select Start a Program, and enter the following:
  • Program/script: powershell.exe
  • Add arguments: -ExecutionPolicy Bypass -File "C:\path\to\your\script.ps1" Replace C:\path\to\your\script.ps1 with the actual path to your PowerShell script.

Official Guidance on IIS Log File Management

For more information on managing IIS log files, you can refer to the official Microsoft documentation:

Managing IIS Log File Storage

Code:

# Set the path for 7z.exe
$sevenZipPath = "C:\Program Files\7-Zip\7z.exe"

# Set the log file path
$logPath = "D:\iislog"

# Set the path to store compressed logs
$archivePath = "D:\ArchivedLogs"

# Create the folder to store compressed files, if it doesn't exist
if (-not (Test-Path $archivePath)) {
    New-Item -ItemType Directory -Path $archivePath
}

# Set the number of days to retain log files
$daysToKeep = 30

# Recursively get log files older than the specified number of days, including files in subfolders
$files = Get-ChildItem -Path $logPath -Recurse -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$daysToKeep) }

# Compress and delete files
foreach ($file in $files) {
    # Get the relative path of the file, preserving the structure of subfolders
    $relativePath = $file.FullName.Substring($logPath.Length + 1)
    $subFolderPath = Split-Path $relativePath -Parent

    # Construct the full path for the compressed file, preserving subfolder structure
    $zipFileName = Join-Path $archivePath ($subFolderPath + "\" + $file.BaseName + ".7z")

    # Create the subfolder for the compressed file (if it doesn't exist)
    $zipFolder = Split-Path $zipFileName -Parent
    if (-not (Test-Path $zipFolder)) {
        New-Item -ItemType Directory -Path $zipFolder
    }

    # Compress the file
    Write-Host "Compressing file: $($file.FullName)"
    $compressCommand = "& `"$sevenZipPath`" a -mx=9 `"$zipFileName`" `"$($file.FullName)`""
    Invoke-Expression $compressCommand

    # Check if the compressed file exists
    if (Test-Path $zipFileName) {
        # Delete the original log file
        Write-Host "Compression successful, deleting file: $($file.FullName)"
        Remove-Item -Path $file.FullName -Force
    } else {
        Write-Host "Compression failed, keeping file: $($file.FullName)"
    }
}

Write-Host "Log file compression and cleanup complete."

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据