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
180 lines (154 loc) · 5.24 KB
/
Copy pathReadConsoleCmdlet.cs
File metadata and controls
180 lines (154 loc) · 5.24 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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
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("Read", "Host", HelpUri = "http://go.microsoft.com/fwlink/?LinkID=113371")]
[OutputType(typeof(String), typeof(SecureString))]
public sealed class ReadHostCommand : PSCmdlet
{
/// <summary>
///
/// Constructs a new instance.
///
/// </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>
///
/// Set to no echo the input as is is typed.
///
/// </summary>
[Parameter]
public
SwitchParameter
AsSecureString
{
get
{
return _safe;
}
set
{
_safe = value;
}
}
#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 StringBuilder();
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 FieldDescription(promptString);
if (AsSecureString)
{
fd.SetParameterType(typeof(System.Security.SecureString));
}
else
{
fd.SetParameterType(typeof(String));
}
Collection<FieldDescription> fdc = new Collection<FieldDescription>();
fdc.Add(fd);
Dictionary<string, PSObject> result = Host.UI.Prompt("", "", 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)
{
WriteObject(o);
}
}
}
else
{
object result;
if (AsSecureString)
{
result = Host.UI.ReadLineAsSecureString();
}
else
{
result = Host.UI.ReadLine();
}
WriteObject(result);
}
}
#endregion Cmdlet Overrides
private object _prompt = null;
private Boolean _safe = false;
}
} // namespace Microsoft.PowerShell.Commands