Title: Archive the IIS Logs
Description: Zip IIS log files based on age selection and delete the .log files.
zip file name create with Month and Year name.
Description: Zip IIS log files based on age selection and delete the .log files.
zip file name create with Month and Year name.
- ##################################################################################
- # Script name: ArchiveLogFiles.ps1
- # Purpose: Archive the Log files
- # Company:
- # Input Parameters Format : .\ArchiveLogFiles.ps1 "Path1,path2" "Age in Months"
- # Out Put: All .Log files will Archive
- ##################################################################################
- # SiteCollectionReport.ps1 - Show some parameter features
- # Input Param checking and through the message to user
- Param($LogPathLocations,$CompressionRetention)
- if(($LogPathLocations -eq $null) -or ($CompressionRetention -eq $null) )
- {
- Write-Host "Input params should be with below format with same order"
- Write-Host "Syntax: .\ArchiveLogFiles.ps1 ""C:\Users\lakku\Desktop\ZipTest,C:\Users\lakku\Desktop\Zip"" ""2"""
- exit
- }
- #end check and validating the incoming parameters
- #$LogPathLocations = "C:\Users\lakku\Desktop\ZipTest"
- #$CompressionRetention = 2
- # FUNCTIONS
- function New-Zip
- {
- param([string]$zipfilename)
- set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
- (dir $zipfilename).IsReadOnly = $false
- }
- function Add-Zip
- {
- param([string]$zipfilename)
- if(-not (test-path($zipfilename)))
- {
- set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
- (dir $zipfilename).IsReadOnly = $false
- }
- $shellApplication = new-object -com shell.application
- $zipPackage = $shellApplication.NameSpace($zipfilename)
- foreach($file in $input)
- {
- $zipPackage.CopyHere($file.FullName)
- Start-sleep -milliseconds 500
- }
- }
- function Get-Zip
- {
- param([string]$zipfilename)
- if(test-path($zipfilename))
- {
- $shellApplication = new-object -com shell.application
- $zipPackage = $shellApplication.NameSpace($zipfilename)
- $zipPackage.Items() | Select Path
- }
- }
- # MAIN SCRIPT
- $date = $(get-date)
- $numdays = $date.Day
- $prevmonthlastday = $date.AddDays(-$numdays)
- $Folders = $LogPathLocations.split(",")
- $ParentFolder=$Folders
- for ($i=0; $i -lt $Folders.Length; $i++)
- {
- [string]$PathName=$Folders[$i];
- $DFSFolders = Get-ChildItem $PathName -recurse | ? {$_.PSIsContainer -eq $True} | ForEach-Object -Process {$_.FullName}
- $DFSFolders=$DFSFolders+$ParentFolder;
- foreach ($DFSfolder in $DFSfolders)
- {
- $Items = get-childitem $DFSfolder |? {($_.extension -like "*.log") -and ($_.LastWriteTime -le $prevmonthlastday.AddMonths(-$CompressionRetention))}
- $GroupedItems = $Items | sort LastWriteTime | select @{n='Month';e={$_.LastWriteTime.ToString("yyyy-MM")}},fullname,length,name | group Month
- if($GroupedItems -ne $null)
- {
- foreach ($GroupedItem in $GroupedItems)
- {
- $MonthNumber = $GroupedItem.name
- $ZipPath = "$DFSfolder\$MonthNumber.zip"
- New-Zip $ZipPath
- $GroupedItem.group | select fullname,length | %{Get-Item $_.fullname | Add-Zip $ZipPath start-sleep -s ($_.Length/20000000) }
- $GroupedItem.group | Foreach { Remove-Item $_.fullname -Force}
- }
- }
- }
- }