Logging in Powershell in SharePoint 2010

Logging in Powershell in SharePoint 2010
1 vote, 5.00 avg. rating (95% score)

Introduction

The most common and mandatory thing while coding is logging the results or logging the errors. The log file will give overview of what happened or check if anything went wrong.
In this post I will provide you a generic function how to log information while working with the script

Logging

Following example creates a log file and enters information into it

$DATE = get-date
$LogPath = "C:\Custom\Projects\AdiCodes"
$LogFileName = "AdisGroupSiteNewLog.log"

$FilePath = $LogPath + "\" + $LogFileName

$logFileCreated = $False
function write-log([string]$label, [string]$logMsg)
{
	if($logFileCreated -eq $False)
	{
		write-host "Creating log file..."
		if((Test-Path -path $LogPath) -ne $True)
		{
			write-host "Provide proper values to LogPath folder" -ForegroundColor Red
		}
		else
		{
			Add-Content -Path $FilePath -Value $logHeader
			$script:logFileCreated  = $True
			write-host "Log file created..."
		}
	}
	else
	{
		[string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
		Add-Content -Path $FilePath -Value $info
	}
}

try
{
	$siteUrl = "http://dummySite"
	write-log "Accessing web with url" $siteUrl
	$web = Get-SPWeb -identity $siteUrl
}
catch
{
write-host "Error: Please check the log file"  -ForegroundColor Red
write-log ("!!ERROR!!",$error[0])
}

Oveview of write-log

The above function uses two variables
$LogPath the directory where Logfile should be saved
Ex:
$LogPath = “C:\Custom\Projects\AdiCodes”
Note: The directory path should exist, otherwise the function will not log information

$LogFileName the log file name.
Ex:
$LogFileName = “AdisGroupSiteNewLog.log”
This file will be created if does not exists and appends data to that file

There are common scenarios where the script is scheduled as job. At that time we may require to create
log file based on the date so that it will be useful to review. More likely we can say that, lets review
today’s log file.
Set the $LogFileName as follows to create a new log file every day

$LogFileName = "AdisGroupSiteNewLog-{0}.log" -f $DATE.ToString("yyyyMMdd")

If we want to create the log file in the directory where script file is there, then set $LogPath as

$LogPath= Split-Path -parent $MyInvocation.MyCommand.Definition

Conclusion

This is one of the best useful method to any developer. Any script you write, make it mandatory that it should contain logging functionality.
You can observe how usefull this will be when deploying in staging environments or when working with large script files. Personally while developing projects and writing scripts for them, I never wrote a script without write-log function. Let me know your comments if you want any information and Happy Coding :)

References

Don’t miss Best articles of Powershell

April 28, 2012 В· Adi В· No Comments
Tags: , ,  В· Posted in: Logging, Packaging and Deployment, Powershell, Sharepoint 2010

Leave a Reply

What is 6 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)