forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadConsoleCmdlet.cs
More file actions
187 lines (166 loc) · 5.92 KB
/
Copy pathReadConsoleCmdlet.cs
File metadata and controls
187 lines (166 loc) · 5.92 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Security;
using System.Text;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Retrieves input from the host virtual console and writes it to the pipeline output.
/// </summary>
[Cmdlet(VerbsCommunications.Read, "Host", DefaultParameterSetName = "AsString", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096610")]
[OutputType(typeof(string), typeof(SecureString))]
public sealed class ReadHostCommand : PSCmdlet
{
/// <summary>
/// Initializes a new instance of the <see cref="ReadHostCommand"/> class.
/// </summary>
public
ReadHostCommand()
{
// empty, provided per design guidelines.
}
#region Parameters
/// <summary>
/// The objects to display on the host before collecting input.
/// </summary>
[Parameter(Position = 0, ValueFromRemainingArguments = true)]
[AllowNull]
public
object
Prompt
{
get
{
return _prompt;
}
set
{
_prompt = value;
}
}
/// <summary>
/// Gets or sets to no echo the input as is typed. If set then the cmdlet returns a secure string.
/// </summary>
[Parameter(ParameterSetName = "AsSecureString")]
public
SwitchParameter
AsSecureString
{
get
{
return _safe;
}
set
{
_safe = value;
}
}
/// <summary>
/// Gets or sets whether the console will echo the input as is typed. If set then the cmdlet returns a regular string.
/// </summary>
[Parameter(ParameterSetName = "AsString")]
public
SwitchParameter
MaskInput
{
get;
set;
}
#endregion Parameters
#region Cmdlet Overrides
/// <summary>
/// Write the prompt, then collect a line of input from the host, then output it to the output stream.
/// </summary>
protected override void BeginProcessing()
{
PSHostUserInterface ui = Host.UI;
string promptString;
if (_prompt != null)
{
IEnumerator e = LanguagePrimitives.GetEnumerator(_prompt);
if (e != null)
{
StringBuilder sb = new();
while (e.MoveNext())
{
// The current object might itself be a collection, like a string array, as in read/console "foo","bar","baz"
// If it is, then the PSObject ToString() will take care of it. We could go on unwrapping collections
// forever, but it's a pretty common use case to see a varags confused with an array.
string element = (string)LanguagePrimitives.ConvertTo(e.Current, typeof(string), CultureInfo.InvariantCulture);
if (!string.IsNullOrEmpty(element))
{
// Prepend a space if the stringbuilder isn't empty...
// We could consider using $OFS here but that's probably more
// effort than is really needed...
if (sb.Length > 0)
sb.Append(' ');
sb.Append(element);
}
}
promptString = sb.ToString();
}
else
{
promptString = (string)LanguagePrimitives.ConvertTo(_prompt, typeof(string), CultureInfo.InvariantCulture);
}
FieldDescription fd = new(promptString);
if (AsSecureString || MaskInput)
{
fd.SetParameterType(typeof(SecureString));
}
else
{
fd.SetParameterType(typeof(string));
}
Collection<FieldDescription> fdc = new();
fdc.Add(fd);
Dictionary<string, PSObject> result = Host.UI.Prompt(string.Empty, string.Empty, fdc);
// Result can be null depending on the host implementation. One typical
// example of a null return is for a canceled dialog.
if (result != null)
{
foreach (PSObject o in result.Values)
{
if (MaskInput && o?.BaseObject is SecureString secureString)
{
WriteObject(Utils.GetStringFromSecureString(secureString));
}
else
{
WriteObject(o);
}
}
}
}
else
{
object result;
if (AsSecureString || MaskInput)
{
result = Host.UI.ReadLineAsSecureString();
}
else
{
result = Host.UI.ReadLine();
}
if (MaskInput)
{
WriteObject(Utils.GetStringFromSecureString((SecureString)result));
}
else
{
WriteObject(result);
}
}
}
#endregion Cmdlet Overrides
private object _prompt = null;
private bool _safe = false;
}
}