TestSampleCmdletCommand.cs

using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
 
namespace BcAdmin
{
    /// <summary>
    /// This cmdlet tests the sample functionality.
    /// </summary>
    /// <description>
    /// The Test-SampleCmdlet cmdlet demonstrates how to create a cmdlet with parameters and output
    /// </description>
    [Cmdlet(VerbsDiagnostic.Test,"SampleCmdlet")]
    [OutputType(typeof(FavoriteStuff))]
    public class TestSampleCmdletCommand : PSCmdlet
    {
        /// <summary>
        /// Gets or sets the favorite number.
        /// </summary>
        /// <description>
        /// This parameter specifies the favorite number of the user.
        /// </description>
        [Parameter(
            Mandatory = true,
            Position = 0,
            ValueFromPipeline = true,
            ValueFromPipelineByPropertyName = true)]
        public int FavoriteNumber { get; set; }
 
        /// <summary>
        /// Gets or sets the favorite pet.
        /// </summary>
        /// <description>
        /// This parameter specifies the favorite pet of the user. Valid values are "Cat", "Dog", and "Horse".
        /// </description>
        [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()
        {
            WriteObject(new FavoriteStuff {
                FavoriteNumber = FavoriteNumber,
                FavoritePet = FavoritePet
            });
        }
 
        // 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!");
        }
    }
 
    public class FavoriteStuff
    {
        public int FavoriteNumber { get; set; }
        public string FavoritePet { get; set; }
    }
}