Wednesday, June 10, 2020

Function to Zip log files


Below Functions will help you to organize your scripts. First function will create reporting folder to store all script related reports. Second function will zip files within report folder and archive them. When zipping files it will put a time stamp and if there is no file to zip it will stop. I have built a filter to collect desired file types, which can easily be modified if desired. Keep scripting and stay organized.

 Function-Check-Report-Folder

 
 
#(1)_.Create Log folder vars
$Repname  = 'MYREPORT-Report'
$logname  = $Repname+'-Log.TXT'
$csvname  = $Repname+'-Log.CSV'
$traname  = $Repname+'-Transcript.LOG'
$now      = (get-Date -format 'dd-MMM-yyyy-HH-mm')
$user     = $env:USERNAME
$desFol   = ("C:\temp\$Repname\")
$logfile  = $desFol+$now+$logname
$csvfile  = $desFol+$now+$csvname
$scrfile  = $desFol+$now+$traname
 
function Function-Check-Report-Folder{
 
  [CmdletBinding()]
  param(
  [Parameter(Mandatory=$True)]
  [String]$DestinationFolder
 
  ) 
 
  Try{
 
  If (!(Test-Path $DestinationFolder)){
     New-Item -ItemType Directory -Force $DestinationFolder | Out-Null
     }
 
 
  }Catch{
 
  $errofile = $($PSItem.ToString())
  Write-Warning 'Error has occoured'
  Write-host 'Problem FOUND' $errofile -ForegroundColor red -BackgroundColor Black
 
    }
 
}
 
Function-Check-Report-Folder -DestinationFolder $desFol
 
 
 

 




function Function-Archive-Reports

 

function Function-Archive-Reports {
[cmdletbinding()]
param(
     [parameter(
         Mandatory=$true)]
         [String]$repName,
         [String]$source
)
 
     Begin
{
        
    # Add Vars
     $now     =
(Get-Date -format 'dd-MMM-yy-HH-mm-ss')
     $folName = ($now+$repname)+'.Archive'
     $ArcFol  =
($desFol)+$folName
     $zipFolder = ($ArcFol+'.zip')
 
     }
 
     Process
{
     # Check Archive Folder create if it does not exist
     If
(!( Test-Path $ArcFol )) {
               New-Item -ItemType Directory -Force $ArcFol | Out-Null
         }
 
      Try{
 
       # Get List
       write-Host 'Moving files into archive folder' -ForegroundColor DarkYellow
        $list = get-childItem $desFol | `
                   ?{$_.Extension -like '.CSV' -or $_.Extension -like '.LOG'}
        #Count Items for invetory                   
        $total = ($list).count
        if
($total -like '0'){
         Write-Host 'Nothing to Zip'
         Write-Host 'Script will stop'
         Start-Sleep -Seconds 5
         break;
           }
 
        write-host "`tTotal files located:" $total -ForegroundColor White
        write-host "`tMoving files into ZIP Folder:" -ForegroundColor White
        $list | move-Item -Destination $ArcFol
        write-Host "`tZipping Archive folder" -ForegroundColor White
 
    If
(!( Test-Path $zipFolder )) {
  
      Add-Type -assembly 'system.io.compression.filesystem' -ErrorAction Stop
     [io.compression.zipfile]::CreateFromDirectory($ArcFol, $zipFolder)
 
    }else{
    Write-Host "`tZip folder exist" -ForegroundColor Green
    write-Host "`tCompleted"
 
         }
 
       }Catch{
         $errofile = $($PSItem.ToString())
         write-Warning 'Error has occoured'
has occoured'
         write-host 'Problem FOUND' $errofile -ForegroundColor red -BackgroundColor Black
          }
     }
 
     End
{
 
     }
 
}
 
Function-Archive-Reports -repName $Repname -source $desFol
 

 

 

Azure Solutions Architect

AWS Certified Cloud Practitioner

Azure Certified Security Engineer Associate

https://simplepowershell.blogspot.com

https://cloudsec365.blogspot.com

https://msazure365.blogspot.com

https://twitter.com/Message_Talk



 


No comments:

Post a Comment