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