DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Windows Powershell Cmdlet - Creation And Registration

09.10.2006
| 4909 views |
  • submit to reddit
        Adapted from "Windows PowerShell Programmer's Guide", at http://windowssdk.msdn.microsoft.com/en-us/library/ms714674.aspx

// Reference added to "System.Management.Automation.dll" in "C:\Program Files\Windows PowerShell\v1.0":
using System.Management.Automation;

namespace CmdletTest
{
    [Cmdlet(VerbsCommon.Get, "Hjnntaao")]
    public class GetHjnntaaoCommand : Cmdlet
    {
        // BeginProcessing - pre-processing/set-up

        // If accepts pipeline input, must override ProcessRecord and, optionally, EndProcessing

        // If does not take pipline input, should override EndProcessing

        // Cmdlets should never call WriteLine, or equivalent.
        protected override void ProcessRecord()
        {
            Process[] processes = Process.GetProcesses();

            
            WriteObject(processes);
        }
    }

    /* 
       This method of registering the Cmdlet with PowerShell extends the shell using snap-ins. This should
       be the default way of registering Cmdlets. The other way, registering with a custom shell,
       should only be used to create executable files.
     
       To install:
       1) installutil CmdletTest.dll
       2) Verify the snap in is in the list of registered snap-ins awaiting to be loaded by
          running "get-pssnapin -registered"
       3) Add the snap in to the shell by using "add-pssnapin "GetHjnntaaoPSSSnapIn01"
     */
    [RunInstaller(true)]
    public class GetHjnntaaoPSSnapIn01 : PSSnapIn
    {
        public override string Name
        {
            get { return "GetHjnntaaoPSSnapIn01"; }
        }

        public override string Vendor
        {
            get { return "HjnntaaoCorp"; }
        }

        public override string Description
        {
            get { return "Hjnntaao's Cmdlet"; }
        }

    }
}