Skip to content

Instantly share code, notes, and snippets.

@kfrancis
Created June 14, 2016 14:26
Show Gist options
  • Save kfrancis/48fca721ec70865c3988b964b43810a3 to your computer and use it in GitHub Desktop.
Save kfrancis/48fca721ec70865c3988b964b43810a3 to your computer and use it in GitHub Desktop.
# Script that archives the .log files in the current directory that are from last month into a file that contains the month/year
$7Zip = 'C:\"Program Files"\7-Zip\7z.exe'
$firstDayOfMonth = (Get-Date (Get-Date) -Day 1 -Hour 0 -Minute 0 -Second 0)
Write-Host ("First Day of THIS month is: " + $firstDayOfMonth)
$lastDayOfLastMonth = $firstDayOfMonth.AddSeconds(-1)
Write-Host ("Last Day of LAST month is: " + $lastDayOfLastMonth)
$outputFileName = [string]::Format("Logs_{0}-{1}.zip", $lastDayOfLastMonth.Year, $lastDayOfLastMonth.Month.ToString("00"))
$logFiles = Get-ChildItem | Where-Object { $_.Extension -match ".(log)" -and $_.LastWriteTime -lt $firstDayOfMonth }
foreach ($logFile in $logFiles) {
Write-Host ("Adding " + $logFile.FullName + " to " + $outputFileName)
$logFilePath = ($logFile.FullName)
Invoke-Expression "$7Zip a $outputFileName $logFilePath -sdel -mmt"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment