This repository has been archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from StrangeRanger/dev
A Working Prototype
- Loading branch information
Showing
10 changed files
with
202 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
FAFB-PowerShell-Tool.Tests/FAFB-PowerShell-Tool.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Microsoft.VisualStudio.TestTools.UnitTesting; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" ")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# FAFB-PowerShell-Tool | ||
# FAFB-PowerShell-Tool | ||
|
||
[ADD DESCRIPTION OF PROJECT] |