Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from StrangeRanger/dev
Browse files Browse the repository at this point in the history
A Working Prototype
  • Loading branch information
StrangeRanger authored Nov 23, 2023
2 parents e9a3dbc + e69efef commit f5cb920
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dotnet-desktop-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ jobs:
dotnet-version: '8.0.x'

- name: Build
run: dotnet build $env:Project_File -c Release -f net8.0-windows -r win-${{ matrix.targetplatform }} --self-contained true
run: dotnet build $env:Project_File -c Release -f net8.0-windows -r win-${{ matrix.targetplatform }}

- name: Publish Artifact
run: dotnet publish -c Release -f net8.0-windows -r win-${{ matrix.targetplatform }} -o ./FAFB_PowerShell_Tool_${{ matrix.targetplatform }} --self-contained -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true -p:DebugType=embedded
run: dotnet publish -c Release -f net8.0-windows -r win-${{ matrix.targetplatform }} -o ./FAFB_PowerShell_Tool_${{ matrix.targetplatform }} --self-contained true -p:PublishSingleFile=false -p:IncludeNativeLibrariesForSelfExtract=true -p:DebugType=embedded

- name: Upload Artifact
uses: actions/upload-artifact@v3
Expand Down
24 changes: 24 additions & 0 deletions FAFB-PowerShell-Tool.Tests/FAFB-PowerShell-Tool.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>FAFB_PowerShell_Tool.Tests</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FAFB-PowerShell-Tool\FAFB-PowerShell-Tool.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions FAFB-PowerShell-Tool.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
25 changes: 25 additions & 0 deletions FAFB-PowerShell-Tool.Tests/PowerShellExecutorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FAFB_PowerShell_Tool.Tests
{
[TestClass]
public class PowerShellExecutorTest
{
[TestMethod]
public void ThrowArgumentNullExceptionWhenCommandTextIsNull()
{
Assert.ThrowsException<ArgumentNullException>(() => PowerShellExecutor.Execute(null!));
}

[TestMethod]
public void ThrowArgumentExceptionWhenCommandTextIsWhitespace()
{
Assert.ThrowsException<ArgumentException>(() => PowerShellExecutor.Execute(""));
Assert.ThrowsException<ArgumentException>(() => PowerShellExecutor.Execute(" "));
}
}
}
23 changes: 22 additions & 1 deletion FAFB-PowerShell-Tool.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FAFB-PowerShell-Tool", "FAFB-PowerShell-Tool\FAFB-PowerShell-Tool.csproj", "{C6E51308-E9E0-4738-9D5A-49841912633B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FAFB-PowerShell-Tool.Tests", "FAFB-PowerShell-Tool.Tests\FAFB-PowerShell-Tool.Tests.csproj", "{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -39,6 +40,26 @@ Global
{C6E51308-E9E0-4738-9D5A-49841912633B}.Release|x64.Build.0 = Release|Any CPU
{C6E51308-E9E0-4738-9D5A-49841912633B}.Release|x86.ActiveCfg = Release|Any CPU
{C6E51308-E9E0-4738-9D5A-49841912633B}.Release|x86.Build.0 = Release|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Debug|ARM.ActiveCfg = Debug|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Debug|ARM.Build.0 = Debug|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Debug|ARM64.Build.0 = Debug|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Debug|x64.ActiveCfg = Debug|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Debug|x64.Build.0 = Debug|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Debug|x86.ActiveCfg = Debug|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Debug|x86.Build.0 = Debug|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Release|Any CPU.Build.0 = Release|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Release|ARM.ActiveCfg = Release|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Release|ARM.Build.0 = Release|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Release|ARM64.ActiveCfg = Release|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Release|ARM64.Build.0 = Release|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Release|x64.ActiveCfg = Release|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Release|x64.Build.0 = Release|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Release|x86.ActiveCfg = Release|Any CPU
{E93739C2-C7BC-4F69-BEA2-C5F30548EA8C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
13 changes: 10 additions & 3 deletions FAFB-PowerShell-Tool/FAFB-PowerShell-Tool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>

<PublishSingleFile>true</PublishSingleFile>
<!-- TODO: Possibly change the below configurations.-->
<PublishSingleFile>false</PublishSingleFile>
<SelfContained>true</SelfContained>
<DebugType>embedded</DebugType>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
</PropertyGroup>

</Project>
<ItemGroup>
<PackageReference Include="Microsoft.Management.Infrastructure" Version="3.0.0" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.0" />
<PackageReference Include="System.Management.Automation" Version="7.4.0" />
</ItemGroup>

</Project>
10 changes: 9 additions & 1 deletion FAFB-PowerShell-Tool/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FAFB_PowerShell_Tool"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
Title="Active Directory Queries" Height="450" Width="800">
<Grid>
<Border BorderBrush="Black" BorderThickness="1"/>
<Button x:Name="Command_Button_1" Content="Command 1" HorizontalAlignment="Left" Margin="63,110,0,0" VerticalAlignment="Top" Height="49" Width="140" Click="CommandButton1"/>
<Button x:Name="Command_Button_2" Content="Command 2" HorizontalAlignment="Left" Margin="230,110,0,0" VerticalAlignment="Top" Height="49" Width="140" Click="CommandButton2"/>
<Button x:Name="Command_Button_3" Content="Command 3" HorizontalAlignment="Left" Margin="400,110,0,0" VerticalAlignment="Top" Height="49" Width="140" Click="CommandButton3"/>
<Button x:Name="Command_Button_4" Content="Command 4" HorizontalAlignment="Left" Margin="568,110,0,0" VerticalAlignment="Top" Height="49" Width="140" Click="CommandButton4"/>
<Button x:Name="Execute_Button" Content="Execute" HorizontalAlignment="Left" Margin="318,217,0,0" VerticalAlignment="Top" Height="49" Width="140" Click="ExecutionButton"/>
<Button x:Name="Execute_Generic_Command" Content="Execute Non-PowerShell Command" HorizontalAlignment="Left" Margin="133,320,0,0" VerticalAlignment="Top" Height="58" Width="542" Click="ExecuteGenericCommand"/>


</Grid>
</Window>
59 changes: 50 additions & 9 deletions FAFB-PowerShell-Tool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
using System.Text;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FAFB_PowerShell_Tool
{
Expand All @@ -16,9 +8,58 @@ namespace FAFB_PowerShell_Tool
/// </summary>
public partial class MainWindow : Window
{
private string command = null!;

public MainWindow()
{
InitializeComponent();
}

private void CommandButton1(object sender, RoutedEventArgs e)
{
command = "Get-ADUser -filter * -Properties * | out-gridvie";
}

private void CommandButton2(object sender, RoutedEventArgs e)
{
command = "Get-Process | Out-String -Width 4096";
}

private void CommandButton3(object sender, RoutedEventArgs e)
{
command = "Get-ChildItem -Path $env:USERPROFILE | Out-String -Width 4096";
}

private void CommandButton4(object sender, RoutedEventArgs e)
{
command = "New-Item -Path $env:USERPROFILE\\myFile.txt -ItemType File";
}

private void ExecutionButton(object sender, RoutedEventArgs e)
{
try
{
List<string> commandOutput = PowerShellExecutor.Execute(command);
string fullCommandOutput = "";


foreach (var str in commandOutput)
{
fullCommandOutput += str;
}

MessageBox.Show(fullCommandOutput);
}
catch (Exception ex)
{
MessageBox.Show("INTERNAL ERROR: " + ex.Message);
}
}

private void ExecuteGenericCommand(object sender, RoutedEventArgs e)
{
string hostName = System.Net.Dns.GetHostName();
MessageBox.Show("System Host Name: " + hostName);
}
}
}
56 changes: 56 additions & 0 deletions FAFB-PowerShell-Tool/PowerShellExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Management.Automation;
using System.Windows;

namespace FAFB_PowerShell_Tool;

// TODO: Should this be made into a singleton rather than a static class?
public class PowerShellExecutor
{
public static List<string> Execute(string commandText)
{
using PowerShell ps = PowerShell.Create();
List<string> returnValues = new List<string>();

// TODO: Use a MessageBox to show errors to the user.
ThrowExceptionIfCommandTextIsNullOrWhiteSpace(commandText);
ps.AddScript(commandText);

var results = ps.Invoke();

if (ps.HadErrors)
{
foreach (var error in ps.Streams.Error)
{
returnValues.Add("Error: " + error.ToString());
}
}
else
{
foreach (var result in results)
{
returnValues.Add(result.ToString());
}
}

return returnValues;
}

private static void ThrowExceptionIfCommandTextIsNullOrWhiteSpace(string commandText)
{
if (commandText is null)
{
throw new ArgumentNullException("Command text cannot be null.");
}

if (string.IsNullOrWhiteSpace(commandText))
{
throw new ArgumentException("Command text cannot be null or whitespace.");
}
}

private static void ShowError(string message)
{
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# FAFB-PowerShell-Tool
# FAFB-PowerShell-Tool

[ADD DESCRIPTION OF PROJECT]

0 comments on commit f5cb920

Please sign in to comment.