rilpoint_mw113

Backup ULS

# PowerShell script to backup SharePoint 2010 diagnostic logs
 
Add-PsSnapin Microsoft.SharePoint.PowerShell
Start-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leak.
$backupLocation = "C:\Backup\ULSLog"    # Replace with your backup location
$today = Get-Date            # Get current date
$todayEdited = Get-Date -format "MM-dd-yyyy HH.mm.ss"    # Get current date and format it to avoid invalid characters such as "/", ":"
 
# Get the latest backup log file by sorting the file list at the backup location, then select the last item
$lastLogFile= Get-ChildItem $backupLocation\*.* | Sort-Object name | Select-Object -last 1
 
# If no log file exists at the backup location, merge the last hour of SharePoint log data from all servers.
# In this example, I only get the logs that have level High or above to prevent overload.
# Available levels are: Unexpected, Monitorable, High, Medium, Verbose, VerboseEx
 
if ($lastLogFile -eq $null)    
{
    write-Host Merging logs from all servers in the last hour
    Merge-SPLogFile –Path "$backupLocation\$todayEdited.log" –Overwrite -Level High
}
 
else    # If a log file already exists, merge SharePoint logs from last backup run until now
{
    # Format the log file name and convert it to DateTime object
    $lastRunTime = Get-Date $lastLogFile.Name.TrimEnd(".log").Replace(".",":")
 
    write-Host Merging logs from $lastRunTime until now
    Merge-SPLogFile –Path "$backupLocation\$todayEdited.log" –StartTime $lastRunTime –EndTime $today –Overwrite -Level High
}
 
Stop-SPAssignment -Global
 
Remove-PsSnapin Microsoft.SharePoint.PowerShell
 
write-Host "Finished script."
Skin by RIL Partner