SubTemplateCmdlet/MyCmdlet.cs
using System;
using System.Management.Automation; using System.Management.Automation.Runspaces; namespace MyCmdlet { [Cmdlet(VerbsDiagnostic.Test,"SampleCmdlet")] [OutputType(typeof(FavoriteStuff))] public class TestSampleCmdletCommand : PSCmdlet { public class FavoriteStuff { public int FavoriteNumber { get; set; } public string FavoritePet { get; set; } } [Parameter(Mandatory = true,Position = 0,ValueFromPipeline = true,ValueFromPipelineByPropertyName = true)] public int FavoriteNumber { get; set; } [Parameter(Position = 1, ValueFromPipelineByPropertyName = true)] [ValidateSet("Cat", "Dog", "Horse")] public string FavoritePet { get; set; } = "Dog"; // This method gets called once for each cmdlet in the pipeline when the pipeline starts executing protected override void BeginProcessing() { WriteVerbose("Begin!"); } // This method will be called for each input received from the pipeline to this cmdlet; if no input is received, this method is not called protected override void ProcessRecord() { var favoriteStuff = new FavoriteStuff { FavoriteNumber = FavoriteNumber, FavoritePet = FavoritePet }; WriteObject(favoriteStuff); } // This method will be called once at the end of pipeline execution; if no input is received, this method is not called protected override void EndProcessing() { WriteVerbose("End!"); } } } |