diff --git a/src/UniversalDashboard/Execution/ExecutionService.cs b/src/UniversalDashboard/Execution/ExecutionService.cs index ee9348d7..d9c8adc0 100644 --- a/src/UniversalDashboard/Execution/ExecutionService.cs +++ b/src/UniversalDashboard/Execution/ExecutionService.cs @@ -13,6 +13,7 @@ using System.Management.Automation.Runspaces; using System.Text; using UniversalDashboard.Interfaces; +using System.Reflection; namespace UniversalDashboard.Execution { @@ -53,6 +54,9 @@ public ExecutionService(IDashboardService dashboardService, IHubContext hub, string { await hub.Clients.All.SendAsync("syncElement", componentId); } + + // PS Host + + public static async Task Write(this IHubContext hub, string clientId, string message, MessageType messageType) + { + await hub.Clients.Client(clientId).SendAsync("write", message, messageType); + } } public class DashboardHub : Hub { diff --git a/src/UniversalDashboard/Services/UDHost.cs b/src/UniversalDashboard/Services/UDHost.cs new file mode 100644 index 00000000..48dec4f7 --- /dev/null +++ b/src/UniversalDashboard/Services/UDHost.cs @@ -0,0 +1,149 @@ +using Microsoft.AspNetCore.SignalR; +using NLog; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Globalization; +using System.Linq; +using System.Management.Automation; +using System.Management.Automation.Host; +using System.Management.Automation.Runspaces; +using System.Security; +using UniversalDashboard.Models.Enums; + +namespace UniversalDashboard.Services +{ + public class UDHost : PSHost + { + private readonly UDHostUserInterface _udHostUserInterface; + + public UDHost() + { + _udHostUserInterface = new UDHostUserInterface(); + } + + public override string Name => "UDHost"; + + public override Version Version => new Version(1, 0); + + public override Guid InstanceId => Guid.NewGuid(); + + public override PSHostUserInterface UI => _udHostUserInterface; + + public override CultureInfo CurrentCulture => CultureInfo.CurrentCulture; + + public override CultureInfo CurrentUICulture => CultureInfo.CurrentUICulture; + + public override void EnterNestedPrompt() + { + + } + + public override void ExitNestedPrompt() + { + + } + + public override void NotifyBeginApplication() + { + + } + + public override void NotifyEndApplication() + { + + } + + public override void SetShouldExit(int exitCode) + { + + } + } + + public class UDHostUserInterface : PSHostUserInterface + { + private static readonly Logger Log = LogManager.GetLogger(nameof(UDHostUserInterface)); + + public override PSHostRawUserInterface RawUI => null; + + public IHubContext HubContext { get; set; } + public string ConnectionId { get; set; } + + public override Dictionary Prompt(string caption, string message, Collection descriptions) + { + throw new NotImplementedException(); + } + + public override int PromptForChoice(string caption, string message, Collection choices, int defaultChoice) + { + throw new NotImplementedException(); + } + + public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options) + { + throw new NotImplementedException(); + } + + public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName) + { + throw new NotImplementedException(); + } + + public override string ReadLine() + { + throw new NotImplementedException(); + } + + public override SecureString ReadLineAsSecureString() + { + throw new NotImplementedException(); + } + + public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) + { + Log.Trace(value); + //HubContext.Write(ConnectionId, value, MessageType.Informational).ConfigureAwait(false); + } + + public override void Write(string value) + { + Log.Trace(value); + //HubContext.Write(ConnectionId, value, MessageType.Informational).ConfigureAwait(false); + } + + public override void WriteDebugLine(string message) + { + Log.Trace("[DEBUG]:" + message); + //HubContext.Write(ConnectionId, message + Environment.NewLine, MessageType.Debug).ConfigureAwait(false); + } + + public override void WriteErrorLine(string value) + { + Log.Trace("[ERROR]:" + value); + //HubContext.Write(ConnectionId, value + Environment.NewLine, MessageType.Error).ConfigureAwait(false); + } + + public override void WriteLine(string value) + { + Log.Trace("[DEBUG]:" + value); + //HubContext.Write(ConnectionId, value + Environment.NewLine, MessageType.Informational).ConfigureAwait(false); + } + + public override void WriteProgress(long sourceId, ProgressRecord record) + { + + } + + public override void WriteVerboseLine(string message) + { + Log.Trace("[VERBOSE]:" + message); + //HubContext.Write(ConnectionId, message + Environment.NewLine, MessageType.Verbose).ConfigureAwait(false); + } + + public override void WriteWarningLine(string message) + { + Log.Trace("[WARNING]:" + message); + //HubContext.Write(ConnectionId, message + Environment.NewLine, MessageType.Warning).ConfigureAwait(false); + } + } +} diff --git a/src/UniversalDashboard/Services/UDRunspaceFactory.cs b/src/UniversalDashboard/Services/UDRunspaceFactory.cs index 688fa2d0..8eda337a 100644 --- a/src/UniversalDashboard/Services/UDRunspaceFactory.cs +++ b/src/UniversalDashboard/Services/UDRunspaceFactory.cs @@ -106,44 +106,4 @@ public void Dispose() } #endregion } - - public class UDHost : PSHost - { - public override string Name => "UDHost"; - - public override Version Version => new Version(1, 0); - - public override Guid InstanceId => Guid.NewGuid(); - - public override PSHostUserInterface UI => null; - - public override CultureInfo CurrentCulture => CultureInfo.CurrentCulture; - - public override CultureInfo CurrentUICulture => CultureInfo.CurrentUICulture; - - public override void EnterNestedPrompt() - { - - } - - public override void ExitNestedPrompt() - { - - } - - public override void NotifyBeginApplication() - { - - } - - public override void NotifyEndApplication() - { - - } - - public override void SetShouldExit(int exitCode) - { - - } - } } diff --git a/src/client/src/app/ud-dashboard.jsx b/src/client/src/app/ud-dashboard.jsx index b562768c..6d694e24 100644 --- a/src/client/src/app/ud-dashboard.jsx +++ b/src/client/src/app/ud-dashboard.jsx @@ -123,6 +123,11 @@ export default class UdDashboard extends React.Component { } }); + connection.on('write', (message) => { + console.log(message); + PubSub.publish("write", message); + }); + PubSub.subscribe('element-event', function(e, data) { if (data.type === "requestStateResponse") { connection.invoke("requestStateResponse", data.requestId, data.state)