diff --git a/PowerShellBlazor/Data/WeatherForecast.cs b/PowerShellBlazor/Data/WeatherForecast.cs new file mode 100644 index 0000000..68c9921 --- /dev/null +++ b/PowerShellBlazor/Data/WeatherForecast.cs @@ -0,0 +1,13 @@ +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 new file mode 100644 index 0000000..8fbb68c --- /dev/null +++ b/PowerShellBlazor/Data/WeatherForecastService.cs @@ -0,0 +1,20 @@ +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/Pages/Index.razor b/PowerShellBlazor/Pages/Index.razor index 87e5c33..08af79e 100644 --- a/PowerShellBlazor/Pages/Index.razor +++ b/PowerShellBlazor/Pages/Index.razor @@ -1,6 +1,5 @@ -@page "/" +@page "/" @inject IPowerShellService PowerShellService -
diff --git a/PowerShellBlazor/Program.cs b/PowerShellBlazor/Program.cs index 362ffcf..c06267c 100644 --- a/PowerShellBlazor/Program.cs +++ b/PowerShellBlazor/Program.cs @@ -1,4 +1,4 @@ -global using PowerShellBlazor.Models; +global using PowerShellBlazor.Models; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using PowerShellBlazor.Services; diff --git a/PowerShellBlazor/Scripts/test.ps1 b/PowerShellBlazor/Scripts/test.ps1 index 634d5b8..794db43 100644 --- a/PowerShellBlazor/Scripts/test.ps1 +++ b/PowerShellBlazor/Scripts/test.ps1 @@ -1,4 +1,4 @@ -for ($i=1; $i -le 5; $i++) { +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)" diff --git a/PowerShellBlazor/Services/IPowerShellService.cs b/PowerShellBlazor/Services/IPowerShellService.cs index 77498f5..80976cb 100644 --- a/PowerShellBlazor/Services/IPowerShellService.cs +++ b/PowerShellBlazor/Services/IPowerShellService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Management.Automation; namespace PowerShellBlazor.Services diff --git a/PowerShellBlazor/Services/PowerShellService.cs b/PowerShellBlazor/Services/PowerShellService.cs index e0e17c8..9ae9063 100644 --- a/PowerShellBlazor/Services/PowerShellService.cs +++ b/PowerShellBlazor/Services/PowerShellService.cs @@ -1,4 +1,4 @@ -using System.Management.Automation; +using System.Management.Automation; namespace PowerShellBlazor.Services { @@ -83,6 +83,122 @@ namespace PowerShellBlazor.Services shell.Streams.Error.DataAdded += delegate (object? sender, DataAddedEventArgs e) { AddOutput(PSStream.Error, shell.Streams.Error[e.Index].ToString()); +======= + public List Output { get; set; } = new(); + + private void AddOutput(string str) + { + Output.Add(str); + OutputChanged.Invoke(this, Output); + } + + public event EventHandler> OutputChanged; + + public async Task RunScript(PowerShell shell, bool varwidth) + { + if (shell == null) + { + AddOutput("Shell empty - nothing to execute"); + } + else + { + string fontstr = ""; + if (varwidth != true) + { + fontstr = "face='monospace' size=3"; + } + + AddOutput("Executing: " + shell.Commands.Commands[0].ToString()); + string prevmsg = ""; + string msg = ""; + + AddOutput("
BEGIN"); + AddOutput("
_________________________________________________________________________"); + + 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(); + } + }; + + 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(); + } + }; + + 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(); + } + }; + + 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(); + } }; // Execute powershell command diff --git a/PowerShellBlazor/Shared/MainLayout.razor b/PowerShellBlazor/Shared/MainLayout.razor index 6cd80c5..b5a8f47 100644 --- a/PowerShellBlazor/Shared/MainLayout.razor +++ b/PowerShellBlazor/Shared/MainLayout.razor @@ -1,4 +1,4 @@ -@inherits LayoutComponentBase +@inherits LayoutComponentBase PowerShell Blazor