Pablo Galiano :

Subscriptions

<March 2010>
SuMoTuWeThFrSa
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

News

Subscribe to Pablo Galiano by Email

Post Categories

PowerShell (RSS)

Creating a PowerShell cmdlet

Some time ago kzu told me about a shell extension called CleanSources, the purpose of this application is to recursive delete all the files and folders under the \bin and \obj directories for a given path.

This tool is very usefull if we dont want to open a Visual Studio and do a Clean Solution/Project for every source code that we want to clean.

So, based on this idea I will create a PowerShell cmdlet to replicate that functionality.

 

These are the detailed steps:

Create a Visual Studio project:

Create a class library project and add a reference to System.Managment.Automation.dll located at %programfiles%/Windows PowerShell\v1.0 and to System.Configuration.Install.dll

 

Create the cmdlet class:

    [Cmdlet(VerbsCommon.Remove, "Binaries")]

    public class CleanerCmdLet : PSCmdlet

    {

    }

 

Add the cmdlet parameter:

        private string basePath;

 

        [Parameter(Mandatory = true, Position = 0)]

        public string BasePath

        {

            get { return basePath; }

            set { basePath = value; }

        }

 

Implement the cmdlet functionality:

        protected override void ProcessRecord()

        {

            CleanSubDirectory(this.basePath);

        }

 

        private void CleanSubDirectory(string path)

        {

            foreach(string subDirectory in Directory.GetDirectories(path))

            {

                DirectoryInfo info = new DirectoryInfo(subDirectory);

 

                if(info.Name.ToLower().Equals("bin") || info.Name.ToLower().Equals("obj"))

                {

                    try

                    {

                        info.Delete(true);

                    }

                    catch(UnauthorizedAccessException)

                    {

                        //Do nothing if the file is readonly

                    }

                }

                else

                {

                    CleanSubDirectory(subDirectory);

                }

            }

        }

 

Create the snap-in class:

using System;

using System.Collections.Generic;

using System.Text;

using System.Management.Automation;

using System.Management.Automation.Runspaces;

using System.Collections.ObjectModel;

using System.ComponentModel;

 

    [RunInstaller(true)]

    public class RemoveBinariesPSSnapIn : CustomPSSnapIn

    {

        private Collection<CmdletConfigurationEntry> cmdlets = new Collection<CmdletConfigurationEntry>();

        private Collection<ProviderConfigurationEntry> providers = new Collection<ProviderConfigurationEntry>();

        private Collection<TypeConfigurationEntry> types = new Collection<TypeConfigurationEntry>();

        private Collection<FormatConfigurationEntry> formats = new Collection<FormatConfigurationEntry>();

 

        public RemoveBinariesPSSnapIn()

            : base()

        {

            cmdlets.Add(new CmdletConfigurationEntry("remove-binaries", typeof(CleanerCmdLet), null));

        }

 

        public override string Name

        {

            get { return "RemoveBinariesPSSnapIn"; }

        }

 

        public override string Vendor

        {

            get { return "Clarius Consulting"; }

        }

 

        public override string Description

        {

            get { return "This snap-in contains the Remove Binaries cmdlet."; }

        }

 

        public override Collection<CmdletConfigurationEntry> Cmdlets

        {

            get { return cmdlets; }

        }

 

        public override Collection<ProviderConfigurationEntry> Providers

        {

            get { return providers; }

        }

 

        public override Collection<TypeConfigurationEntry> Types

        {

            get { return types; }

        }

 

        public override Collection<FormatConfigurationEntry> Formats

        {

            get { return formats; }

        }

 

    }

 

Deploy it:

Compile the class library project and open a Visual Studio command prompt (using console :)) and run installutil.exe over the compiled assembly

 

Test it:

Open a PowerShell session

Execute the Get-PSSnapin -Registered cmdlet, if the registration process was correct this should be the output of that cmdlet:

PS C:\> Get-PSSnapin -Registered

Name        : RemoveBinariesPSSnapIn
PSVersion   : 1.0
Description : This snap-in contains the Remove Binaries cmdlet.

This means that cmdlet is registered, but we need to add it to our shell configuration:

PS C:\> Add-PSSnapin RemoveBinariesPSSnapIn

The last step is to execute it:

Remove-Binaries C:\Test

 

Enjoy.

 

Pablo

posted Tuesday, August 08, 2006 6:06 PM by pga with 46 Comments