diff --git a/.github/workflows/BuildAndTestOnEveryPush.yml b/.github/workflows/BuildAndTestOnEveryPush.yml index c3ee083..035b95e 100644 --- a/.github/workflows/BuildAndTestOnEveryPush.yml +++ b/.github/workflows/BuildAndTestOnEveryPush.yml @@ -30,6 +30,6 @@ jobs: - name: Push NuGet package to the testfeed if: runner.os == 'Windows' - run: dotnet nuget push Frends.Community.PowerShell\bin\Release\Frends.Community.PowerShell.*.nupkg --api-key ${{ secrets.CommunityFeedApiKey }} --source https://www.myget.org/F/frends-community-test/api/v2/package --symbol-source https://www.myget.org/F/frends-community-test/symbols/api/v2/package + run: dotnet nuget push Frends.Community.PowerShell\bin\Release\Frends.Community.PowerShell.*.nupkg --api-key ${{ secrets.CommunityFeedApiKey }} --source https://www.myget.org/F/frends-community-test/api/v2/package diff --git a/.github/workflows/PackAndPushAfterMerge.yml b/.github/workflows/PackAndPushAfterMerge.yml index 623f424..f53e72b 100644 --- a/.github/workflows/PackAndPushAfterMerge.yml +++ b/.github/workflows/PackAndPushAfterMerge.yml @@ -17,5 +17,4 @@ jobs: run: dotnet pack --configuration Release --include-source - name: Push NuGet package to the (prod) feed - run: dotnet nuget push Frends.Community.PowerShell\bin\Release\Frends.Community.PowerShell.*.nupkg --api-key ${{ secrets.CommunityFeedApiKey }} --source https://www.myget.org/F/frends-community/api/v2/package --symbol-source https://www.myget.org/F/frends-community/symbols/api/v2/package - + run: dotnet nuget push Frends.Community.PowerShell\bin\Release\Frends.Community.PowerShell.*.nupkg --api-key ${{ secrets.CommunityFeedApiKey }} --source https://www.myget.org/F/frends-community/api/v2/package \ No newline at end of file diff --git a/Frends.Community.PowerShell.Tests/PowerShellTests.cs b/Frends.Community.PowerShell.Tests/PowerShellTests.cs index b55f1cd..7120de7 100644 --- a/Frends.Community.PowerShell.Tests/PowerShellTests.cs +++ b/Frends.Community.PowerShell.Tests/PowerShellTests.cs @@ -1,8 +1,7 @@ -using System; -using System.Diagnostics; +using NUnit.Framework; +using System; using System.IO; using System.Linq; -using NUnit.Framework; namespace Frends.Community.PowerShell.Tests { @@ -25,10 +24,11 @@ public void RunCommand_ShouldRunCommandWithParameter() }, LogInformationStream = true }, - new RunOptions()); + new RunOptions(), + default); - Assert.That(result.Result, Is.Not.Null); - Assert.That(result.Result.Single(), Is.EqualTo(TimeSpan.FromHours(1))); + Assert.IsNotNull(result.Result); + Assert.AreEqual(TimeSpan.FromHours(1), result.Result.Single()); } [Test] @@ -44,10 +44,11 @@ public void RunScript_ShouldRunScriptWithParameter() ReadFromFile = false, Script = script, LogInformationStream = true - }, new RunOptions()); + }, new RunOptions(), + default); - Assert.That(result.Result.Count, Is.EqualTo(2)); - Assert.That(result.Result.Last(), Is.EqualTo("my test param: my test param")); + Assert.AreEqual(2, result.Result.Count); + Assert.AreEqual("my test param: my test param", result.Result.Last()); } [TestCase(true)] @@ -79,9 +80,10 @@ function Test-Switch { new RunOptions { Session = session - }); + }, + default); - Assert.That(result.Result.Single(), Is.EqualTo(switchParameterValue)); + Assert.AreEqual(switchParameterValue, result.Result.Single()); } @@ -103,15 +105,15 @@ public void RunScript_ShouldRunScriptFromFile() ReadFromFile = true, ScriptFilePath = scriptFilePath, LogInformationStream = true - }, new RunOptions()); + }, new RunOptions(), default); } finally { File.Delete(scriptFilePath); } - Assert.That(result.Result.Count, Is.EqualTo(2)); - Assert.That(result.Result.Last(), Is.EqualTo(TimeSpan.FromHours(2))); + Assert.AreEqual(2, result.Result.Count); + Assert.AreEqual(TimeSpan.FromHours(2), result.Result.Last()); } [Test] @@ -124,18 +126,18 @@ public void RunScript_ShouldRunScriptFromParameter() ReadFromFile = false, Script = script, LogInformationStream = true - }, new RunOptions()); + }, new RunOptions(), default); - Assert.That(result.Result.Last(), Is.EqualTo(TimeSpan.FromHours(2))); + Assert.AreEqual(TimeSpan.FromHours(2), result.Result.Last()); } [Test] public void RunCommandAndScript_ShouldUseSharedSession() { var session = PowerShell.CreateSession(); - - var result1 = PowerShell.RunScript(new RunScriptInput + + PowerShell.RunScript(new RunScriptInput { ReadFromFile = false, Script = "$timespan = $timespan + (new-timespan -hours 1)", @@ -144,7 +146,7 @@ public void RunCommandAndScript_ShouldUseSharedSession() new RunOptions { Session = session - }); + }, default); var result2 = PowerShell.RunScript(new RunScriptInput { @@ -155,9 +157,9 @@ public void RunCommandAndScript_ShouldUseSharedSession() new RunOptions { Session = session - }); + }, default); - Assert.That(result2.Result.Single(), Is.EqualTo(TimeSpan.FromHours(2))); + Assert.AreEqual(TimeSpan.FromHours(2), result2.Result.Single()); } [Test] @@ -182,9 +184,9 @@ public static class pstest { get-process -name doesnotexist -ErrorAction Stop "; - var resultError = Assert.Throws(() => PowerShell.RunScript(new RunScriptInput { ReadFromFile = false, Script = script, LogInformationStream = true }, null)); + var resultError = Assert.Throws(() => PowerShell.RunScript(new RunScriptInput { ReadFromFile = false, Script = script, LogInformationStream = true }, null, default)); - Assert.That(resultError.Message, Is.Not.Null); + Assert.IsNotNull(resultError.Message); } [Test] @@ -201,10 +203,10 @@ public void RunScript_ShouldOutputCustomPowershellObjects() ReadFromFile = false, Script = script, LogInformationStream = true - }, null); + }, null, default); - Assert.That(result.Result[0].Property1, Is.EqualTo("Value1")); - Assert.That(result.Result[0].Property2, Is.EqualTo("Value2")); + Assert.AreEqual("Value1", result.Result[0].Property1); + Assert.AreEqual("Value2", result.Result[0].Property2); } } } diff --git a/Frends.Community.PowerShell/Definitions.cs b/Frends.Community.PowerShell/Definitions.cs index c29ad6e..3a91fae 100644 --- a/Frends.Community.PowerShell/Definitions.cs +++ b/Frends.Community.PowerShell/Definitions.cs @@ -1,24 +1,24 @@ using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; -using Frends.Community.PowerShell; #pragma warning disable 1591 namespace Frends.Community.PowerShell { - public class RunCommandInput { + public class RunCommandInput + { /// /// The PowerShell command to execute /// public string Command { get; set; } - + /// /// Parameters for the command, provided switch parameters need to have a boolean value /// public PowerShellParameter[] Parameters { get; set; } - + /// /// Should the information stream be logged. If false, log will be an empty string. /// If set to true, a lot of string data may be logged. Use with caution. @@ -38,7 +38,7 @@ public class RunScriptInput /// /// Parameters for the script, provided switch parameters need to have a boolean value /// - public PowerShellParameter[] Parameters { get;set; } + public PowerShellParameter[] Parameters { get; set; } /// /// Should the script be read from a file or from the Script parameter diff --git a/Frends.Community.PowerShell/Frends.Community.PowerShell.csproj b/Frends.Community.PowerShell/Frends.Community.PowerShell.csproj index a8d0c49..7b4dce8 100644 --- a/Frends.Community.PowerShell/Frends.Community.PowerShell.csproj +++ b/Frends.Community.PowerShell/Frends.Community.PowerShell.csproj @@ -10,7 +10,7 @@ true Frends true - 1.2.0 + 1.2.1 diff --git a/Frends.Community.PowerShell/PowerShell.cs b/Frends.Community.PowerShell/PowerShell.cs index b03999d..f28614c 100644 --- a/Frends.Community.PowerShell/PowerShell.cs +++ b/Frends.Community.PowerShell/PowerShell.cs @@ -8,6 +8,7 @@ using System.Management.Automation.Runspaces; using System.Runtime.CompilerServices; using System.Text; +using System.Threading; #pragma warning disable 1591 @@ -78,7 +79,7 @@ internal static SessionWrapper CreateSession() /// Executes a PowerShell script from a file or the script parameter /// /// Object { Result: List<dynamic>, Errors: List<string>, Log: string} - public static PowerShellResult RunScript(RunScriptInput input, [Browsable(false)]RunOptions options) + public static PowerShellResult RunScript(RunScriptInput input, [Browsable(false)] RunOptions options, CancellationToken cancellationToken) { return DoAndHandleSession(options?.Session, session => { @@ -93,7 +94,7 @@ public static PowerShellResult RunScript(RunScriptInput input, [Browsable(false) { File.WriteAllText(tempScript, script, Encoding.UTF8); - return ExecuteCommand(tempScript, input.Parameters, input.LogInformationStream, session.PowerShell); + return ExecuteCommand(tempScript, input.Parameters, input.LogInformationStream, session.PowerShell, cancellationToken); } finally { @@ -122,22 +123,23 @@ private static PowerShellResult DoAndHandleSession(SessionWrapper sessionFromOut /// Executes a PowerShell command with parameters, leave parameter value empty for a switch /// /// Object { Result: List<dynamic>, Errors: List<string>, Log: string} - public static PowerShellResult RunCommand(RunCommandInput input, [Browsable(false)]RunOptions options) + public static PowerShellResult RunCommand(RunCommandInput input, [Browsable(false)] RunOptions options, CancellationToken cancellationToken) { return DoAndHandleSession(options?.Session, (session) => { - return ExecuteCommand(input.Command, input.Parameters, input.LogInformationStream, session.PowerShell); + return ExecuteCommand(input.Command, input.Parameters, input.LogInformationStream, session.PowerShell, cancellationToken); }); } private static PowerShellResult ExecuteCommand(string inputCommand, PowerShellParameter[] powerShellParameters, bool logInformationStream, - System.Management.Automation.PowerShell powershell) + System.Management.Automation.PowerShell powershell, CancellationToken cancellationToken) { var command = new Command(inputCommand, isScript: false, useLocalScope: false); foreach (var parameter in powerShellParameters ?? new PowerShellParameter[] { }) { + cancellationToken.ThrowIfCancellationRequested(); var parameterName = parameter.Name.Trim('-', ' '); // Remove dash from start // Switch parameters will have to specify value as true: @@ -146,7 +148,7 @@ private static PowerShellResult ExecuteCommand(string inputCommand, PowerShellPa powershell.Commands.AddCommand(command); - return ExecutePowershell(powershell, logInformationStream); + return ExecutePowershell(powershell, logInformationStream, cancellationToken); } private static IList GetErrorMessages(PSDataCollection errors) @@ -154,7 +156,7 @@ private static IList GetErrorMessages(PSDataCollection erro return errors.Select(err => $"{err.ScriptStackTrace}: {err.Exception.Message}").ToList(); } - private static PowerShellResult ExecutePowershell(System.Management.Automation.PowerShell powershell, bool logInformationStream) + private static PowerShellResult ExecutePowershell(System.Management.Automation.PowerShell powershell, bool logInformationStream, CancellationToken cancellationToken) { try { diff --git a/Frends.Community.PowerShell/PowerShellHost.cs b/Frends.Community.PowerShell/PowerShellHost.cs index fcc1a0e..12ebe3f 100644 --- a/Frends.Community.PowerShell/PowerShellHost.cs +++ b/Frends.Community.PowerShell/PowerShellHost.cs @@ -190,7 +190,7 @@ public override Version Version public override void SetShouldExit(int exitCode) { - + } /// diff --git a/README.md b/README.md index 4efbc4d..089c6a9 100644 --- a/README.md +++ b/README.md @@ -100,3 +100,4 @@ NOTE: Be sure to merge the latest from "upstream" before making a pull request! | 1.0.0 | First version | | 1.1.0 | Multi-framework and Github actions support | | 1.2.0 | Option to turn information stream logging on or off | +| 1.2.1 | Added support for a cancellation token. |