Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
Are there any VS templetes availaible for developing CMDLets

Answered Are there any VS templetes availaible for developing CMDLets

  • Monday, January 04, 2010 11:46 AM
     
     
    Hi,

    Are there any VS project templetes availaible for developement of CMDLets.

    - Girija

All Replies

  • Monday, January 04, 2010 2:18 PM
     
     Answered
    There's a few out there, but this is the one I use.  The tutorial walks you through the process nicely:

    Recently I've been doing my best to user powershell to create my cmdlets.  It's much easier and has access to all of the .net code anyway.  But if you have a lot of prexisting c# code VS makes it easier.
    • Marked As Answer by Girija Shankar Wednesday, January 06, 2010 10:00 AM
    •  
  • Monday, January 04, 2010 3:23 PM
     
     
    Hey,

    Thanks. In the templetes you can create a One CMDLet per project. I wanted to know if I can create multiple CMDLets in single project or is it that to have multiple CMDLets, each one of them will be in seperate projects under a single solution ?

    Another question I had, If i have two CMDLets and both of them use same parameter (With the same properties), how can i declare them once and re use them. or is it that I will have to create them seperately for different cmdlets ?

    Thanks..

    - Girija
  • Monday, January 04, 2010 3:44 PM
     
     Answered Has Code
    I'm not certain about question 2, but I'm pretty sure you must declare them for each cmdlet class.

    For question 1 you can add a second cmdlet like this:

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

    Then within your snapin add the following:
    _cmdlets.Add(new CmdletConfigurationEntry("Get-HelloCmdlet", typeof(HelloCmdlet), "Get-HelloCmdlet.dll-Help.xml"));
    _cmdlets.Add(new CmdletConfigurationEntry("Get-HelloCmdlet2", typeof(HelloCmdlet2), "Get-HelloCmdlet.dll-Help.xml"));

    I'm not sure if you've read through the documentation: http://msdn.microsoft.com/en-us/library/dd878294(VS.85).aspx.  The templates are a great start, but they really just give you a quick way to start doing what's in the above documentation.  You should also read through the powershell module documentation for binary files.  The templates do not have the module manifest file.  Since powershell binaries can now be used as modules I think it's a better way to go than with the snap-ins, but both ways will work. 
  • Monday, January 04, 2010 3:47 PM
     
     Answered
    I said "better way to go" when referring to modules vs. snapins... That's from a user perspective.  I think modules are much simpler as a consumer of 3rd-party code than snapins.  It's actually an additional step when developing to create the manifest file, but I think it's easy enough to do.
  • Monday, January 04, 2010 4:00 PM
    Moderator
     
     Answered
    Another question I had, If i have two CMDLets and both of them use same parameter (With the same properties), how can i declare them once and re use them. or is it that I will have to create them seperately for different cmdlets ?

    **NOT A PROGRAMMING EXPERT**

    You must redefine your parameters for each cmdlet.

    Now that being said, I *think* you could create your own class and inherit from that.  For example, the common parameters (see "help about_commonparameters).
    • Marked As Answer by Girija Shankar Wednesday, January 06, 2010 10:00 AM
    •