ps-archive

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.


  1. ##################################################################################  
  2. #  Script name: ArchiveLogFiles.ps1  
  3. #  Purpose: Archive the Log files  
  4. #  Company: 
  5. # Input Parameters Format : .\ArchiveLogFiles.ps1 "Path1,path2" "Age in Months"   
  6. # Out Put: All .Log files will Archive   
  7. ##################################################################################  
  8. # SiteCollectionReport.ps1 - Show some parameter features  
  9. # Input Param checking and through the message to  user   
  10.    
  11. Param($LogPathLocations,$CompressionRetention)      
  12. if(($LogPathLocations -eq $null)  -or ($CompressionRetention -eq $null) )   
  13.     
  14.    {   
  15.      Write-Host "Input params should be with below format with same order"   
  16.      Write-Host "Syntax:  .\ArchiveLogFiles.ps1   ""C:\Users\lakku\Desktop\ZipTest,C:\Users\lakku\Desktop\Zip""  ""2"""  
  17.     exit  
  18.    }    
  19. #end check and validating the incoming parameters  
  20.   
  21.   
  22. #$LogPathLocations = "C:\Users\lakku\Desktop\ZipTest"  
  23. #$CompressionRetention = 2  
  24.   
  25. # FUNCTIONS  
  26. function New-Zip  
  27.     {  
  28.      param([string]$zipfilename)  
  29.      set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))  
  30.      (dir $zipfilename).IsReadOnly = $false  
  31.     }  
  32.   
  33. function Add-Zip  
  34.     {  
  35.         param([string]$zipfilename)  
  36.        
  37.          if(-not (test-path($zipfilename)))  
  38.              {  
  39.              set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))  
  40.              (dir $zipfilename).IsReadOnly = $false   
  41.              }  
  42.   
  43.          $shellApplication = new-object -com shell.application  
  44.          $zipPackage = $shellApplication.NameSpace($zipfilename)  
  45.   
  46.          foreach($file in $input)   
  47.              {   
  48.              $zipPackage.CopyHere($file.FullName)  
  49.              Start-sleep -milliseconds 500  
  50.              }  
  51.       }  
  52.   
  53. function Get-Zip  
  54.     {  
  55.      param([string]$zipfilename)  
  56.          if(test-path($zipfilename))  
  57.              {  
  58.                  $shellApplication = new-object -com shell.application  
  59.                  $zipPackage = $shellApplication.NameSpace($zipfilename)  
  60.                  $zipPackage.Items() | Select Path  
  61.              }  
  62.     }  
  63.   
  64. # MAIN SCRIPT  
  65.   
  66. $date = $(get-date)  
  67. $numdays = $date.Day  
  68. $prevmonthlastday = $date.AddDays(-$numdays)  
  69.   
  70. $Folders = $LogPathLocations.split(",")  
  71. $ParentFolder=$Folders  
  72.   
  73. for ($i=0; $i -lt $Folders.Length; $i++)  
  74. {   
  75. [string]$PathName=$Folders[$i];  
  76.   
  77.   
  78. $DFSFolders = Get-ChildItem $PathName -recurse | ? {$_.PSIsContainer -eq $True} | ForEach-Object -Process {$_.FullName}   
  79. $DFSFolders=$DFSFolders+$ParentFolder;  
  80.   
  81.   
  82. foreach ($DFSfolder in $DFSfolders)  
  83.     {  
  84.       
  85.          
  86.         $Items =  get-childitem $DFSfolder   |? {($_.extension -like "*.log") -and ($_.LastWriteTime -le $prevmonthlastday.AddMonths(-$CompressionRetention))}  
  87.         $GroupedItems = $Items | sort LastWriteTime | select @{n='Month';e={$_.LastWriteTime.ToString("yyyy-MM")}},fullname,length,name | group Month  
  88.   
  89.          if($GroupedItems -ne $null)  
  90.              {  
  91.                  foreach ($GroupedItem in $GroupedItems)  
  92.                      {  
  93.                        
  94.                          $MonthNumber = $GroupedItem.name    
  95.                          $ZipPath = "$DFSfolder\$MonthNumber.zip"   
  96.                          New-Zip $ZipPath  
  97.                          $GroupedItem.group | select fullname,length | %{Get-Item $_.fullname | Add-Zip $ZipPath start-sleep -s ($_.Length/20000000) }  
  98.                             
  99.                            
  100.                          $GroupedItem.group | Foreach { Remove-Item $_.fullname -Force}  
  101.                
  102.                      }  
  103.                        
  104.                }  
  105.                  
  106.                  
  107.       }  
  108. }