iis_log_collector.ps1¶
File:
iis/iis_scripts/iis_log_collector.ps1
<#
.SYNOPSIS
IIS Log Collector - Gets logs from IIS site paths + additional configured paths
#>
# ============================================
# CONFIGURATION SECTION
# ============================================
# Additional log root paths to check
$AdditionalLogRoots = @(
"F:\inetpub"
)
# Date range options - set ONE of these to $true
$CollectToday = $false
$CollectLast2Days = $false
$CollectLast7Days = $true
$CollectSpecificDays = $false # If true, specify days below
$SpecificDays = @(9, 10, 11) # Days of current month
# ============================================
# SCRIPT START
# ============================================
Import-Module WebAdministration -ErrorAction Stop
# Get current date info
$Today = Get-Date
$DateSuffix = $Today.ToString("yyyy-MM-dd")
$TempFolder = Join-Path $env:TEMP "IIS_Logs_$DateSuffix"
# Create output folders
New-Item -ItemType Directory -Path $TempFolder -Force | Out-Null
Write-Host "`n=== IIS Website Information ===" -ForegroundColor Cyan
$Sites = Get-Website
foreach ($Site in $Sites) {
Write-Host "ID: $($Site.Id) | Name: $($Site.Name) | Path: $($Site.physicalPath)" -ForegroundColor Yellow
}
# Determine date filter
$DatesToCollect = @()
if ($CollectToday) {
$DatesToCollect = @($Today)
Write-Host "`nCollecting: Today only" -ForegroundColor Green
}
elseif ($CollectLast2Days) {
$DatesToCollect = @($Today, $Today.AddDays(-1))
Write-Host "`nCollecting: Last 2 days" -ForegroundColor Green
}
elseif ($CollectLast7Days) {
$DatesToCollect = -6..0 | ForEach-Object { $Today.AddDays($_) }
Write-Host "`nCollecting: Last 7 days" -ForegroundColor Green
}
elseif ($CollectSpecificDays) {
$CurrentYear = $Today.Year
$CurrentMonth = $Today.Month
$DatesToCollect = $SpecificDays | ForEach-Object {
Get-Date -Year $CurrentYear -Month $CurrentMonth -Day $_
}
Write-Host "`nCollecting: Days $($SpecificDays -join ', ') of current month" -ForegroundColor Green
}
# Convert dates to IIS log format (yyMMdd)
$DatePatterns = $DatesToCollect | ForEach-Object { $_.ToString("yyMMdd") }
Write-Host "Date patterns: $($DatePatterns -join ', ')" -ForegroundColor Gray
Write-Host "`n=== Processing Sites ===" -ForegroundColor Cyan
$TotalLogsCopied = 0
foreach ($Site in $Sites) {
$SiteName = $Site.Name
$SiteId = $Site.Id
$SitePath = $Site.physicalPath
Write-Host "`nProcessing: $SiteName (ID: $SiteId)" -ForegroundColor Yellow
Write-Host " Site Path: $SitePath" -ForegroundColor Gray
# Build list of log paths to check
$PathsToCheck = @()
# 1. Search recursively for logs folder under the site's physical path (first one found only)
if (Test-Path $SitePath) {
Write-Host " Searching for logs folder under site path..." -ForegroundColor Gray
$LogFolder = Get-ChildItem -Path $SitePath -Directory -Recurse -Filter "logs" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($LogFolder) {
$PathsToCheck += $LogFolder.FullName
Write-Host " Found: $($LogFolder.FullName)" -ForegroundColor Gray
}
}
# 2. Check logs folder under each additional root
foreach ($Root in $AdditionalLogRoots) {
if (Test-Path $Root) {
# Look for logs folder directly in root
$PathsToCheck += Join-Path $Root "logs"
# Also check standard IIS structure under root
$PathsToCheck += Join-Path $Root "logs\LogFiles\W3SVC$SiteId"
}
}
$LogsFound = @()
foreach ($Path in $PathsToCheck) {
if (Test-Path $Path) {
Write-Host " Checking: $Path" -ForegroundColor Gray
# Get matching log files - filter by last modified date
$LogFiles = Get-ChildItem -Path $Path -File -ErrorAction SilentlyContinue |
Where-Object {
$FileDate = $_.LastWriteTime.Date
$DatesToCollect | ForEach-Object { $_.Date -eq $FileDate } | Where-Object { $_ -eq $true }
}
if ($LogFiles) {
$LogsFound += $LogFiles
Write-Host " Found $($LogFiles.Count) log(s)" -ForegroundColor Green
}
}
}
if ($LogsFound.Count -eq 0) {
Write-Host " No logs found for specified dates" -ForegroundColor Red
continue
}
# Copy and rename logs into site-specific folder
$SiteFolder = Join-Path $TempFolder $SiteName
New-Item -ItemType Directory -Path $SiteFolder -Force | Out-Null
foreach ($Log in $LogsFound) {
$NewName = "$($SiteName)_$($Log.Name)"
$DestPath = Join-Path $SiteFolder $NewName
Copy-Item -Path $Log.FullName -Destination $DestPath -Force
Write-Host " Copied: $($Log.Name) -> $NewName" -ForegroundColor Gray
$TotalLogsCopied++
}
}
Write-Host "`n=== Creating Archive ===" -ForegroundColor Cyan
Write-Host "Total logs collected: $TotalLogsCopied" -ForegroundColor Green
if ($TotalLogsCopied -gt 0) {
$ZipPath = Join-Path $env:USERPROFILE "Desktop\IIS_Logs_$DateSuffix.zip"
Compress-Archive -Path "$TempFolder\*" -DestinationPath $ZipPath -Force
Remove-Item -Path $TempFolder -Recurse -Force
Write-Host "`nSUCCESS!" -ForegroundColor Green
Write-Host "Archive created: $ZipPath" -ForegroundColor Green
Write-Host "Size: $([math]::Round((Get-Item $ZipPath).Length / 1MB, 2)) MB" -ForegroundColor Gray
}
else {
Write-Host "`nNo logs collected - nothing to archive" -ForegroundColor Red
Remove-Item -Path $TempFolder -Recurse -Force
}
Write-Host "`n=== COMPLETE ===" -ForegroundColor Cyan