Skip to content

Commit d8d32d7

Browse files
SteveL-MSFTadityapatwardhan
authored andcommitted
Implement Get-Error cmdlet as Experimental Feature (PowerShell#10727)
1 parent 8b68a4c commit d8d32d7

9 files changed

Lines changed: 478 additions & 36 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.Management.Automation;
8+
9+
namespace Microsoft.PowerShell.Commands
10+
{
11+
/// <summary>
12+
/// Class for Get-Error implementation.
13+
/// </summary>
14+
[Experimental("Microsoft.PowerShell.Utility.PSGetError", ExperimentAction.Show)]
15+
[Cmdlet(VerbsCommon.Get, "Error",
16+
HelpUri = "https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/get-error?view=powershell-7&WT.mc_id=ps-gethelp",
17+
DefaultParameterSetName = NewestParameterSetName)]
18+
public sealed class GetErrorCommand : PSCmdlet
19+
{
20+
internal const string ErrorParameterSetName = "Error";
21+
internal const string NewestParameterSetName = "Newest";
22+
internal const string AliasNewest = "Last";
23+
24+
/// <summary>
25+
/// Gets or sets the error object to resolve.
26+
/// </summary>
27+
[Parameter(Position = 0, ValueFromPipeline = true, ParameterSetName = ErrorParameterSetName)]
28+
[ValidateNotNullOrEmpty]
29+
public PSObject InputObject { get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the number of error objects to resolve starting with newest first.
33+
/// </summary>
34+
[Parameter(ParameterSetName = NewestParameterSetName)]
35+
[Alias(AliasNewest)]
36+
[ValidateRange(1, int.MaxValue)]
37+
public int Newest { get; set; } = 1;
38+
39+
/// <summary>
40+
/// Process the error object.
41+
/// </summary>
42+
protected override void ProcessRecord()
43+
{
44+
var errorRecords = new List<object>();
45+
var index = 0;
46+
47+
if (InputObject != null)
48+
{
49+
if (InputObject.BaseObject is Exception || InputObject.BaseObject is ErrorRecord)
50+
{
51+
errorRecords.Add(InputObject);
52+
}
53+
}
54+
else
55+
{
56+
var errorVariable = SessionState.PSVariable.Get("error");
57+
var count = Newest;
58+
ArrayList errors = (ArrayList)errorVariable.Value;
59+
if (count > errors.Count)
60+
{
61+
count = errors.Count;
62+
}
63+
64+
while (count > 0)
65+
{
66+
errorRecords.Add(errors[index]);
67+
index++;
68+
count--;
69+
}
70+
}
71+
72+
index = 0;
73+
bool addErrorIdentifier = errorRecords.Count > 1 ? true : false;
74+
75+
foreach (object errorRecord in errorRecords)
76+
{
77+
PSObject obj = PSObject.AsPSObject(errorRecord);
78+
obj.TypeNames.Insert(0, "PSExtendedError");
79+
80+
// Remove some types so they don't get rendered by those formats
81+
obj.TypeNames.Remove("System.Management.Automation.ErrorRecord");
82+
obj.TypeNames.Remove("System.Exception");
83+
84+
if (addErrorIdentifier)
85+
{
86+
obj.Properties.Add(new PSNoteProperty("PSErrorIndex", index++));
87+
}
88+
89+
WriteObject(obj);
90+
}
91+
}
92+
}
93+
}

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs

Lines changed: 81 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -735,125 +735,184 @@ public ConsoleColorProxy(ConsoleHostUserInterface ui)
735735
_ui = ui;
736736
}
737737

738+
public ConsoleColor FormatAccentColor
739+
{
740+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
741+
get
742+
{
743+
return _ui.FormatAccentColor;
744+
}
745+
746+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
747+
set
748+
{
749+
_ui.FormatAccentColor = value;
750+
}
751+
}
752+
738753
public ConsoleColor ErrorAccentColor
739754
{
740755
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
741756
get
742-
{ return _ui.ErrorAccentColor; }
757+
{
758+
return _ui.ErrorAccentColor;
759+
}
743760

744761
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
745762
set
746-
{ _ui.ErrorAccentColor = value; }
763+
{
764+
_ui.ErrorAccentColor = value;
765+
}
747766
}
748767

749768
public ConsoleColor ErrorForegroundColor
750769
{
751770
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
752771
get
753-
{ return _ui.ErrorForegroundColor; }
772+
{
773+
return _ui.ErrorForegroundColor;
774+
}
754775

755776
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
756777
set
757-
{ _ui.ErrorForegroundColor = value; }
778+
{
779+
_ui.ErrorForegroundColor = value;
780+
}
758781
}
759782

760783
public ConsoleColor ErrorBackgroundColor
761784
{
762785
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
763786
get
764-
{ return _ui.ErrorBackgroundColor; }
787+
{
788+
return _ui.ErrorBackgroundColor;
789+
}
765790

766791
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
767792
set
768-
{ _ui.ErrorBackgroundColor = value; }
793+
{
794+
_ui.ErrorBackgroundColor = value;
795+
}
769796
}
770797

771798
public ConsoleColor WarningForegroundColor
772799
{
773800
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
774801
get
775-
{ return _ui.WarningForegroundColor; }
802+
{
803+
return _ui.WarningForegroundColor;
804+
}
776805

777806
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
778807
set
779-
{ _ui.WarningForegroundColor = value; }
808+
{
809+
_ui.WarningForegroundColor = value;
810+
}
780811
}
781812

782813
public ConsoleColor WarningBackgroundColor
783814
{
784815
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
785816
get
786-
{ return _ui.WarningBackgroundColor; }
817+
{
818+
return _ui.WarningBackgroundColor;
819+
}
787820

788821
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
789822
set
790-
{ _ui.WarningBackgroundColor = value; }
823+
{
824+
_ui.WarningBackgroundColor = value;
825+
}
791826
}
792827

793828
public ConsoleColor DebugForegroundColor
794829
{
795830
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
796831
get
797-
{ return _ui.DebugForegroundColor; }
832+
{
833+
return _ui.DebugForegroundColor;
834+
}
798835

799836
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
800837
set
801-
{ _ui.DebugForegroundColor = value; }
838+
{
839+
_ui.DebugForegroundColor = value;
840+
}
802841
}
803842

804843
public ConsoleColor DebugBackgroundColor
805844
{
806845
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
807846
get
808-
{ return _ui.DebugBackgroundColor; }
847+
{
848+
return _ui.DebugBackgroundColor;
849+
}
809850

810851
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
811852
set
812-
{ _ui.DebugBackgroundColor = value; }
853+
{
854+
_ui.DebugBackgroundColor = value;
855+
}
813856
}
814857

815858
public ConsoleColor VerboseForegroundColor
816859
{
817860
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
818861
get
819-
{ return _ui.VerboseForegroundColor; }
862+
{
863+
return _ui.VerboseForegroundColor;
864+
}
820865

821866
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
822867
set
823-
{ _ui.VerboseForegroundColor = value; }
868+
{
869+
_ui.VerboseForegroundColor = value;
870+
}
824871
}
825872

826873
public ConsoleColor VerboseBackgroundColor
827874
{
828875
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
829876
get
830-
{ return _ui.VerboseBackgroundColor; }
877+
{
878+
return _ui.VerboseBackgroundColor;
879+
}
831880

832881
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
833882
set
834-
{ _ui.VerboseBackgroundColor = value; }
883+
{
884+
_ui.VerboseBackgroundColor = value;
885+
}
835886
}
836887

837888
public ConsoleColor ProgressForegroundColor
838889
{
839890
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
840891
get
841-
{ return _ui.ProgressForegroundColor; }
892+
{
893+
return _ui.ProgressForegroundColor;
894+
}
842895

843896
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
844897
set
845-
{ _ui.ProgressForegroundColor = value; }
898+
{
899+
_ui.ProgressForegroundColor = value;
900+
}
846901
}
847902

848903
public ConsoleColor ProgressBackgroundColor
849904
{
850905
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
851906
get
852-
{ return _ui.ProgressBackgroundColor; }
907+
{
908+
return _ui.ProgressBackgroundColor;
909+
}
853910

854911
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
855912
set
856-
{ _ui.ProgressBackgroundColor = value; }
913+
{
914+
_ui.ProgressBackgroundColor = value;
915+
}
857916
}
858917
}
859918

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,9 @@ public override void WriteErrorLine(string value)
13551355
}
13561356
}
13571357

