Tuesday, April 21, 2009
I’m going to show you how easy it is to build a Windows PowerShell Cmdlet using Visual Studio 2008, C# and my PowerShell Cmdlet templates available on CodePlex.
  1. Download my Windows PowerShell Cmdlet templates from CodePlex

  2. Run the VSI installer to install the project and item templates

  3. Open Visual Studio 2008 and select File->New->Project



  4. From the New Project dialog select  the ‘PowerShellCmdlet’ template



  5. Now right click on the project and select ‘Add New Item’. This will show the ‘Add New Item’ dialog. You can see that there are class templates for PSCmdlets, Cmdlets, SnapIns and XML helper files. For now select the Cmdlet template.



  6. The ProcessRecord method performs the actual processing for the Cmdlet, in this example we will just call the WriteObject method to display the ‘Hello World’ message.

    using System.Management.Automation;
    
    namespace HelloPowerShell
    {
        [Cmdlet(VerbsCommon.Get, "HelloCmdlet")]
        public class HelloCmdlet : Cmdlet
        {
            protected override void ProcessRecord()
            {
                WriteObject("Hello World!");
            }
        }
    }
    


  7. Again right click on the project and select add new item, this time select the ‘PowerShellCmdlet Help XML’. The template will automatically prefix ‘.dll-help.xml’ therefore you should just type ‘Get-Hello’ as the name.

    <?xml version="1.0" encoding="utf-8" ?>
    <helpItems xmlns="http://msh" schema="maml">
      <command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" 
    xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10"
    xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10"> <command:details> <command:name> hellocmdlet </command:name> <maml:description> <maml:para>hellocmdlet</maml:para> </maml:description> <maml:copyright> <maml:para>Copyright</maml:para> </maml:copyright> <command:verb></command:verb> <command:noun></command:noun> </command:details> <maml:description> <maml:para> hellocmdlet description </maml:para> </maml:description> </command:command> </helpItems>

  8. Now add another new item and select the ‘PowerShellCmdlet SnapIn’ template. Call this ‘HelloSnapIn.cs’

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    
    namespace HelloPowerShell
    {
        [RunInstaller(true)]
        public class HelloSnapIn : CustomPSSnapIn
        {
            private Collection<CmdletConfigurationEntry> _cmdlets;
    
            /// <summary>
            /// Gets description of powershell snap-in.
            /// </summary>
            public override string Description
            {
                get { return "A Description of HelloCmdlet"; }
            }
    
            /// <summary>
            /// Gets name of power shell snap-in
            /// </summary>
            public override string Name
            {
                get { return "HelloCmdlet"; }
            }
    
            /// <summary>
            /// Gets name of the vendor
            /// </summary>
            public override string Vendor
            {
                get { return ""; }
            }
    
            public override Collection<CmdletConfigurationEntry> Cmdlets
            {
                get
                {
                    if (null == _cmdlets)
                    {
                        _cmdlets = new Collection<CmdletConfigurationEntry>();
                        _cmdlets.Add(new CmdletConfigurationEntry
                          ("Get-HelloCmdlet", typeof(HelloCmdlet), "Get-HelloCmdlet.dll-Help.xml"));
                    }
                    return _cmdlets;
                }
            }
    
        }
    }
    


  9. By default the SnapIn template fills out some sample information using MyCmdlet as the name simply change this to refer to Get-HelloCmdlet.

  10. You can build the project and get a dll

  11. Now start Windows PowerShell and run with administrator privileges as you will be installing a Cmdlet.

  12. If you are using a 64-bit version of windows issue the command:
    Set-Alias installutil C:\Windows\Microsoft.NET\Framework64\v2.0.50727\InstallUtil.exe
    If you have a 32-bit version of windows issues the command:
    Set-Alias installutil C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe

  13. Now install your Cmdlet with the command:
    installutil HelloPowerShell.dll

  14. You can verify that the Cmdlet has been installed with the command:
    Get-PSSnapin -Registered

  15. Add the snap-in to your shell with:
    Add-PSSnapin HelloCmdlet

  16. You can run the Cmdlet with the command Get-HelloCmdlet and you should see the message “Hello World!”

For more details on installing and register your Cmdlet refer to: http://msdn.microsoft.com/en-us/library/ms714644(VS.85).aspx

Comments are closed.