Purge-LogFiles PowerShell Script updated to Version 2.1

Share on twitter
Share on linkedin
Share on facebook
Share on stumbleupon
Share on xing
Share on email
Share on print

The PowerShell script to purge Exchange Server and IIS log files has been updated to version 2.1.

The function Copy-LogFiles has been slightly rewritten and there has been a change in the cmdlet parameters.

When using ArchiveMode CopyAndZip or CopyZipAndDelete all copied log files in the EXCHANGESERVER\LOGS folder are added to a compressed archive. The script creates a separate archive for IIS and Exchange logs.

Example of compressed archive files

 Code updated

function Copy-LogFiles {  [CmdletBinding()]  param(    [string]$SourceServer,    [string]$SourcePath,    $FilesToMove,    [string]$ArchivePrefix = ''  )  if($SourceServer -ne '') {     # path per SERVER for zipped archives    $ServerRepositoryPath = Join-Path -Path $RepositoryRootPath -ChildPath $SourceServer    # subfolder used as target for copying source folders and files    $ServerRepositoryLogsPath = Join-Path -Path $ServerRepositoryPath -ChildPath $LogSubfolderName    $ServerRepositoryPath = Join-Path -Path $RepositoryRootPath -ChildPath $SourceServer    if(!(Test-Path -Path $ServerRepositoryPath)) {      # Create new target directory for server, if does not exist      $null = New-Item -Path $ServerRepositoryPath -ItemType Directory -Force -Confirm:$false    }    foreach ($File in $FilesToMove) {      # target directory      $targetDir = $File.DirectoryName.Replace($TargetServerFolder, $ServerRepositoryLogsPath)      # target file path      $targetFile = $File.FullName.Replace($TargetServerFolder, $ServerRepositoryLogsPath)            # create target directory, if not exists      if(!(Test-Path -Path $targetDir)) {$null = mkdir -Path $targetDir}      # copy file to target      $null = Copy-Item -Path $File.FullName -Destination $targetFile -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue    }            if($ZipArchive) {      # zip copied log files            $Archive = Join-Path -Path $ServerRepositoryPath -ChildPath ('{0}-{1}' -f $ArchivePrefix, $ArchiveFileName)      $logger.Write(('Zip copied files to {0}' -f $ArchiveFileName))            # delete archive file, if already exists      if(Test-Path -Path $Archive) {Remove-Item -Path $Archive -Force -Confirm:$false}      try {        # create zipped asrchive        Add-Type -AssemblyName 'System.IO.Compression.FileSystem'        [IO.Compression.ZipFile]::CreateFromDirectory($ServerRepositoryLogsPath,$Archive)      }      catch {        $logger.Write(('Error compressing files from {0} to {1}' -f $ServerRepositoryLogsPath, $Archive),3)            }      finally {        # cleanup, if compression was successful        if($DeleteZippedFiles) {          $logger.Write(('Deleting folder {0}' -f $ServerRepositoryLogsPath))          $null = Remove-Item -Path $ServerRepositoryLogsPath -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue        }      }    }   }  }

 

Note

Links

Social

 

 

%d Bloggern gefällt das: