Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
OutConsole.cs213 linesDownload Raw Back to out-console
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections.Generic;6using System.Management.Automation;7using System.Management.Automation.Host;8using System.Management.Automation.Internal;9 10using Microsoft.PowerShell.Commands.Internal.Format;11 12namespace Microsoft.PowerShell.Commands13{14    /// <summary>15    /// Null sink to absorb pipeline output.16    /// </summary>17    [Cmdlet("Out", "Null", SupportsShouldProcess = false,18        HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096792", RemotingCapability = RemotingCapability.None)]19    public class OutNullCommand : PSCmdlet20    {21        /// <summary>22        /// This parameter specifies the current pipeline object.23        /// </summary>24        [Parameter(ValueFromPipeline = true)]25        public PSObject InputObject { get; set; } = AutomationNull.Value;26 27        /// <summary>28        /// Do nothing.29        /// </summary>30        protected override void ProcessRecord()31        {32            // explicitely overridden:33            // do not do any processing34        }35    }36 37    /// <summary>38    /// Implementation for the out-default command39    /// this command it implicitly inject by the40    /// powershell host at the end of the pipeline as the41    /// default sink (display to console screen)42    /// </summary>43    [Cmdlet(VerbsData.Out, "Default", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096486", RemotingCapability = RemotingCapability.None)]44    public class OutDefaultCommand : FrontEndCommandBase45    {46        /// <summary>47        /// Determines whether objects should be sent to API consumers.48        /// This command is automatically added to the pipeline when PowerShell is transcribing and49        /// invoked via API. This ensures that the objects pass through the formatting and output50        /// system, but can still make it to the API consumer.51        /// </summary>52        [Parameter]53        public SwitchParameter Transcript { get; set; }54 55        /// <summary>56        /// Set inner command.57        /// </summary>58        public OutDefaultCommand()59        {60            this.implementation = new OutputManagerInner();61        }62 63        /// <summary>64        /// Just hook up the LineOutput interface.65        /// </summary>66        protected override void BeginProcessing()67        {68            var lineOutput = new ConsoleLineOutput(Host, false, new TerminatingErrorContext(this));69 70            ((OutputManagerInner)this.implementation).LineOutput = lineOutput;71 72            if (this.CommandRuntime is MshCommandRuntime mrt)73            {74                mrt.MergeUnclaimedPreviousErrorResults = true;75            }76 77            if (Transcript)78            {79                _transcribeOnlyCookie = Host.UI.SetTranscribeOnly();80            }81 82            // This needs to be done directly through the command runtime, as Out-Default83            // doesn't actually write pipeline objects.84            base.BeginProcessing();85 86            if (Context.CurrentCommandProcessor.CommandRuntime.OutVarList != null)87            {88                _outVarResults = new List<PSObject>();89            }90        }91 92        /// <summary>93        /// Process the OutVar, if set.94        /// </summary>95        protected override void ProcessRecord()96        {97            if (Transcript)98            {99                WriteObject(InputObject);100            }101 102            // This needs to be done directly through the command runtime, as Out-Default103            // doesn't actually write pipeline objects.104            if (_outVarResults != null)105            {106                object inputObjectBase = PSObject.Base(InputObject);107 108                // Ignore errors and formatting records, as those can't be captured109                if (inputObjectBase != null &&110                    inputObjectBase is not ErrorRecord &&111                    !inputObjectBase.GetType().FullName.StartsWith(112                        "Microsoft.PowerShell.Commands.Internal.Format", StringComparison.OrdinalIgnoreCase))113                {114                    _outVarResults.Add(InputObject);115                }116            }117 118            base.ProcessRecord();119        }120 121        /// <summary>122        /// Swap the outVar with what we've processed, if OutVariable is set.123        /// </summary>124        protected override void EndProcessing()125        {126            // This needs to be done directly through the command runtime, as Out-Default127            // doesn't actually write pipeline objects.128            if ((_outVarResults != null) && (_outVarResults.Count > 0))129            {130                Context.CurrentCommandProcessor.CommandRuntime.OutVarList.Clear();131                foreach (object item in _outVarResults)132                {133                    Context.CurrentCommandProcessor.CommandRuntime.OutVarList.Add(item);134                }135 136                _outVarResults = null;137            }138 139            base.EndProcessing();140        }141 142        /// <summary>143        /// Revert transcription state on Dispose.144        /// </summary>145        protected override void InternalDispose()146        {147            try148            {149                base.InternalDispose();150            }151            finally152            {153                if (_transcribeOnlyCookie != null)154                {155                    _transcribeOnlyCookie.Dispose();156                    _transcribeOnlyCookie = null;157                }158            }159        }160 161        private List<PSObject> _outVarResults = null;162        private IDisposable _transcribeOnlyCookie = null;163    }164 165    /// <summary>166    /// Implementation for the out-host command.167    /// </summary>168    [Cmdlet(VerbsData.Out, "Host", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096863", RemotingCapability = RemotingCapability.None)]169    public class OutHostCommand : FrontEndCommandBase170    {171        #region Command Line Parameters172 173        /// <summary>174        /// Non positional parameter to specify paging.175        /// </summary>176        private bool _paging;177 178        #endregion179 180        /// <summary>181        /// Constructor of OutHostCommand.182        /// </summary>183        public OutHostCommand()184        {185            this.implementation = new OutputManagerInner();186        }187 188        /// <summary>189        /// Optional, non positional parameter to specify paging190        /// FALSE: names only191        /// TRUE: full info.192        /// </summary>193        [Parameter]194        public SwitchParameter Paging195        {196            get { return _paging; }197 198            set { _paging = value; }199        }200 201        /// <summary>202        /// Just hook up the LineOutput interface.203        /// </summary>204        protected override void BeginProcessing()205        {206            var lineOutput = new ConsoleLineOutput(Host, _paging, new TerminatingErrorContext(this));207 208            ((OutputManagerInner)this.implementation).LineOutput = lineOutput;209            base.BeginProcessing();210        }211    }212}213