GetAnswerCommand.cs
using System;
using System.Management.Automation; using System.Management.Automation.Runspaces; namespace DotupPsCore { [Cmdlet(VerbsCommon.Get, "Answer")] [OutputType(typeof(string))] public class GetAnswerCommand : PSCmdlet { [Parameter( Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] public string Question { get; set; } [Parameter( Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] public string DefaultAnswer { get; set; } // 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() { if (DefaultAnswer != null) { Question = $"{Question} ({DefaultAnswer}):"; } WriteObject(Question); var result = this.InvokeCommand.InvokeScript("Read-Host"); WriteObject(result); } // 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!"); } } } |