Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/BuildAndTestOnEveryPush.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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


3 changes: 1 addition & 2 deletions .github/workflows/PackAndPushAfterMerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 28 additions & 26 deletions Frends.Community.PowerShell.Tests/PowerShellTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -25,10 +24,11 @@ public void RunCommand_ShouldRunCommandWithParameter()
},
Comment thread
ttossavainen marked this conversation as resolved.
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]
Expand All @@ -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)]
Expand Down Expand Up @@ -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());
}


Expand All @@ -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]
Expand All @@ -124,18 +126,18 @@ public void RunScript_ShouldRunScriptFromParameter()
ReadFromFile = false,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert.That(result.Result.Count, Is.EqualTo(2));

Please use AreEqual instead of That.

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)",
Expand All @@ -144,7 +146,7 @@ public void RunCommandAndScript_ShouldUseSharedSession()
new RunOptions
Comment thread
ttossavainen marked this conversation as resolved.
{
Session = session
});
}, default);

var result2 = PowerShell.RunScript(new RunScriptInput
{
Expand All @@ -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]
Expand All @@ -182,9 +184,9 @@ public static class pstest {
get-process -name doesnotexist -ErrorAction Stop
";

var resultError = Assert.Throws<Exception>(() => PowerShell.RunScript(new RunScriptInput { ReadFromFile = false, Script = script, LogInformationStream = true }, null));
var resultError = Assert.Throws<Exception>(() => PowerShell.RunScript(new RunScriptInput { ReadFromFile = false, Script = script, LogInformationStream = true }, null, default));

Assert.That(resultError.Message, Is.Not.Null);
Assert.IsNotNull(resultError.Message);
}

[Test]
Expand All @@ -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);
}
}
}
10 changes: 5 additions & 5 deletions Frends.Community.PowerShell/Definitions.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// The PowerShell command to execute
/// </summary>
public string Command { get; set; }

/// <summary>
/// Parameters for the command, provided switch parameters need to have a boolean value
/// </summary>
public PowerShellParameter[] Parameters { get; set; }

/// <summary>
/// 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.
Expand All @@ -38,7 +38,7 @@ public class RunScriptInput
/// <summary>
/// Parameters for the script, provided switch parameters need to have a boolean value
/// </summary>
public PowerShellParameter[] Parameters { get;set; }
public PowerShellParameter[] Parameters { get; set; }

/// <summary>
/// Should the script be read from a file or from the Script parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<IncludeSource>true</IncludeSource>
<PackageTags>Frends</PackageTags>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 9 additions & 7 deletions Frends.Community.PowerShell/PowerShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Management.Automation.Runspaces;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

#pragma warning disable 1591

Expand Down Expand Up @@ -78,7 +79,7 @@ internal static SessionWrapper CreateSession()
/// Executes a PowerShell script from a file or the script parameter
/// </summary>
/// <returns>Object { Result: List&lt;dynamic&gt;, Errors: List&lt;string&gt;, Log: string}</returns>
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 =>
{
Expand All @@ -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
{
Expand Down Expand Up @@ -122,22 +123,23 @@ private static PowerShellResult DoAndHandleSession(SessionWrapper sessionFromOut
/// Executes a PowerShell command with parameters, leave parameter value empty for a switch
/// </summary>
/// <returns>Object { Result: List&lt;dynamic&gt;, Errors: List&lt;string&gt;, Log: string}</returns>
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:
Expand All @@ -146,15 +148,15 @@ private static PowerShellResult ExecuteCommand(string inputCommand, PowerShellPa

powershell.Commands.AddCommand(command);

return ExecutePowershell(powershell, logInformationStream);
return ExecutePowershell(powershell, logInformationStream, cancellationToken);
}

private static IList<string> GetErrorMessages(PSDataCollection<ErrorRecord> errors)
{
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
{
Expand Down
2 changes: 1 addition & 1 deletion Frends.Community.PowerShell/PowerShellHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public override Version Version

public override void SetShouldExit(int exitCode)
{

}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |