Created
April 5, 2017 20:55
-
-
Save jwatney/00cbc711d5f90231f5fc90b00f11425a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Archive-Logs { | |
[CmdLetBinding()] | |
PARAM ( | |
[string][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]$LogsPath, | |
[string][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]$ArchivesPath | |
) | |
BEGIN { | |
} | |
PROCESS { | |
$today = (Get-Date) | |
$date = Get-Date $today.AddMonths(-1) -day 1 -hour 0 -minute 0 -second 0 | |
$folders = Get-ChildItem -Path $LogsPath -Filter "W3SVC*" | |
Write-Host "Archiving log files older than $date" | |
foreach($folder in $folders) { | |
New-Item -ErrorAction SilentlyContinue -ItemType directory -Path "$ArchivesPath\$folder" | Out-Null | |
$files = Get-ChildItem -Path $folder.FullName -Filter "u_ex*.log" -ErrorAction Stop | Where { $_.LastWriteTime -lt $date } | |
foreach($file in $files) { | |
try { | |
Write-Host ("{0}\{1}\{2}.zip" -f $ArchivesPath, $folder, $file.BaseName) | |
Compress-Archive -Path $file.FullName -DestinationPath ("{0}\{1}\{2}.zip" -f $ArchivesPath, $folder, $file.BaseName) | |
Remove-Item $file.FullName | |
} catch [Exception] { | |
$PSCmdlet.ThrowTerminatingError($_) | |
} | |
} | |
} | |
} | |
END {} | |
} | |
Archive-Logs -LogsPath "C:\inetpub\logs\LogFiles" -ArchivesPath "C:\logs\iis" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment