Merge branch 'master' into Ash

This commit is contained in:
2022-08-23 18:24:13 +10:00
committed by GitHub
8 changed files with 155 additions and 7 deletions

View File

@@ -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; }
}

View File

@@ -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<WeatherForecast[]> 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());
}
}

View File

@@ -1,6 +1,5 @@
@page "/" @page "/"
@inject IPowerShellService PowerShellService @inject IPowerShellService PowerShellService
<EditForm EditContext="@editContext" OnSubmit="RunScript"> <EditForm EditContext="@editContext" OnSubmit="RunScript">
<DataAnnotationsValidator /> <DataAnnotationsValidator />
<div class="mb-3"> <div class="mb-3">

View File

@@ -1,4 +1,4 @@
global using PowerShellBlazor.Models; global using PowerShellBlazor.Models;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web;
using PowerShellBlazor.Services; using PowerShellBlazor.Services;

View File

@@ -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-Progress "Loop $i - progress output (Write-Progress)"
Write-Information "Here's some information (Write-Information)" Write-Information "Here's some information (Write-Information)"
Write-Output "Normal output text (Write-Output)" Write-Output "Normal output text (Write-Output)"

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Management.Automation; using System.Management.Automation;
namespace PowerShellBlazor.Services namespace PowerShellBlazor.Services

View File

@@ -1,4 +1,4 @@
using System.Management.Automation; using System.Management.Automation;
namespace PowerShellBlazor.Services namespace PowerShellBlazor.Services
{ {
@@ -83,6 +83,122 @@ namespace PowerShellBlazor.Services
shell.Streams.Error.DataAdded += delegate (object? sender, DataAddedEventArgs e) shell.Streams.Error.DataAdded += delegate (object? sender, DataAddedEventArgs e)
{ {
AddOutput(PSStream.Error, shell.Streams.Error[e.Index].ToString()); AddOutput(PSStream.Error, shell.Streams.Error[e.Index].ToString());
=======
public List<string> Output { get; set; } = new();
private void AddOutput(string str)
{
Output.Add(str);
OutputChanged.Invoke(this, Output);
}
public event EventHandler<List<string>> 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("<b>Executing: </b>" + shell.Commands.Commands[0].ToString());
string prevmsg = "";
string msg = "";
AddOutput("<br><b>BEGIN</b>");
AddOutput("<br>_________________________________________________________________________");
var psOutput = new PSDataCollection<PSObject>();
// Collect powershell OUTPUT
psOutput.DataAdded += delegate (object? sender, DataAddedEventArgs e)
{
msg = psOutput[e.Index].ToString();
if (msg != prevmsg)
{
AddOutput("<br><span><font color=black " + fontstr + ">" + msg + "</font></span>");
}
else
{
AddOutput(".");
}
prevmsg = msg;
if (sender is not null)
{
var psoutput = (PSDataCollection<PSObject>)sender;
Collection<PSObject> 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("<br><span><font color=green " + fontstr + ">" + msg + "</font></span>");
}
else
{
AddOutput(".");
}
prevmsg = msg;
if (sender is not null)
{
var psprogress = (PSDataCollection<ProgressRecord>)sender;
Collection<ProgressRecord> 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("<br><span><font color=orange " + fontstr + "><b>***WARNING***:</b> " + msg + "</font></span>");
}
else
{
AddOutput(".");
}
prevmsg = msg;
if (sender is not null)
{
var pswarning = (PSDataCollection<WarningRecord>)sender;
Collection<WarningRecord> 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("<br><span><font color=red " + fontstr + "><b>***ERROR***:</b> " + msg + "</font></span>");
}
else
{
AddOutput(".");
}
prevmsg = msg;
if (sender is not null)
{
var pserror = (PSDataCollection<ErrorRecord>)sender;
Collection<ErrorRecord> results = pserror.ReadAll();
}
}; };
// Execute powershell command // Execute powershell command

View File

@@ -1,4 +1,4 @@
@inherits LayoutComponentBase @inherits LayoutComponentBase
<PageTitle>PowerShell Blazor</PageTitle> <PageTitle>PowerShell Blazor</PageTitle>