Last active
December 6, 2018 07:05
-
-
Save skunkie/3e29684972f57b63b96a8062a47438e3 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
#Requires -Version 3.0 | |
<# | |
.SYNOPSIS | |
Log Archiver for Windows | |
.DESCRIPTION | |
This script can be used to archive old log files | |
.PARAMETER Path | |
The path to the directory with log files. | |
.PARAMETER ArchiveDays | |
The number of days from now, to archive files. | |
.PARAMETER DeleteDays | |
The number of days from now, to delete files. | |
.PARAMETER Filter | |
An optional filter for log files | |
.EXAMPLE | |
PS >.\LogArchive.ps1 -Path "D:\Logs\" -ArchiveDays 30 -DeleteDays 90 -Filter "*.log" | |
.EXAMPLE | |
PS >C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "C:\Scripts\LogArchive.ps1" "-Path C:\inetpub\logs\LogFiles\W3SVC1\ -ArchiveDays 60 -DeleteDays 180 -Filter "*.log"" | |
Use this example in the Task Scheduler | |
.LINK | |
None | |
.NOTES | |
Author: skunkie | |
Last modified: 19/01/2015 | |
Requires -Version 3.0 | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true)] | |
[String[]]$Path, | |
[Parameter(Position=1, Mandatory=$true)] [System.Int32]$ArchiveDays, | |
[Parameter(Position=2, Mandatory=$true)] [System.Int32]$DeleteDays, | |
[Parameter(Position=3, Mandatory=$false)][System.String]$Filter | |
) | |
Set-StrictMode -Version Latest | |
# Set the preference of Error Action | |
$ErrorActionPreference = 'Stop' | |
if ($DeleteDays -le $ArchiveDays) { | |
throw "DeleteDays is less than or equal to ArchiveDays" | |
} | |
$archDays = (Get-Date).AddDays(-$ArchiveDays) | |
$delDays = (Get-Date).AddDays(-$DeleteDays) | |
$files = $null | |
$delFiles = $null | |
$files = Get-ChildItem -File -Path $Path -Filter $Filter | Where-Object {$_.LastWriteTime -lt $archDays} | |
foreach ($file in $files) { | |
$fileFullName = $file.FullName | |
if ($file.LastWriteTime -lt $archDays -and -not $fileFullName.ToLower().EndsWith('.zip')) { | |
$zipFileFullName = "{0}.zip" -f $fileFullName | |
Set-Content -Path $zipFileFullName('PK' + [char]5 + [char]6 + ("$([char]0)" * 18)) | |
(Get-ChildItem -Path $zipFileFullName).IsReadOnly = $false | |
$shellApplication = New-Object -ComObject Shell.Application | |
$zipPackage = $shellApplication.NameSpace($zipFileFullName) | |
$zipPackage.CopyHere($fileFullName) | |
do { | |
Start-Sleep -Milliseconds 250 } | |
while ($zipPackage.Items().Count -eq 0) | |
$zipFile = Get-ChildItem -File -Path $zipFileFullName | |
$zipFile.LastWriteTime = $file.LastWriteTime | |
} elseif ($file.LastWriteTime -gt $delDays) { | |
continue | |
} | |
Remove-Item -Path $fileFullName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment