Windows-powershell / PowerShell-master /src /Microsoft.PowerShell.Commands.Utility /commands /utility /Get-Error.cs
| // Copyright (c) Microsoft Corporation. | |
| // Licensed under the MIT License. | |
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.Management.Automation; | |
| namespace Microsoft.PowerShell.Commands | |
| { | |
| /// <summary> | |
| /// Class for Get-Error implementation. | |
| /// </summary> | |
| [ | |
| ] | |
| [] | |
| public sealed class GetErrorCommand : PSCmdlet | |
| { | |
| internal const string ErrorParameterSetName = "Error"; | |
| internal const string NewestParameterSetName = "Newest"; | |
| internal const string AliasNewest = "Last"; | |
| internal const string ErrorRecordPSExtendedError = "System.Management.Automation.ErrorRecord#PSExtendedError"; | |
| internal const string ExceptionPSExtendedError = "System.Exception#PSExtendedError"; | |
| /// <summary> | |
| /// Gets or sets the error object to resolve. | |
| /// </summary> | |
| [] | |
| [] | |
| public PSObject InputObject { get; set; } | |
| /// <summary> | |
| /// Gets or sets the number of error objects to resolve starting with newest first. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| public int Newest { get; set; } = 1; | |
| /// <summary> | |
| /// Process the error object. | |
| /// </summary> | |
| protected override void ProcessRecord() | |
| { | |
| var errorRecords = new List<object>(); | |
| var index = 0; | |
| if (InputObject != null) | |
| { | |
| if (InputObject.BaseObject is Exception || InputObject.BaseObject is ErrorRecord) | |
| { | |
| errorRecords.Add(InputObject); | |
| } | |
| } | |
| else | |
| { | |
| var errorVariable = SessionState.PSVariable.Get("error"); | |
| var count = Newest; | |
| ArrayList errors = (ArrayList)errorVariable.Value; | |
| if (count > errors.Count) | |
| { | |
| count = errors.Count; | |
| } | |
| while (count > 0) | |
| { | |
| errorRecords.Add(errors[index]); | |
| index++; | |
| count--; | |
| } | |
| } | |
| index = 0; | |
| bool addErrorIdentifier = errorRecords.Count > 1; | |
| foreach (object errorRecord in errorRecords) | |
| { | |
| var obj = PSObject.AsPSObject(errorRecord, storeTypeNameAndInstanceMembersLocally: true); | |
| if (obj.TypeNames.Contains("System.Management.Automation.ErrorRecord")) | |
| { | |
| if (!obj.TypeNames.Contains(ErrorRecordPSExtendedError)) | |
| { | |
| obj.TypeNames.Insert(0, ErrorRecordPSExtendedError); | |
| // Need to remove so this rendering doesn't take precedence as ErrorRecords is "OutOfBand" | |
| obj.TypeNames.Remove("System.Management.Automation.ErrorRecord"); | |
| } | |
| } | |
| if (obj.TypeNames.Contains("System.Exception")) | |
| { | |
| if (!obj.TypeNames.Contains(ExceptionPSExtendedError)) | |
| { | |
| obj.TypeNames.Insert(0, ExceptionPSExtendedError); | |
| // Need to remove so this rendering doesn't take precedence as Exception is "OutOfBand" | |
| obj.TypeNames.Remove("System.Exception"); | |
| } | |
| } | |
| if (addErrorIdentifier) | |
| { | |
| obj.Properties.Add(new PSNoteProperty("PSErrorIndex", index++)); | |
| } | |
| WriteObject(obj); | |
| } | |
| } | |
| } | |
| } | |