de-DEen-GB
 
rss

Just can't get enough of IT

This blog is about mostly anything in IT. But the primary focuses are Microsoft Technologies like Exchange, Office 365, Azure and Cloud Security.

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

 

 

Read More »
On April 10, 2017
0 Comment
718 Views

 

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

Release 2.0 allows for copying of files that will be deleted to be copied to a central file repository. The script will create a folder per server and the full log file folder structure will be preserved.

The next release will contain an option to compress the copied log files.

Added code:

function Copy-LogFiles {
  [CmdletBinding()]
  param(
    [string]$SourceServer,
    [string]$SourcePath,
    $FilesToMove
  )

  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

    }-Force   
    
    if($ZipArchive) {
      # zip copied log files
      #
      <# NOT FULLY TESTED YET 
      $Archive = Join-Path -Path $ServerRepositoryPath -ChildPath $ArchiveFileName
      $logger.Write(('Zip copied files to {0}' -f $ArchiveFileName))

      if(Test-Path -Path $Archive) {Remove-Item $Archive -Force -Confirm:$false}

      Add-Type -AssemblyName 'System.IO.Compression.FileSystem'
      [IO.Compression.ZipFile]::CreateFromDirectory($ServerRepositoryLogsPath,$Archive)

      #>
    } 
  }  
}

Note

Links

Social

 

 

Read More »
On December 11, 2014
1 Comment
3419 Views
Last updated: 2017-10-01 | NOTE: If you are using v2.1 update to v2.13

Exchange Server 2013 Exchange Server 2016 Description

This script deletes all Exchange and IIS logs older than X days from all Exchange 2013+ servers that are fetched using the Get-ExchangeServer cmdlet.

The Exchange log file location is read from the environment variable and used to build an adminstrative UNC path for file deletions.

  • It is assumed that the Exchange setup path is IDENTICAL across all Exchange servers.
  • The IIS log file location is read from the local IIS metabase of the LOCAL server and is used to build an administrative UNC path for IIS log file deletions.
  • It is assumed that the IIS log file location is identical across all Exchange servers.

Examples

# EXAMPLE 1
# Delete Exchange and IIS log files older than 14 days 

.\Purge-LogFiles -DaysToKeep 14

# EXAMPLE 3
# Delete Exchange and IIS log files older than 7 days with automatic discovery

.\Purge-LogFiles -DaysToKeep 7 -Auto

# EXAMPLE 3
# Delete Exchange and IIS log files older than 7 days with automatic discovery and send email report

.\Purge-LogFiles -DaysToKeep 7 -Auto -SendMail -MailFrom postmaster@sedna-inc.com -MailTo exchangeadmin@sedna-inc.com -MailServer mail.sedna-inc.com

# EXAMPLE 4
# Delete Exchange and IIS log files older than 14 days, but copy files to a central repository and compress the log files before final deletion

.\Purge-LogFiles -DaysToKeep 14 -RepositoryRootPath \\OTHERSERVER\OtherShare\LOGS -ArchiveMode CopyZipAndDelete

Version History

  • 2.13 Issue #7 fixed | SendMail switch query
  • 2.11 Issue #6 fixed | Missing Remove-Item cmdlet
  • 2.1 Log file archiving and archive compressions added
  • 2.0 CopyFilesBeforeDelete implemented
  • 1.93 Minor chances to PowerShell hygiene
  • 1.92 .Count issue fixed to run on Windows Server 2012
  • 1.91 Write DaysToKeep to log
  • 1.9 Global functions updated (write to event log)
  • 1.8 Support for global logging and other functions added
  • 1.6 Count Error fixed 1.7 Email report functionality added
  • 1.5 Sorting of server names added and Write-Host output changed
  • 1.4 Handling of IIS default location fixed
  • 1.3 Check if running in elevated mode added
  • 1.2 Auto/Manual configration options added
  • 1.1 Variable fix and optional code added
  • 1.0 Initial community release

Requirements

This PowerShell script requires the GlobalFunctions module, described here.

When using PowerShell 5 or later you can simply use Install-Module GlobalFunctions to install the module to your system.

Links

Additional Credits

Brian Reid, C7 Solutions, http://www.c7solutions.com/2013/04/removing-old-exchange-2013-log-files-html

Follow

 

Read More »
On October 9, 2014
0 Comment
468 Views

Description

This script reconfigures the IIS log folder to use a different folder instead of the default C:\inetpub\logs folder.

Additionally the log settings can be adjusted as well. The script changes the default log file location and settings on a server level.

By default the settings are inherited by websites. If manual changes have been made on a webite level, not all settings will be inherited.

Examples

Change the IIS log file location to D:\IISLogs

.\Set-Webserver.ps1 -LogFolderPath D:\IISLogs

Change the IIS log period to an hourly period

.\Set-Webserver.ps1 -LogFilePeriod Hourly

Use the local time for filenames and log file rollover

.\Set-Webserver.ps1 -LocalTimeRollover $true

Version History

  • 1.0, Initial community release
  • 1.1, PowerShell hygiene applied, some typo fixes

Links

Script last updated: 2016-07-28

Additional Credits

Additional credits go to Michel de Rooij, https://eightwone.com

Follow

Read More »