forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTee-Object.cs
More file actions
164 lines (142 loc) · 4.77 KB
/
Copy pathTee-Object.cs
File metadata and controls
164 lines (142 loc) · 4.77 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Management.Automation;
using System.Text;
using Microsoft.PowerShell.Commands.Internal.Format;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Class for Tee-object implementation.
/// </summary>
[Cmdlet("Tee", "Object", DefaultParameterSetName = "File", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097034")]
public sealed class TeeObjectCommand : PSCmdlet, IDisposable
{
/// <summary>
/// Object to process.
/// </summary>
[Parameter(ValueFromPipeline = true)]
public PSObject InputObject
{
get { return _inputObject; }
set { _inputObject = value; }
}
private PSObject _inputObject;
/// <summary>
/// FilePath parameter.
/// </summary>
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "File")]
[Alias("Path")]
public string FilePath
{
get { return _fileName; }
set { _fileName = value; }
}
private string _fileName;
/// <summary>
/// Literal FilePath parameter.
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "LiteralFile")]
[Alias("PSPath", "LP")]
public string LiteralPath
{
get
{
return _fileName;
}
set
{
_fileName = value;
}
}
/// <summary>
/// Append switch.
/// </summary>
[Parameter(ParameterSetName = "File")]
public SwitchParameter Append
{
get { return _append; }
set { _append = value; }
}
private bool _append;
/// <summary>
/// Gets or sets the Encoding.
/// </summary>
[Parameter(ParameterSetName = "File")]
[Parameter(ParameterSetName = "LiteralFile")]
[ArgumentToEncodingTransformation]
[ArgumentEncodingCompletions]
[ValidateNotNullOrEmpty]
public Encoding Encoding { get; set; } = Encoding.Default;
/// <summary>
/// Variable parameter.
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "Variable")]
public string Variable
{
get { return _variable; }
set { _variable = value; }
}
private string _variable;
/// <summary>
/// </summary>
protected override void BeginProcessing()
{
_commandWrapper = new CommandWrapper();
if (string.Equals(ParameterSetName, "File", StringComparison.OrdinalIgnoreCase))
{
_commandWrapper.Initialize(Context, "out-file", typeof(OutFileCommand));
_commandWrapper.AddNamedParameter("filepath", _fileName);
_commandWrapper.AddNamedParameter("append", _append);
_commandWrapper.AddNamedParameter("encoding", Encoding);
}
else if (string.Equals(ParameterSetName, "LiteralFile", StringComparison.OrdinalIgnoreCase))
{
_commandWrapper.Initialize(Context, "out-file", typeof(OutFileCommand));
_commandWrapper.AddNamedParameter("LiteralPath", _fileName);
_commandWrapper.AddNamedParameter("append", _append);
_commandWrapper.AddNamedParameter("encoding", Encoding);
}
else
{
// variable parameter set
_commandWrapper.Initialize(Context, "set-variable", typeof(SetVariableCommand));
_commandWrapper.AddNamedParameter("name", _variable);
// Can't use set-var's passthru because it writes the var object to the pipeline, we want just
// the values to be written
}
}
/// <summary>
/// </summary>
protected override void ProcessRecord()
{
_commandWrapper.Process(_inputObject);
WriteObject(_inputObject);
}
/// <summary>
/// </summary>
protected override void EndProcessing()
{
_commandWrapper.ShutDown();
}
/// <summary>
/// Release all resources.
/// </summary>
public void Dispose()
{
if (!_alreadyDisposed)
{
_alreadyDisposed = true;
if (_commandWrapper != null)
{
_commandWrapper.Dispose();
_commandWrapper = null;
}
}
}
#region private
private CommandWrapper _commandWrapper;
private bool _alreadyDisposed;
#endregion private
}
}