2018-02-13 09:23:53 -08:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
2016-07-29 13:02:49 -07:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Management.Automation.Host;
|
|
|
|
|
using System.Security;
|
2019-04-30 10:25:11 +05:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
using Dbg = System.Management.Automation.Diagnostics;
|
|
|
|
|
using InternalHostUserInterface = System.Management.Automation.Internal.Host.InternalHostUserInterface;
|
|
|
|
|
|
|
|
|
|
namespace System.Management.Automation.Remoting
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The ServerRemoteHostUserInterface class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class ServerRemoteHostUserInterface : PSHostUserInterface, IHostUISupportsMultipleChoiceSelection
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Server method executor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private ServerMethodExecutor _serverMethodExecutor;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor for ServerRemoteHostUserInterface.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal ServerRemoteHostUserInterface(ServerRemoteHost remoteHost)
|
|
|
|
|
{
|
|
|
|
|
Dbg.Assert(remoteHost != null, "Expected remoteHost != null");
|
2016-08-02 13:29:09 -07:00
|
|
|
ServerRemoteHost = remoteHost;
|
2016-03-30 14:20:52 -07:00
|
|
|
Dbg.Assert(!remoteHost.HostInfo.IsHostUINull, "Expected !remoteHost.HostInfo.IsHostUINull");
|
|
|
|
|
|
|
|
|
|
_serverMethodExecutor = remoteHost.ServerMethodExecutor;
|
|
|
|
|
|
|
|
|
|
// Use HostInfo to duplicate host-RawUI as null or non-null based on the client's host-RawUI.
|
2016-08-02 13:29:09 -07:00
|
|
|
RawUI = remoteHost.HostInfo.IsHostRawUINull ? null : new ServerRemoteHostRawUserInterface(this);
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raw ui.
|
|
|
|
|
/// </summary>
|
2016-08-02 13:29:09 -07:00
|
|
|
public override PSHostRawUserInterface RawUI { get; }
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Server remote host.
|
|
|
|
|
/// </summary>
|
2016-08-02 13:29:09 -07:00
|
|
|
internal ServerRemoteHost ServerRemoteHost { get; }
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override string ReadLine()
|
|
|
|
|
{
|
|
|
|
|
return _serverMethodExecutor.ExecuteMethod<string>(RemoteHostMethodId.ReadLine);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prompt for choice.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
|
|
|
|
|
{
|
|
|
|
|
return _serverMethodExecutor.ExecuteMethod<int>(RemoteHostMethodId.PromptForChoice, new object[] { caption, message, choices, defaultChoice });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-06 04:40:24 +05:00
|
|
|
/// Prompt for choice. User can select multiple choices.
|
2016-03-30 14:20:52 -07:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="caption"></param>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <param name="choices"></param>
|
|
|
|
|
/// <param name="defaultChoices"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Collection<int> PromptForChoice(string caption,
|
|
|
|
|
string message,
|
|
|
|
|
Collection<ChoiceDescription> choices,
|
|
|
|
|
IEnumerable<int> defaultChoices)
|
|
|
|
|
{
|
|
|
|
|
return _serverMethodExecutor.ExecuteMethod<Collection<int>>(RemoteHostMethodId.PromptForChoiceMultipleSelection,
|
|
|
|
|
new object[] { caption, message, choices, defaultChoices });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prompt.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
|
|
|
|
|
{
|
|
|
|
|
// forward the call to the client host
|
|
|
|
|
Dictionary<string, PSObject> results = _serverMethodExecutor.ExecuteMethod<Dictionary<string, PSObject>>(
|
|
|
|
|
RemoteHostMethodId.Prompt, new object[] { caption, message, descriptions });
|
|
|
|
|
|
|
|
|
|
// attempt to do the requested type casts on the server (it is okay to fail the cast and return the original object)
|
|
|
|
|
foreach (FieldDescription description in descriptions)
|
|
|
|
|
{
|
|
|
|
|
Type requestedType = InternalHostUserInterface.GetFieldType(description);
|
|
|
|
|
if (requestedType != null)
|
|
|
|
|
{
|
|
|
|
|
PSObject valueFromClient;
|
|
|
|
|
if (results.TryGetValue(description.Name, out valueFromClient))
|
|
|
|
|
{
|
|
|
|
|
object conversionResult;
|
|
|
|
|
if (LanguagePrimitives.TryConvertTo(valueFromClient, requestedType, CultureInfo.InvariantCulture, out conversionResult))
|
|
|
|
|
{
|
|
|
|
|
if (conversionResult != null)
|
|
|
|
|
{
|
|
|
|
|
results[description.Name] = PSObject.AsPSObject(conversionResult);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
results[description.Name] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void Write(string message)
|
|
|
|
|
{
|
|
|
|
|
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.Write1, new object[] { message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string message)
|
|
|
|
|
{
|
|
|
|
|
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.Write2, new object[] { foregroundColor, backgroundColor, message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void WriteLine()
|
|
|
|
|
{
|
|
|
|
|
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteLine1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void WriteLine(string message)
|
|
|
|
|
{
|
|
|
|
|
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteLine2, new object[] { message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string message)
|
|
|
|
|
{
|
|
|
|
|
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteLine3, new object[] { foregroundColor, backgroundColor, message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write error line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void WriteErrorLine(string message)
|
|
|
|
|
{
|
|
|
|
|
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteErrorLine, new object[] { message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write debug line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void WriteDebugLine(string message)
|
|
|
|
|
{
|
|
|
|
|
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteDebugLine, new object[] { message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write progress.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void WriteProgress(long sourceId, ProgressRecord record)
|
|
|
|
|
{
|
|
|
|
|
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteProgress, new object[] { sourceId, record });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write verbose line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void WriteVerboseLine(string message)
|
|
|
|
|
{
|
|
|
|
|
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteVerboseLine, new object[] { message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write warning line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void WriteWarningLine(string message)
|
|
|
|
|
{
|
|
|
|
|
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteWarningLine, new object[] { message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read line as secure string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override SecureString ReadLineAsSecureString()
|
|
|
|
|
{
|
|
|
|
|
return _serverMethodExecutor.ExecuteMethod<SecureString>(RemoteHostMethodId.ReadLineAsSecureString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prompt for credential.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
return _serverMethodExecutor.ExecuteMethod<PSCredential>(RemoteHostMethodId.PromptForCredential1,
|
2016-03-30 14:20:52 -07:00
|
|
|
new object[] { caption, message, userName, targetName });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prompt for credential.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
|
|
|
|
|
{
|
|
|
|
|
return _serverMethodExecutor.ExecuteMethod<PSCredential>(RemoteHostMethodId.PromptForCredential2,
|
2016-07-29 13:02:49 -07:00
|
|
|
new object[] { caption, message, userName, targetName, allowedCredentialTypes, options });
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|