Click here to Skip to main content
15,886,026 members
Articles / Web Development / IIS

Build a PowerShell cmdlet

Rate me:
Please Sign up or sign in to vote.
4.20/5 (10 votes)
20 Oct 2007CPOL6 min read 79.5K   623   28  
Learn how to build a simple Windows PowerShell cmdlet, use parameters, wildcards, CustomPSSnapin, and the Extended Type System.
using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using System.ComponentModel;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;

namespace codeproject
{
    [RunInstaller(true)]
    public class codeprojectSnapIn : 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 codeprojectSnapIn()
            : base()
        {
            formats.Add(new FormatConfigurationEntry(@"d:\codeproject\format.ps1xml"));
            cmdlets.Add(new CmdletConfigurationEntry("get-ws",typeof(getWebsites),null));
        }
        public override string Name
        {
            get { return "cpdemo"; }
        }
        public override string Vendor
        {
            get { return "Shahar Gvirtz"; }
        }
        public override string VendorResource
        {
            get { return "Shahar Gvirtz"; }
        }
        public override string Description
        {
            get { return "Code Project Demo"; }
        }
        public override string DescriptionResource
        {
            get { return "Code Project Demo"; }
        }
                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; }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions