If you are able, I would recommend going with the βC # project, which builds the PowerShell module. There are various advantages depending on your situation, including:
- Security at compile time. I know that some developers would prefer compiled / strongly typed languages ββdue to extra security
- Easier to write automated tests. This may be controversial, but in my opinion having libraries like nUnit and other testing structures are a huge plus.
- Knowledge of languages. I came across many who are familiar with C #, but not powershell, and therefore they are a struggle.
To get started, I found in this article . Basically, he says, to add a link to System.Management.Automation.dll to your project, and then a very simple cmdlet would look like this:
using System; using System.Collection.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Management.Automation; namespace MyModule { [Cmdlet(VerbsCommon.Get, "Saluation")] public class GetSaluation : PSCmdlet { private string[] nameCollection; [Parameter( Mandatory = true, ValueFromPipelineByPropertyName = true, ValueFromPipelin = true, Position = 0, HelpMessage = "Name to get salutation for." )] [Alias("Person", "FirstName")] public string[] Name { get { return nameCollection;} set { nameCollection = value;} } protected override void BeginProcessing() { base.BeginProcessing(); } protected override void ProcessRecord() { foreach (string name in nameCollection) { WriteVerbose("Creating salutation for " + name); string salutation = "Hello, " + name; WriteObject(salutation); } } protected override void EndProcessing() { base.EndProcessing(); } }
Then, to use this module, open a command prompt, go to where your dll is built, and use the Import-Module cmdlet.
And then for your specific question ( How to reuse parameter pairs with different cmdlets? ), You can have a base cmdlet that defines parameters, and all cmdlets that you want to write can inherit from the base class.
Travis
source share