PortiBlog

Creating your own Powershell CmdLet for SharePoint

25 januari 2016

What are we trying to accomplish?

I was asked to list all Areas and Category within the Diagnostic Logging at a customer request.

Because I was using PowerShell to retrieve all other settings I made the choice to create a custom CmdLet because there isn’t any available by default to retrieve all Areas and Category. Below you'll find the source code of my CmdLet, feel free to use it. 

I've also added some explanation of how to add the CmdLet and what the CmdLet can do for you.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint.Administration;
using System.Data.SqlClient;
using System.Management.Automation;


class DiagnosticsCategory
{
    private string _Name;
    private uint _Id;
    private TraceSeverity _TraceSeverity;
    private EventSeverity _EventSeverity;
    private DiagnosticsArea _Area;

    public string Name
    {
        get { return this._Name; }
        set { this._Name = value; }
    }
    public uint Id
    {
        get { return this._Id; }
        set { this._Id = value; }
    }
    public TraceSeverity TraceSeverity
    {
        get { return this._TraceSeverity; }
        set { this._TraceSeverity = value; }
    }
    public EventSeverity EventSeverity
    {
        get { return this._EventSeverity; }
        set { this._EventSeverity = value; }
    }
    public DiagnosticsArea Area
    {
        get { return this._Area; }
        set { this._Area = value; }
    }
}
class DiagnosticsArea
{
    private string _Name;
    private uint _Id;

    public string Name
    {
        get { return this._Name; }
        set { this._Name = value; }
    }
    public uint Id
    {
        get { return this._Id; }
        set { this._Id = value; }
    }
}

class DiagnosticLogging
{

    private static List<DiagnosticsArea> GetAreas()
    {
        List<DiagnosticsArea> areas = new List<DiagnosticsArea>();
        SPServiceCollection services = SPFarm.Local.Services;
        var diagServices = from service in services
                           where service is SPDiagnosticsServiceBase
                           select service;

        foreach (SPDiagnosticsServiceBase service in diagServices)
        {
            SPDiagnosticsCollection<SPDiagnosticsArea> allAreas = service.Areas;
            foreach (SPDiagnosticsArea item in allAreas)
            {
                DiagnosticsArea Area = new DiagnosticsArea();
                Area.Name = item.Name;
                Area.Id = item.Id;
                areas.Add(Area);
            }
        }
        return areas;
    }
    private static List<DiagnosticsCategory> GetCategories(DiagnosticsArea Are_a)
    {
        List<DiagnosticsCategory> categories = new List<DiagnosticsCategory>();
        SPServiceCollection services = SPFarm.Local.Services;
        var diagServices = from service in services
                           where service is SPDiagnosticsServiceBase
                           select service;
        foreach (SPDiagnosticsServiceBase service in diagServices)
        {
            SPDiagnosticsCollection<SPDiagnosticsArea> allAreas = service.Areas;
            foreach (SPDiagnosticsArea item in allAreas)
            {
                DiagnosticsArea Area = new DiagnosticsArea();
                Area.Name = item.Name;
                Area.Id = item.Id;

                if (string.IsNullOrEmpty(Are_a.Name) || Are_a.Name == Area.Name)
                {
                    foreach (SPDiagnosticsCategory item2 in item.Categories)
                    {
                        DiagnosticsCategory cat = new DiagnosticsCategory();
                        cat.Name = item2.Name;
                        cat.Id = item2.Id;
                        cat.EventSeverity = item2.EventSeverity;
                        cat.TraceSeverity = item2.TraceSeverity;
                        cat.Area = Area;
                        categories.Add(cat);
                    }
                }

            }
        }
        return categories;
    }

    [Cmdlet(VerbsCommon.Get, "DiagnosticLoggingAreas")]
    public class Get_Areas : PSCmdlet
    {
        protected override void EndProcessing()
        {
            var areas = GetAreas();
            this.WriteObject(areas, true);
        }
    }

    [Cmdlet(VerbsCommon.Get, "DiagnosticLoggingCategories")]
    public class Get_Categories : PSCmdlet
    {
        [System.Management.Automation.Parameter(Position = 0, Mandatory = false)]
        public DiagnosticsArea Area;

        protected override void ProcessRecord()
        {
            if (Area == null) { Area = new DiagnosticsArea(); }
            var cats = GetCategories(Area);
            this.WriteObject(cats, true);
        }
    }
}

After compiling the assembly load the module by using the following command within PowerShell

Import-Module <path>\<assemblyname>.dll

By typing Get-Module the output will show all available commands from the imported module.

Get-DiagnosticLoggingAreas

The list above shows all areas from Diagnostic Logging

2 Properties are available for an Area: Name and Id.

Get-DiagnosticLoggingCategories

The list above shows all Categories from Diagnostic Logging.

5 Properties are available for an Category: Area, EventSeverity, Id, Name, TraceSeverity

After retrieving all areas you can pass one area to the getcategories to retrieve all categories from that specific Area.

Submit a comment