1358+
// Format colors
1359+
public ConsoleColor FormatAccentColor { get; set; } = ConsoleColor.Green;
1360+
13581361
// Error colors
13591362
public ConsoleColor ErrorAccentColor { get; set; } = ConsoleColor.Cyan;
13601363
public ConsoleColor ErrorForegroundColor { get; set; } = ConsoleColor.Red;

src/Modules/Unix/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ CmdletsToExport = @(
2525
'Get-TraceSource', 'Set-TraceSource', 'Add-Type', 'Get-TypeData', 'Remove-TypeData', 'Update-TypeData',
2626
'Get-UICulture', 'Get-Unique', 'Get-Uptime', 'Clear-Variable', 'Get-Variable', 'New-Variable',
2727
'Remove-Variable', 'Set-Variable', 'Get-Verb', 'Write-Verbose', 'Write-Warning', 'Invoke-WebRequest',
28-
'Format-Wide', 'ConvertTo-Xml', 'Select-Xml'
28+
'Format-Wide', 'ConvertTo-Xml', 'Select-Xml', 'Get-Error'
2929
)
3030
FunctionsToExport = @()
3131
AliasesToExport = @('fhx')
@@ -38,6 +38,10 @@ PrivateData = @{
3838
Name = 'Microsoft.PowerShell.Utility.PSManageBreakpointsInRunspace'
3939
Description = 'Enables -BreakAll parameter on Debug-Runspace and Debug-Job cmdlets to allow users to decide if they want PowerShell to break immediately in the current location when they attach a debugger.'
4040
}
41+
@{
42+
Name = 'Microsoft.PowerShell.Utility.PSGetError'
43+
Description = 'Enable Get-Error cmdlet that displays detailed information about ErrorRecords included nested objects'
44+
}
4145
)
4246
}
4347
}

