kwizcom banner advertisement

Archive ULS & IIS Log Files in SharePoint using PowerShell

Requirement: Archive Log files of IIS and ULS Log. 

Log files in SharePoint (both IIS Log and ULS Log) could grow and fill-up the disk space based on your configurations and usage. There are many scripts available in various blogs and boards and Here is my own script to archive Log files on SharePoint servers.
PowerShell to archive IIS ULS Log Files in SharePoint

PowerShell to Archive Log Files in SharePoint:
Set the Log file path and archive location in the configuration parameters section and run the script. This script does the following:
  • Copies all log files which are last modified before 7 Days or earlier from today, to a folder.
  • Create a Zip file from the folder containing log files to archive.
  • Deletes old log files.
Add-Type -Assembly System.IO.Compression.FileSystem

#Configuration Parameters
$LogFilesPath="C:\inetpub\logs\LogFiles\W3SVC3"
$ArchivePath="C:\LogArchive"

#Get All Log Files Modified before Last Week
$LogFilesToArchive = Get-ChildItem -Path $LogFilesPath -recurse | where-object {$_.LastWriteTime -le (Get-Date).addDays(-7) }
Write-host "Total Log Files to Archive:"$LogFilesToArchive.count -f Yellow
if($LogFilesToArchive.count -eq 0) { Break }

#Create a Temp Folder
$LogFileDate = Get-Date -Format "yyyyMMdd"
$TempFolder = Join-path -path $ArchivePath $LogFileDate
If (!(Test-Path $TempFolder)) 
{  
    New-Item $TempFolder -type Directory | Out-Null
}

#Copy Log Files to Temp Folder and Remove them
Foreach ($Logfile in $LogFilesToArchive)
{
    #Copy Log file to the Temp Directory
    Copy-Item -Path $Logfile.FullName -Destination $TempFolder
    
    #Delete the copied Log file from the source
    Remove-Item $Logfile.FullName -Confirm:$False
}

#Archive the Temp Folder
$ZipFileName= "$($ArchivePath)\$($LogFileDate).zip"
If (Test-Path $ZipFileName){ Remove-Item $ZipFileName }

$CompressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($TempFolder, $ZipFileName, $CompressionLevel, $False)

#Remove the Temp Folder
Remove-Item $TempFolder -Recurse -Force -Confirm:$False

Write-host "$($LogFilesToArchive.count) Log Files Archived to '$($ZipFileName)'" -f Green
Schedule it in Windows Task scheduler to automate the script. You can also wrap the script inside a reusable function to archive log files from multiple locations. This script removes all log files older than 30 day. You can change the hardcoded value to fit into your requirement.
Add-Type -Assembly System.IO.Compression.FileSystem

Function Archive-Logs($LogFilesPath, $ArchivePath)
{
    #Get All Log Files Modified older than a Last Week
    $LogFilesToArchive = Get-ChildItem -Path $LogFilesPath -recurse | where-object {$_.LastWriteTime -le (Get-Date).addDays(-30) }
    Write-host "Total Log Files to Archive:"$LogFilesToArchive.count -f Yellow
    if($LogFilesToArchive.count -eq 0) { Return }

    #Create a Temp Folder
    $LogFileDate = Get-Date -Format "yyyyMMdd-HHMMss"
    $TempFolder = Join-path -path $ArchivePath $LogFileDate
    If (!(Test-Path $TempFolder)) 
    {  
        New-Item $TempFolder -type Directory | Out-Null
    }

    #Copy Log Files to Temp Folder and Remove them
    Foreach ($Logfile in $LogFilesToArchive)
    {
        #Copy Log file to the Temp Directory
        Copy-Item -Path $Logfile.FullName -Destination $TempFolder
    
        #Delete the copied Log file from the source
        Remove-Item $Logfile.FullName -Confirm:$False
    }

    #Archive the Temp Folder
    $ZipFileName= "$($ArchivePath)\$($LogFileDate).zip"
    If (Test-Path $ZipFileName){ Remove-Item $ZipFileName }

    $CompressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($TempFolder, $ZipFileName, $CompressionLevel, $False)

    #Remove the Temp Folder
    Remove-Item $TempFolder -Recurse -Force -Confirm:$False

    Write-host "$($LogFilesToArchive.count) Log Files Archived to '$($ZipFileName)'" -f Green
}

#Call The function
Archive-Logs -LogFilesPath "C:\inetpub\logs\LogFiles\W3SVC3232" -ArchivePath "C:\LogArchive"
Archive-Logs -LogFilesPath "C:\inetpub\logs\LogFiles\W3SVC6245" -ArchivePath "C:\LogArchive"
Archive-Logs -LogFilesPath "D:\SPLogs\ULS" -ArchivePath "C:\LogArchive"
Archive ULS & IIS Log Files in SharePoint using PowerShell Archive ULS & IIS Log Files in SharePoint using PowerShell Reviewed by Salaudeen Rajack on 1:36 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.