All Lights on SharePoint ULS Logs with PowerShell

This morning I decided to play a bit with the various PowerShell cmdlets that are available in SharePoint 2013 and that allow users to interact with the ULS logs. This blog post aims at providing an explaination of each of the cmdlets, and give concrete examples as to when you could potential use them in real world scenarios. The first thing I did in order to obtain the full list of PowerShell cmdlets that were available to me and that were related to the ULS files was to execute the following line of PowerShell:

Get-Command | Where{$_.ModuleName -eq “Microsoft.SharePoint.PowerShell” -and $_.Name -like ‘*LogFile*’}

20150418-01.png

So 3 cmdlets in total: Merge-SPLogFile, New-SPLogFile, and New-SPUsageLogFile. Their names are pretty self explanatory, but still, let us examine the two first cmdlets.

Merge-SPLogFile:

If you ever played with PowerShell for SharePoint, then this cmdlet ought to be one of the very first one you’ve ever used. This cmdlet let you merge log files from all SharePoint servers contained within your farm into a single file. This is extremely useful, preventing the users from having to go on every single server in the farm and try to determine which one contains the error matching a specific Correlation Id. Let us assume that we have a SharePoint farm with 3 Web-Front ends, and 2 application servers. Upon navigating to a site within the SharePoint environment, the user gets an error page exhibiting the following Correlation ID: b681d542-bde8-45e6-8c6a-3953d8a4aa82. In order to scan all 5 SharePoint servers at once to determine what server has a log file containing the error with that Correlation ID, one could simply execute the following line of PowerShell:

Merge-SPLogFile -Path C:\temp\nik.txt -Correlation b681d542-bde8-45e6-8c6a-3953d8a4aa82

20150418-02.png

It may take a few seconds for the operation to complete. Once completed, a file will have been created at c:\temp\nik.txt and will contain information about the error we are investigating.

20150418-03.png

Now, to prevent you from having to go back and manually retrieve the file that has just been created, we could modify the script a little:

$path = “C:\temp\nik.txt”

Merge-SPLogFile -Path $path -Correlation b681d542-bde8-45e6-8c6a-3953d8a4aa82

Get-Content $path

20150418-04.png

Another very interesting operation we can do with the Merge-SPLogFile cmdlet is to merge information out of all log files based on Severity Level. For example, we can merge all entries that have a severity level of Unexpected by using the following line of PowerShell:

Merge-SPLogFile -Path c:\Temp\nik.txt -Level Unexpected

20150418-05.png

New-SPLogFile:

This cmdlet is very straight forward. Calling it simply creates a new log file. Normally, within SharePoint, ULS log files are created after a predefined time span (default is 30 minutes). Log files are normally split into several small ULS log files. By default, these files are kept for 14 days.

20150418-06.png

This cmdlet is normally called without any parameters. Calling it will automatically create an new log file and the tracing process will use that new file to flush future log entries.

New-SPLogFile

20150418-07.png

Leave a Reply

Your email address will not be published. Required fields are marked *