Skip to content

Instantly share code, notes, and snippets.

@seankearney
Created November 5, 2012 19:52
Show Gist options
  • Save seankearney/4019944 to your computer and use it in GitHub Desktop.
Save seankearney/4019944 to your computer and use it in GitHub Desktop.
IIS Log File Archive
##################################################################
# IIS Web Log Archive Script
# Takes a path to a directory containing IIS Log files
# Zips them up with 7-zip and stores the zip in a
# directory multiple levels up.
# Original log files are then deleted
#
# Expected folder structures:
# X:\Some\Directory\ContainAllLogs\
# |- Live
# |- Domain Name [server.com]
# |- Subdomain [www] ** This is the path that IIS is configured with
# |- IIS Auto Created folder [w3svcxxxxxx]
# |- Archive
# |- Domain Name [server.com]
# |- Subdomain [www] ** This is where the zip will go
#
# Usage:
# Within a .bat file:
#
# powershell set-executionpolicy Unrestricted
# REM -- *.ashfordapps.com
# powershell "& .\archiveLogs.ps1 "X:\WebsiteLogs\Live\Domain.com\Subdomain\W3SVC286639526" "
#
##################################################################
param (
$logPath = $(throw 'Please supply a path to the log files'),
$zipPrefix = ""
)
#$logPath = "C:\test\Logs"
$month = [DateTime]::Today.AddMonths(-1).ToString("MM")
$year = [DateTime]::Today.AddMonths(-1).ToString("yy")
$zipName = ($zipPrefix + "ex" + $year + $month + ".zip")
$searchString = ("ex" + $year + $month + "*.log")
$searchString = [System.IO.Path]::Combine($logPath, $searchString)
$count = 0
$List = @(Get-ChildItem $searchString)
# Check number of files and exit if 0
foreach ($_ in $List ){$_.name
$count = $count +1}
if ($count -eq 0){
return
}
# Build archive path
$domainName = (Get-Item $logPath).Parent.Parent.Name
$subDomainName = (Get-Item $logPath).Parent.Name
# Get the root archive path
# We are at: X:\somepath\Live\[Domain]\[SubDomain]\[auto]
# We want it at: X:\somepath\Archive\[Domain]\[SubDomain]
# X:\somepath\
$archivePath = (Get-Item $logPath).Parent.Parent.Parent.Parent.Fullname
# Tack on the 'Archive' directory
$archivePath = [System.IO.Path]::Combine($archivePath, "Archive")
# Tack on the 'Domain' directory
$archivePath = [System.IO.Path]::Combine($archivePath, $domainName)
# Tack on the 'SubDomain' directory
$archivePath = [System.IO.Path]::Combine($archivePath, $subDomainName)
# Create the directory for the archive
[System.IO.Directory]::CreateDirectory($archivePath) | out-null
$archivePath = [System.IO.Path]::Combine($archivePath, $zipName)
# Archive the files
C:\Utils\PortableApps\7-ZipPortable\App\7-Zip\7z.exe a -tzip $archivePath $searchString
# delete all .log files
ri $searchString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment