File size: 19,474 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Management.Automation.Runspaces;
using Microsoft.PowerShell.Commands.Internal.Format;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Enum for SelectionMode parameter.
/// </summary>
public enum OutputModeOption
{
/// <summary>
/// None is the default and it means OK and Cancel will not be present
/// and no objects will be written to the pipeline.
/// The selectionMode of the actual list will still be multiple.
/// </summary>
None,
/// <summary>
/// Allow selection of one single item to be written to the pipeline.
/// </summary>
Single,
/// <summary>
///Allow select of multiple items to be written to the pipeline.
/// </summary>
Multiple
}
/// <summary>
/// Implementation for the Out-GridView command.
/// </summary>
[Cmdlet(VerbsData.Out, "GridView", DefaultParameterSetName = "PassThru", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2109378")]
public class OutGridViewCommand : PSCmdlet, IDisposable
{
#region Properties
private const string DataNotQualifiedForGridView = "DataNotQualifiedForGridView";
private const string RemotingNotSupported = "RemotingNotSupported";
private TypeInfoDataBase _typeInfoDataBase;
private PSPropertyExpressionFactory _expressionFactory;
private OutWindowProxy _windowProxy;
private GridHeader _gridHeader;
#endregion Properties
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="OutGridViewCommand"/> class.
/// </summary>
public OutGridViewCommand()
{
}
#endregion Constructors
#region Input Parameters
/// <summary>
/// This parameter specifies the current pipeline object.
/// </summary>
[Parameter(ValueFromPipeline = true)]
public PSObject InputObject { get; set; } = AutomationNull.Value;
/// <summary>
/// Gets/sets the title of the Out-GridView window.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public string Title { get; set; }
/// <summary>
/// Get or sets a value indicating whether the cmdlet should wait for the window to be closed.
/// </summary>
[Parameter(ParameterSetName = "Wait")]
public SwitchParameter Wait { get; set; }
/// <summary>
/// Get or sets a value indicating whether the selected items should be written to the pipeline
/// and if it should be possible to select multiple or single list items.
/// </summary>
[Parameter(ParameterSetName = "OutputMode")]
public OutputModeOption OutputMode { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the selected items should be written to the pipeline.
/// Setting this to true is the same as setting the OutputMode to Multiple.
/// </summary>
[Parameter(ParameterSetName = "PassThru")]
public SwitchParameter PassThru
{
get { return OutputMode == OutputModeOption.Multiple ? new SwitchParameter(true) : new SwitchParameter(false); }
set { this.OutputMode = value.IsPresent ? OutputModeOption.Multiple : OutputModeOption.None; }
}
#endregion Input Parameters
#region Public Methods
/// <summary>
/// Provides a one-time, pre-processing functionality for the cmdlet.
/// </summary>
protected override void BeginProcessing()
{
// Set up the ExpressionFactory
_expressionFactory = new PSPropertyExpressionFactory();
// If the value of the Title parameter is valid, use it as a window's title.
if (this.Title != null)
{
_windowProxy = new OutWindowProxy(this.Title, OutputMode, this);
}
else
{
// Using the command line as a title.
_windowProxy = new OutWindowProxy(this.MyInvocation.Line, OutputMode, this);
}
// Load the Type info database.
_typeInfoDataBase = this.Context.FormatDBManager.GetTypeInfoDataBase();
}
/// <summary>
/// Blocks depending on the wait and selected.
/// </summary>
protected override void EndProcessing()
{
base.EndProcessing();
if (_windowProxy == null)
{
return;
}
// If -Wait is used or outputMode is not None we have to wait for the window to be closed
// The pipeline will be blocked while we don't return
if (this.Wait || this.OutputMode != OutputModeOption.None)
{
_windowProxy.BlockUntilClosed();
}
// Output selected items to pipeline.
List<PSObject> selectedItems = _windowProxy.GetSelectedItems();
if (this.OutputMode != OutputModeOption.None && selectedItems != null)
{
foreach (PSObject selectedItem in selectedItems)
{
if (selectedItem == null)
{
continue;
}
PSPropertyInfo originalObjectProperty = selectedItem.Properties[OutWindowProxy.OriginalObjectPropertyName];
if (originalObjectProperty == null)
{
return;
}
this.WriteObject(originalObjectProperty.Value, false);
}
}
}
/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessRecord()
{
if (InputObject == null || InputObject == AutomationNull.Value)
{
return;
}
if (InputObject.BaseObject is IDictionary dictionary)
{
// Dictionaries should be enumerated through because the pipeline does not enumerate through them.
foreach (DictionaryEntry entry in dictionary)
{
ProcessObject(PSObjectHelper.AsPSObject(entry));
}
}
else
{
ProcessObject(InputObject);
}
}
/// <summary>
/// StopProcessing is called close the window when Ctrl+C in the command prompt.
/// </summary>
protected override void StopProcessing()
{
if (this.Wait || this.OutputMode != OutputModeOption.None)
{
_windowProxy.CloseWindow();
}
}
/// <summary>
/// Converts the provided PSObject to a string preserving PowerShell formatting.
/// </summary>
/// <param name="liveObject">PSObject to be converted to a string.</param>
internal string ConvertToString(PSObject liveObject)
{
StringFormatError formatErrorObject = new();
string smartToString = PSObjectHelper.SmartToString(liveObject,
_expressionFactory,
InnerFormatShapeCommand.FormatEnumerationLimit(),
formatErrorObject);
if (formatErrorObject.exception != null)
{
// There was a formatting error that should be sent to the console.
this.WriteError(
new ErrorRecord(
formatErrorObject.exception,
"ErrorFormattingType",
ErrorCategory.InvalidResult,
liveObject)
);
}
return smartToString;
}
#endregion Public Methods
#region Private Methods
/// <summary>
/// Execute formatting on a single object.
/// </summary>
/// <param name="input">Object to process.</param>
private void ProcessObject(PSObject input)
{
// Make sure the OGV window is not closed.
if (_windowProxy.IsWindowClosed())
{
LocalPipeline pipeline = (LocalPipeline)this.Context.CurrentRunspace.GetCurrentlyRunningPipeline();
if (pipeline != null && !pipeline.IsStopping)
{
// Stop the pipeline cleanly.
pipeline.StopAsync();
}
return;
}
object baseObject = input.BaseObject;
// Throw a terminating error for types that are not supported.
if (baseObject is ScriptBlock ||
baseObject is SwitchParameter ||
baseObject is PSReference ||
baseObject is FormatInfoData ||
baseObject is PSObject)
{
ErrorRecord error = new(
new FormatException(StringUtil.Format(FormatAndOut_out_gridview.DataNotQualifiedForGridView)),
DataNotQualifiedForGridView,
ErrorCategory.InvalidType,
null);
this.ThrowTerminatingError(error);
}
if (_gridHeader == null)
{
// Columns have not been added yet; Start the main window and add columns.
_windowProxy.ShowWindow();
_gridHeader = GridHeader.ConstructGridHeader(input, this);
}
else
{
_gridHeader.ProcessInputObject(input);
}
// Some thread synchronization needed.
Exception exception = _windowProxy.GetLastException();
if (exception != null)
{
ErrorRecord error = new(
exception,
"ManagementListInvocationException",
ErrorCategory.OperationStopped,
null);
this.ThrowTerminatingError(error);
}
}
#endregion Private Methods
internal abstract class GridHeader
{
protected OutGridViewCommand parentCmd;
internal GridHeader(OutGridViewCommand parentCmd)
{
this.parentCmd = parentCmd;
}
internal static GridHeader ConstructGridHeader(PSObject input, OutGridViewCommand parentCmd)
{
if (DefaultScalarTypes.IsTypeInList(input.TypeNames) ||
!OutOfBandFormatViewManager.HasNonRemotingProperties(input))
{
return new ScalarTypeHeader(parentCmd, input);
}
return new NonscalarTypeHeader(parentCmd, input);
}
internal abstract void ProcessInputObject(PSObject input);
}
internal sealed class ScalarTypeHeader : GridHeader
{
private readonly Type _originalScalarType;
internal ScalarTypeHeader(OutGridViewCommand parentCmd, PSObject input) : base(parentCmd)
{
_originalScalarType = input.BaseObject.GetType();
// On scalar types the type name is used as a column name.
this.parentCmd._windowProxy.AddColumnsAndItem(input);
}
internal override void ProcessInputObject(PSObject input)
{
if (!_originalScalarType.Equals(input.BaseObject.GetType()))
{
parentCmd._gridHeader = new HeteroTypeHeader(base.parentCmd, input);
}
else
{
// Columns are already added; Add the input PSObject as an item to the underlying Management List.
base.parentCmd._windowProxy.AddItem(input);
}
}
}
internal sealed class NonscalarTypeHeader : GridHeader
{
private readonly AppliesTo _appliesTo = null;
internal NonscalarTypeHeader(OutGridViewCommand parentCmd, PSObject input) : base(parentCmd)
{
// Prepare a table view.
TableView tableView = new();
tableView.Initialize(parentCmd._expressionFactory, parentCmd._typeInfoDataBase);
// Request a view definition from the type database.
ViewDefinition viewDefinition = DisplayDataQuery.GetViewByShapeAndType(parentCmd._expressionFactory, parentCmd._typeInfoDataBase, FormatShape.Table, input.TypeNames, null);
if (viewDefinition != null)
{
// Create a header using a view definition provided by the types database.
parentCmd._windowProxy.AddColumnsAndItem(input, tableView, (TableControlBody)viewDefinition.mainControl);
// Remember all type names and type groups the current view applies to.
_appliesTo = viewDefinition.appliesTo;
}
else
{
// Create a header using only the input object's properties.
parentCmd._windowProxy.AddColumnsAndItem(input, tableView);
_appliesTo = new AppliesTo();
// Add all type names except for Object and MarshalByRefObject types because they are too generic.
// Leave the Object type name if it is the only type name.
int index = 0;
foreach (string typeName in input.TypeNames)
{
if (index > 0 && (typeName.Equals(typeof(object).FullName, StringComparison.OrdinalIgnoreCase) ||
typeName.Equals(typeof(MarshalByRefObject).FullName, StringComparison.OrdinalIgnoreCase)))
{
break;
}
_appliesTo.AddAppliesToType(typeName);
index++;
}
}
}
internal override void ProcessInputObject(PSObject input)
{
// Find out if the input has matching types in the this.appliesTo collection.
foreach (TypeOrGroupReference typeOrGroupRef in _appliesTo.referenceList)
{
if (typeOrGroupRef is TypeReference)
{
// Add deserialization prefix.
string deserializedTypeName = typeOrGroupRef.name;
Deserializer.AddDeserializationPrefix(ref deserializedTypeName);
for (int i = 0; i < input.TypeNames.Count; i++)
{
if (typeOrGroupRef.name.Equals(input.TypeNames[i], StringComparison.OrdinalIgnoreCase)
|| deserializedTypeName.Equals(input.TypeNames[i], StringComparison.OrdinalIgnoreCase))
{
// Current view supports the input's Type;
// Add the input PSObject as an item to the underlying Management List.
base.parentCmd._windowProxy.AddItem(input);
return;
}
}
}
else
{
// Find out if the input's Type belongs to the current TypeGroup.
// TypeGroupReference has only a group's name, so use the database to get through all actual TypeGroup's.
List<TypeGroupDefinition> typeGroupList = base.parentCmd._typeInfoDataBase.typeGroupSection.typeGroupDefinitionList;
foreach (TypeGroupDefinition typeGroup in typeGroupList)
{
if (typeGroup.name.Equals(typeOrGroupRef.name, StringComparison.OrdinalIgnoreCase))
{
// A matching TypeGroup is found in the database.
// Find out if the input's Type belongs to this TypeGroup.
foreach (TypeReference typeRef in typeGroup.typeReferenceList)
{
// Add deserialization prefix.
string deserializedTypeName = typeRef.name;
Deserializer.AddDeserializationPrefix(ref deserializedTypeName);
if (input.TypeNames.Count > 0
&& (typeRef.name.Equals(input.TypeNames[0], StringComparison.OrdinalIgnoreCase)
|| deserializedTypeName.Equals(input.TypeNames[0], StringComparison.OrdinalIgnoreCase)))
{
// Current view supports the input's Type;
// Add the input PSObject as an item to the underlying Management List.
base.parentCmd._windowProxy.AddItem(input);
return;
}
}
}
}
}
}
// The input's Type is not supported by the current view;
// Switch to the Hetero Type view.
parentCmd._gridHeader = new HeteroTypeHeader(base.parentCmd, input);
}
}
internal sealed class HeteroTypeHeader : GridHeader
{
internal HeteroTypeHeader(OutGridViewCommand parentCmd, PSObject input) : base(parentCmd)
{
// Clear all existed columns and add Type and Value columns.
this.parentCmd._windowProxy.AddHeteroViewColumnsAndItem(input);
}
internal override void ProcessInputObject(PSObject input)
{
this.parentCmd._windowProxy.AddHeteroViewItem(input);
}
}
/// <summary>
/// Implements IDisposable logic.
/// </summary>
/// <param name="isDisposing">True if being called from Dispose.</param>
private void Dispose(bool isDisposing)
{
if (isDisposing)
{
if (_windowProxy != null)
{
_windowProxy.Dispose();
_windowProxy = null;
}
}
}
/// <summary>
/// Dispose method in IDisposable.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
}
}
|