diff --git a/PowerShellBlazor/Data/WeatherForecast.cs b/PowerShellBlazor/Data/WeatherForecast.cs deleted file mode 100644 index 68c9921..0000000 --- a/PowerShellBlazor/Data/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace PowerShellBlazor.Data; - -public class WeatherForecast -{ - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } -} - diff --git a/PowerShellBlazor/Data/WeatherForecastService.cs b/PowerShellBlazor/Data/WeatherForecastService.cs deleted file mode 100644 index 8fbb68c..0000000 --- a/PowerShellBlazor/Data/WeatherForecastService.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace PowerShellBlazor.Data; - -public class WeatherForecastService -{ - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - public Task GetForecastAsync(DateTime startDate) - { - return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = startDate.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }).ToArray()); - } -} - diff --git a/PowerShellBlazor/Models/Message.cs b/PowerShellBlazor/Models/Message.cs new file mode 100644 index 0000000..eb51fe1 --- /dev/null +++ b/PowerShellBlazor/Models/Message.cs @@ -0,0 +1,11 @@ +using System; +namespace PowerShellBlazor.Models +{ + public enum PSStream { Output, Information, Progress, Warning, Error } + + public class Message + { + public PSStream PSStream; + public string Data; + } +} \ No newline at end of file diff --git a/PowerShellBlazor/Pages/Index.razor b/PowerShellBlazor/Pages/Index.razor index 9460e1f..87e5c33 100644 --- a/PowerShellBlazor/Pages/Index.razor +++ b/PowerShellBlazor/Pages/Index.razor @@ -1,7 +1,6 @@ @page "/" @inject IPowerShellService PowerShellService -@inject IWebHostEnvironment WebHostEnvironment -@using System.Management.Automation; +
@@ -16,9 +15,11 @@
-@foreach (MarkupString line in PowerShellService.Output) +@foreach (var message in PowerShellService.Output) { - @line; +
+ @((MarkupString)message.Data) +
} @code @@ -33,14 +34,11 @@ protected void RunScript() { PowerShellService.Output = new(); - string pscommand = Path.Combine(WebHostEnvironment.ContentRootPath, "Scripts/" + script); - PowerShell shell = PowerShell.Create(); - shell.AddCommand(pscommand); PowerShellService.OutputChanged += OutputChanged; - Task.Run(() => PowerShellService.RunScript(shell, true)); + Task.Run(() => PowerShellService.RunScript(script)); } - private void OutputChanged(object sender, List newOutput) + private void OutputChanged(object sender, List newOutput) { InvokeAsync(() => this.StateHasChanged()); } diff --git a/PowerShellBlazor/Pages/Index.razor.css b/PowerShellBlazor/Pages/Index.razor.css new file mode 100644 index 0000000..fe16868 --- /dev/null +++ b/PowerShellBlazor/Pages/Index.razor.css @@ -0,0 +1,27 @@ +.Output { + color: black +} + +.Progress { + color: green +} + +.Information { + color: blue +} + +.Warning::before { + content: "*** Warning ***: " +} + +.Warning { + color: orange +} + +.Error::before { + content: "*** Error ***: " +} + +.Error { + color: red +} diff --git a/PowerShellBlazor/PowerShellBlazor.csproj b/PowerShellBlazor/PowerShellBlazor.csproj index efa20b9..b724112 100644 --- a/PowerShellBlazor/PowerShellBlazor.csproj +++ b/PowerShellBlazor/PowerShellBlazor.csproj @@ -9,9 +9,11 @@ + + diff --git a/PowerShellBlazor/Program.cs b/PowerShellBlazor/Program.cs index c607de4..362ffcf 100644 --- a/PowerShellBlazor/Program.cs +++ b/PowerShellBlazor/Program.cs @@ -1,6 +1,6 @@ -using Microsoft.AspNetCore.Components; +global using PowerShellBlazor.Models; +using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; -using PowerShellBlazor.Data; using PowerShellBlazor.Services; var builder = WebApplication.CreateBuilder(args); diff --git a/PowerShellBlazor/Scripts/test.ps1 b/PowerShellBlazor/Scripts/test.ps1 index b3bc198..634d5b8 100644 --- a/PowerShellBlazor/Scripts/test.ps1 +++ b/PowerShellBlazor/Scripts/test.ps1 @@ -1,5 +1,6 @@ for ($i=1; $i -le 5; $i++) { Write-Progress "Loop $i - progress output (Write-Progress)" + Write-Information "Here's some information (Write-Information)" Write-Output "Normal output text (Write-Output)" Write-Warning "Here's some warning text (Write-Warning)" Write-Error "Oh no, here's some error text (Write-Error)" diff --git a/PowerShellBlazor/Services/IPowerShellService.cs b/PowerShellBlazor/Services/IPowerShellService.cs index 96d2bbb..77498f5 100644 --- a/PowerShellBlazor/Services/IPowerShellService.cs +++ b/PowerShellBlazor/Services/IPowerShellService.cs @@ -5,9 +5,9 @@ namespace PowerShellBlazor.Services { public interface IPowerShellService { - public List Output { get; set; } - public Task RunScript(PowerShell shell, bool varwidth); + public List Output { get; set; } + public Task RunScript(string script); - event EventHandler> OutputChanged; + event EventHandler> OutputChanged; } } \ No newline at end of file diff --git a/PowerShellBlazor/Services/PowerShellService.cs b/PowerShellBlazor/Services/PowerShellService.cs index c68d2fb..e0e17c8 100644 --- a/PowerShellBlazor/Services/PowerShellService.cs +++ b/PowerShellBlazor/Services/PowerShellService.cs @@ -1,126 +1,88 @@ -using System; -using System.Collections.ObjectModel; -using System.Management.Automation; +using System.Management.Automation; namespace PowerShellBlazor.Services { public class PowerShellService : IPowerShellService { - public List Output { get; set; } = new(); + private IWebHostEnvironment _WebHostEnvironment; - private void AddOutput(string str) + public PowerShellService(IWebHostEnvironment webHostEnvironment) { - Output.Add(str); - OutputChanged.Invoke(this, Output); + _WebHostEnvironment = webHostEnvironment; } - public event EventHandler> OutputChanged; + public List Output { get; set; } = new(); - public async Task RunScript(PowerShell shell, bool varwidth) + private Message prevMsg = new(); + + private void AddOutput(PSStream stream, string message) { - if (shell == null) + Message msg = new() { - AddOutput("Shell empty - nothing to execute"); + PSStream = stream, + Data = message + }; + + if (msg == prevMsg) + { + Output.Last().Data += "."; } else { - string fontstr = ""; - if (varwidth != true) - { - fontstr = "face='monospace' size=3"; - } + Output.Add(msg); + prevMsg = msg; + } + OutputChanged.Invoke(this, Output); + } - AddOutput("Executing: " + shell.Commands.Commands[0].ToString()); + public event EventHandler> OutputChanged; + + public async Task RunScript(string script) + { + string pscommand = Path.Combine(_WebHostEnvironment.ContentRootPath, "Scripts/" + script); + if (!File.Exists(pscommand)) + { + AddOutput(PSStream.Error, "File not found: " + pscommand); + } + else + { + PowerShell shell = PowerShell.Create(); + shell.AddCommand(pscommand); + + string fontstr = ""; + + AddOutput(PSStream.Output, "Executing: " + shell.Commands.Commands[0].ToString()); string prevmsg = ""; string msg = ""; - AddOutput("
BEGIN"); - AddOutput("
_________________________________________________________________________"); + AddOutput(PSStream.Output, "BEGIN"); + AddOutput(PSStream.Output, "_________________________________________________________________________"); var psOutput = new PSDataCollection(); - // Collect powershell OUTPUT psOutput.DataAdded += delegate (object? sender, DataAddedEventArgs e) { - msg = psOutput[e.Index].ToString(); - - if (msg != prevmsg) - { - AddOutput("
" + msg + ""); - } - else - { - AddOutput("."); - } - prevmsg = msg; - if (sender is not null) - { - var psoutput = (PSDataCollection)sender; - Collection results = psoutput.ReadAll(); - } + AddOutput(PSStream.Output, psOutput[e.Index].ToString()); + }; + + shell.Streams.Information.DataAdded += delegate (object? sender, DataAddedEventArgs e) + { + AddOutput(PSStream.Information, shell.Streams.Information[e.Index].ToString()); }; - prevmsg = ""; - // Collect powershell PROGRESS output shell.Streams.Progress.DataAdded += delegate (object? sender, DataAddedEventArgs e) { - msg = shell.Streams.Progress[e.Index].Activity.ToString(); - if (msg != prevmsg) - { - AddOutput("
" + msg + ""); - } - else - { - AddOutput("."); - } - prevmsg = msg; - if (sender is not null) - { - var psprogress = (PSDataCollection)sender; - Collection results = psprogress.ReadAll(); - } + AddOutput(PSStream.Progress, shell.Streams.Progress[e.Index].Activity.ToString()); }; - prevmsg = ""; - // Collect powershell WARNING output shell.Streams.Warning.DataAdded += delegate (object? sender, DataAddedEventArgs e) { - msg = shell.Streams.Warning[e.Index].ToString(); - if (msg != prevmsg) - { - AddOutput("
***WARNING***: " + msg + ""); - } - else - { - AddOutput("."); - } - prevmsg = msg; - if (sender is not null) - { - var pswarning = (PSDataCollection)sender; - Collection results = pswarning.ReadAll(); - } + AddOutput(PSStream.Warning, shell.Streams.Warning[e.Index].ToString()); }; - prevmsg = ""; - // Collect powershell ERROR output shell.Streams.Error.DataAdded += delegate (object? sender, DataAddedEventArgs e) { - msg = shell.Streams.Error[e.Index].ToString(); - if (msg != prevmsg) - { - AddOutput("
***ERROR***: " + msg + ""); - } - else - { - AddOutput("."); - } - prevmsg = msg; - if (sender is not null) - { - var pserror = (PSDataCollection)sender; - Collection results = pserror.ReadAll(); - } + AddOutput(PSStream.Error, shell.Streams.Error[e.Index].ToString()); }; // Execute powershell command @@ -129,9 +91,8 @@ namespace PowerShellBlazor.Services // Wait for powershell command to finish IasyncResult.AsyncWaitHandle.WaitOne(); - - AddOutput("
_________________________________________________________________________"); - AddOutput("
EXECUTION COMPLETE"); + AddOutput(PSStream.Output, "_________________________________________________________________________"); + AddOutput(PSStream.Output, "EXECUTION COMPLETE"); } } } diff --git a/PowerShellBlazor/Shared/MainLayout.razor b/PowerShellBlazor/Shared/MainLayout.razor index dba6479..6cd80c5 100644 --- a/PowerShellBlazor/Shared/MainLayout.razor +++ b/PowerShellBlazor/Shared/MainLayout.razor @@ -1,6 +1,6 @@ @inherits LayoutComponentBase -PowerShellBlazor +PowerShell Blazor