src/Modules/Windows/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ CmdletsToExport = @(
2323
'ConvertFrom-StringData', 'Format-Table', 'New-TemporaryFile', 'New-TimeSpan', 'Get-TraceSource', 'Set-TraceSource',
2424
'Add-Type', 'Get-TypeData', 'Remove-TypeData', 'Update-TypeData', 'Get-UICulture', 'Get-Unique', 'Get-Uptime',
2525
'Clear-Variable', 'Get-Variable', 'New-Variable', 'Remove-Variable', 'Set-Variable', 'Get-Verb', 'Write-Verbose',
26-
'Write-Warning', 'Invoke-WebRequest', 'Format-Wide', 'ConvertTo-Xml', 'Select-Xml'
26+
'Write-Warning', 'Invoke-WebRequest', 'Format-Wide', 'ConvertTo-Xml', 'Select-Xml', 'Get-Error'
2727
)
2828
FunctionsToExport = @()
2929
AliasesToExport = @('fhx')
@@ -36,6 +36,10 @@ PrivateData = @{
3636
Name = 'Microsoft.PowerShell.Utility.PSManageBreakpointsInRunspace'
3737
Description = 'Enables -BreakAll parameter on Debug-Runspace and Debug-Job cmdlets to allow users to decide if they want PowerShell to break immediately in the current location when they attach a debugger.'
3838
}
39+
@{
40+
Name = 'Microsoft.PowerShell.Utility.PSGetError'
41+
Description = 'Enable Get-Error cmdlet that displays detailed information about ErrorRecords included nested objects'
42+
}
3943
)
4044
}
4145
}

0 commit comments

Comments
 (0)