Over a million developers have joined DZone.

Windows Powershell Cmdlet - Creation And Registration

·
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"; }
        }

    }
}
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}