in SharePoint 2010

Error Handling in SharePoint 2010 PowerShell Cmdlets

I love SharePoint 2010 PowerShell Cmdlets. If you are lazy to open Visual Studio and write code (yea right!) then PowerShell is a great tool for you!

For a full list of SharePoint 2010 PowerShell Cmdlets, visit here – http://msdn.microsoft.com/en-us/library/ee534955.aspx

Now that you have started using these SharePoint 2010 cmdlets, how do you handle errors?

The same rules of error handling which applies to normal PowerShell also applies to SharePoint 2010 Cmdlets. If you are still not aware of those basic error handling in PowerShell (like me), then here is a quick introduction of how you can easily handle errors in PowerShell

Basic Example

Below is a simple cmdlet:

$spSite = Get-SPSite “http://chaks”

If there is no site “http://chaks”, PowerShell will throw an error:

Get-SPSite : Cannot find an SPSite object with Id or Url: http://chaks.
At C:UsersAdministratorDocumentsscript.ps1:1 char:21
+ $spSite = Get-SPSite <<<<  "
http://chaks"
    + CategoryInfo          : InvalidData: (Microsoft.Share…SPCmdletGetSite:
   SPCmdletGetSite) [Get-SPSite], SPCmdletPipeBindException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetSite

Since you have used a local variable $spSite , you can also check whether it is Null:

if($spSite -eq $Null)
{
    Write-Host "Cannot find
http://chaks"
}
else
{
    Write-Host "Connected to
http://chaks"
}

This is fine, but PowerShell will still print the error message and then proceed to check whether $spSite is Null and display the appropriate message.

Using ErrorAction and ErrorVariable

Using these two common parameters – ErrorAction and ErrorVariable – PowerShell makes it easy to handle errors.

So, our example now becomes:

$spSite = Get-SPSite “http://chaks”  -ErrorAction SilentlyContinue -ErrorVariable err

if($spSite -eq $Null)

{
    Write-Host "Cannot find
http://chaks"
}
else
{
    Write-Host "Connected to
http://chaks"
}

The result is:

Cannot find http://chaks

Notice that PowerShell no longer displays that long error message!

To know more about the error, you can access the ErrorVariable we used, which in our case will be $err .Notice, in the command we don’t prefix with a $, but you need to now prefix with $ to access the variable in the script.

So:

Write-Host $err

Will display:

Cannot find an SPSite object with Id or Url: http://chaks

The above error message is much readable than our first long geeky error message Smile

So, with error handling:

$spSite = Get-SPSite "http://demo" -ErrorAction SilentlyContinue -ErrorVariable err
if($err -ne $Null)
{
    Write-Host $err
}
else
{
    $hostName = $spSite.HostName
    Write-Host "Site HostName: $hostName"
}

One thing you should really not forget is that the ErrorAction and ErrorVariable common parameters work only with Cmdlets.

More ways

If you still want to explore error handling, then you can use Trap or Try_Catch_Finally

I will also recommend reading this chapter – Chapter 11: Finding and Avoiding ErrorsMaster-PowerShell | With Dr. Tobias Weltner. You can download the book as PDF for free!

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.