Merge pull request #1 from ashstrahle/Ash

Init
This commit is contained in:
2022-08-23 18:24:20 +10:00
committed by GitHub
10 changed files with 140 additions and 35 deletions

View File

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

View File

@@ -1,7 +1,5 @@
@page "/" @page "/"
@inject IPowerShellService PowerShellService @inject IPowerShellService PowerShellService
@inject IWebHostEnvironment WebHostEnvironment
@using System.Management.Automation;
<EditForm EditContext="@editContext" OnSubmit="RunScript"> <EditForm EditContext="@editContext" OnSubmit="RunScript">
<DataAnnotationsValidator /> <DataAnnotationsValidator />
<div class="mb-3"> <div class="mb-3">
@@ -16,9 +14,11 @@
</EditForm> </EditForm>
<hr /> <hr />
@foreach (MarkupString line in PowerShellService.Output) @foreach (var message in PowerShellService.Output)
{ {
@line; <div class="@message.PSStream">
@((MarkupString)message.Data)
</div>
} }
@code @code
@@ -33,14 +33,11 @@
protected void RunScript() protected void RunScript()
{ {
PowerShellService.Output = new(); PowerShellService.Output = new();
string pscommand = Path.Combine(WebHostEnvironment.ContentRootPath, "Scripts/" + script);
PowerShell shell = PowerShell.Create();
shell.AddCommand(pscommand);
PowerShellService.OutputChanged += OutputChanged; PowerShellService.OutputChanged += OutputChanged;
Task.Run(() => PowerShellService.RunScript(shell, true)); Task.Run(() => PowerShellService.RunScript(script));
} }
private void OutputChanged(object sender, List<string> newOutput) private void OutputChanged(object sender, List<Message> newOutput)
{ {
InvokeAsync(() => this.StateHasChanged()); InvokeAsync(() => this.StateHasChanged());
} }

View File

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

View File

@@ -9,9 +9,11 @@
<ItemGroup> <ItemGroup>
<None Remove="Microsoft.PowerShell.SDK" /> <None Remove="Microsoft.PowerShell.SDK" />
<None Remove="Scripts\" /> <None Remove="Scripts\" />
<None Remove="Models\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Scripts\" /> <Folder Include="Scripts\" />
<Folder Include="Models\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.6" /> <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.6" />

View File

@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Components; global using PowerShellBlazor.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web;
using PowerShellBlazor.Data;
using PowerShellBlazor.Services; using PowerShellBlazor.Services;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);

View File

@@ -1,5 +1,6 @@
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-Output "Normal output text (Write-Output)" Write-Output "Normal output text (Write-Output)"
Write-Warning "Here's some warning text (Write-Warning)" Write-Warning "Here's some warning text (Write-Warning)"
Write-Error "Oh no, here's some error text (Write-Error)" Write-Error "Oh no, here's some error text (Write-Error)"

View File

@@ -1,13 +1,13 @@
using System; using System;
using System.Management.Automation; using System.Management.Automation;
namespace PowerShellBlazor.Services namespace PowerShellBlazor.Services
{ {
public interface IPowerShellService public interface IPowerShellService
{ {
public List<string> Output { get; set; } public List<Message> Output { get; set; }
public Task RunScript(PowerShell shell, bool varwidth); public Task RunScript(string script);
event EventHandler<List<string>> OutputChanged; event EventHandler<List<Message>> OutputChanged;
} }
} }

View File

@@ -1,11 +1,89 @@
using System;
using System.Collections.ObjectModel;
using System.Management.Automation; using System.Management.Automation;
namespace PowerShellBlazor.Services namespace PowerShellBlazor.Services
{ {
public class PowerShellService : IPowerShellService public class PowerShellService : IPowerShellService
{ {
private IWebHostEnvironment _WebHostEnvironment;
public PowerShellService(IWebHostEnvironment webHostEnvironment)
{
_WebHostEnvironment = webHostEnvironment;
}
public List<Message> Output { get; set; } = new();
private Message prevMsg = new();
private void AddOutput(PSStream stream, string message)
{
Message msg = new()
{
PSStream = stream,
Data = message
};
if (msg == prevMsg)
{
Output.Last().Data += ".";
}
else
{
Output.Add(msg);
prevMsg = msg;
}
OutputChanged.Invoke(this, Output);
}
public event EventHandler<List<Message>> 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, "<b>Executing: </b>" + shell.Commands.Commands[0].ToString());
string prevmsg = "";
string msg = "";
AddOutput(PSStream.Output, "<b>BEGIN</b>");
AddOutput(PSStream.Output, "_________________________________________________________________________");
var psOutput = new PSDataCollection<PSObject>();
psOutput.DataAdded += delegate (object? sender, DataAddedEventArgs e)
{
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());
};
shell.Streams.Progress.DataAdded += delegate (object? sender, DataAddedEventArgs e)
{
AddOutput(PSStream.Progress, shell.Streams.Progress[e.Index].Activity.ToString());
};
shell.Streams.Warning.DataAdded += delegate (object? sender, DataAddedEventArgs e)
{
AddOutput(PSStream.Warning, shell.Streams.Warning[e.Index].ToString());
};
shell.Streams.Error.DataAdded += delegate (object? sender, DataAddedEventArgs e)
{
AddOutput(PSStream.Error, shell.Streams.Error[e.Index].ToString());
=======
public List<string> Output { get; set; } = new(); public List<string> Output { get; set; } = new();
private void AddOutput(string str) private void AddOutput(string str)
@@ -129,9 +207,8 @@ namespace PowerShellBlazor.Services
// Wait for powershell command to finish // Wait for powershell command to finish
IasyncResult.AsyncWaitHandle.WaitOne(); IasyncResult.AsyncWaitHandle.WaitOne();
AddOutput(PSStream.Output, "_________________________________________________________________________");
AddOutput("<br>_________________________________________________________________________"); AddOutput(PSStream.Output, "<b>EXECUTION COMPLETE</b>");
AddOutput("<br><b>EXECUTION COMPLETE</b>");
} }
} }
} }

View File

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

View File

@@ -7,16 +7,6 @@
</div> </div>
</div> </div>
<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
<nav class="flex-column">
<div class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</div>
</nav>
</div>
@code { @code {
private bool collapseNavMenu = true; private bool collapseNavMenu = true;