Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tee object can now store received error records to a variable. #21565

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,21 @@ public string Variable
set { _variable = value; }
}

/// <summary>
/// Gets or Sets the name of the variable used to store ErrorRecords.
/// <summary>
[Parameter]
[ValidateNotNullOrEmpty]
[Alias("ERV")]
public string ErrorRecordVariable
{
get { return this.errorRecordVariable; }

set { this.errorRecordVariable = value; }
}

private string _variable;
private string errorRecordVariable;

/// <summary>
/// </summary>
Expand Down Expand Up @@ -123,12 +137,27 @@ protected override void BeginProcessing()
// Can't use set-var's passthru because it writes the var object to the pipeline, we want just
// the values to be written
}

if (!string.IsNullOrEmpty(this.errorRecordVariable))
{
this.errorCommandWrapper = new CommandWrapper();
this.errorCommandWrapper.Initialize(Context, "set-variable", typeof(SetVariableCommand));
this.errorCommandWrapper.AddNamedParameter("name", this.errorRecordVariable);
}
}

/// <summary>
/// </summary>
protected override void ProcessRecord()
{
if (this.errorCommandWrapper is not null)
{
if (this._inputObject.BaseObject is ErrorRecord er)
{
this.errorCommandWrapper.Process(_inputObject);
}
}

_commandWrapper.Process(_inputObject);
WriteObject(_inputObject);
}
Expand All @@ -137,7 +166,12 @@ protected override void ProcessRecord()
/// </summary>
protected override void EndProcessing()
{
// _commandWrapper is always created, but errorCommandWrapper may not be.
_commandWrapper.ShutDown();
if (this.errorCommandWrapper is not null)
{
errorCommandWrapper.ShutDown();
}
}

private void Dispose(bool isDisposing)
Expand All @@ -150,6 +184,12 @@ private void Dispose(bool isDisposing)
_commandWrapper.Dispose();
_commandWrapper = null;
}

if (isDisposing && this.errorCommandWrapper is not null)
{
this.errorCommandWrapper.Dispose();
errorCommandWrapper = null;
}
}
}

Expand All @@ -164,6 +204,7 @@ public void Dispose()

#region private
private CommandWrapper _commandWrapper;
private CommandWrapper errorCommandWrapper;
private bool _alreadyDisposed;
#endregion private
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,26 @@ Describe "Tee-Object DRT Unit Tests" -Tags "CI" {
$results | Should -Be $expected
}

It "Positive Variable/ErrorRecordVariable Test" {
$results = Get-Item $PSHOME,doesnotexist1,$PWD,doesnotexist2 2>&1 | Tee-Object -Variable outputVar -ErrorRecordVariable errVar
SteveL-MSFT marked this conversation as resolved.
Show resolved Hide resolved
$results.Length | Should -Be 4
$outputVar.Length | Should -Be 4
$errVar.Length | Should -Be 2
}

It "Positive File/ErrorRecordVariable Test" {
$results = Get-Item $PSHOME,doesnotexist1,$PWD,doesnotexist2 2>&1 | Tee-Object -File $tempFile -ErrorRecordVariable errVar
Copy link
Contributor

@Jaykul Jaykul May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test where you Tee a second time to prove the error still passes through and is output?

$results.Length | Should -Be 4
(Get-Content $tempFile).Length | Should -BeGreaterThan 4
$errVar.Length | Should -Be 2
}

It "Positive native process test" {
$results = .{ testexe -echoargs a; testexe } 2>&1 | Tee-Object -Variable outputVar -ErrorRecordVariable errVar
$expectedResults = 'Arg 0 is <a>','Test not specified'
$results | Should -Be $expectedResults
$outputVar | Should -Be $expectedResults
$errVar | Should -Be "Test not specified"
}

}