Geeks With Blogs
Simon's blog
We have a requirement to keep all SharePoint logs for certainly longer than disk space on the web front end server allows, we were not allowed any more disk space on the server to store logs, instead the company maintains an archive server which uses large capacity slow speed disks (read cheap). Our ideal situation would be to add all the logs from a particular day into a single appropriately named zip file on a daily basis and then move this file to the archive server without any intervention from us.  So to PowerShell we went, after trying the System IO method which just didn't really work, we went down the route of the PowerShell Community Extensions and Write-Zip, after much trial and error; trying the most direct route of find all files with a certain date and add them to a zip file as well as creating the zip file first and then copying/moving the files into said zip file, this was the result:

Step 1: Get the archive date; in our case seven days ago
Step 2: Move all log files in the log directory into an archive folder with the same name as the archive zip file
Step 3: Append each log file to the zip file
Step 4: Move the archive zip to the archive server, remove the archived log files and the archive zip folder

Step 1

param(
    [string]$logs,
    [string]$archivelocation
    [string]$extarchive
)

$logfiles = gci $logs
$machinename = Get-Content env:computername

# Get the archive date
$archivedate = (get-date).AddDays(-7).ToString("dd/MM/yyyy")

# Set the date for the archive file
$zipfiledate = (get-date).AddDays(-7).ToString("yyyyMMdd")

# Create the zip file
$zipfilename = ($machinename+"-"+$zipfiledate+".zip")
$zipfile = ($archivelocation+"\"+$zipfilename)
$zipfolder = ($archivelocation+"\"+$machinename+$zipfiledate+"zip")

# Create the temp archive folder
New-Item $zipfolder -type directory


Step 2

# Search for and move all the relevant log files to the archive folder
ForEach($file in $logfiles)
{
    If($file.lastwritetime.Tostring("dd/MM/yyyy") -eq $archivedate)
    {
        Write-Host "Found filename: " $file.FullName -ForegroundColor DarkGreen
        Move-Item -Path $file.FullName -Destination $zipfolder
    }
}

Step 3

# Create the archive file
write-zip -Path $zipfolder -OutputPath $zipfile

Step 4

# Tidy up
Move-Item -Path $zipfile.FullName -OutputPath $extarchive
Remove-Item $zipfolder -Recurse
Posted on Monday, March 10, 2014 9:58 AM PowerShell , SharePoint logs , PSCX | Back to top


Comments on this post: Compress SharePoint logs using PowerShell

# re: Compress SharePoint logs using PowerShell
Requesting Gravatar...
Thanks for share these steps. Nice work and great effort.

[URL="www.spiralclick.com"]Web Design Dubai[/URL]
Left by Alan David on Mar 15, 2014 5:27 AM

Your comment:
 (will show your gravatar)


Copyright © simonh | Powered by: GeeksWithBlogs.net