2018-02-13 18:23:53 +01:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Licensed under the MIT License.
|
2018-12-18 17:11:21 +01:00
|
|
|
|
2015-09-03 21:43:17 +02:00
|
|
|
using System;
|
2019-04-14 04:14:53 +02:00
|
|
|
using System.Collections.Generic;
|
2015-09-03 21:43:17 +02:00
|
|
|
using System.Management.Automation;
|
|
|
|
using System.Management.Automation.Runspaces;
|
2018-12-18 17:11:21 +01:00
|
|
|
using Xunit;
|
2015-09-03 21:43:17 +02:00
|
|
|
|
2019-01-30 23:12:26 +01:00
|
|
|
namespace PSTests.Sequential
|
2015-09-03 21:43:17 +02:00
|
|
|
{
|
2015-12-01 19:45:20 +01:00
|
|
|
// NOTE: do not call AddCommand("out-host") after invoking or MergeMyResults,
|
|
|
|
// otherwise Invoke will not return any objects
|
2015-12-15 23:21:33 +01:00
|
|
|
public class RunspaceTests
|
2015-09-03 21:43:17 +02:00
|
|
|
{
|
2017-11-30 23:44:41 +01:00
|
|
|
private static int count = 1;
|
2018-12-18 17:11:21 +01:00
|
|
|
private static string script = string.Format($"get-command get-command");
|
2015-12-01 19:45:20 +01:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestRunspaceWithPipeline()
|
|
|
|
{
|
|
|
|
using (Runspace runspace = RunspaceFactory.CreateRunspace())
|
|
|
|
{
|
|
|
|
runspace.Open();
|
|
|
|
|
|
|
|
using (var pipeline = runspace.CreatePipeline(script))
|
|
|
|
{
|
|
|
|
int objCount = 0;
|
|
|
|
foreach (var result in pipeline.Invoke())
|
|
|
|
{
|
|
|
|
++objCount;
|
|
|
|
Assert.NotNull(result);
|
|
|
|
}
|
2018-12-18 17:11:21 +01:00
|
|
|
|
2015-12-01 19:45:20 +01:00
|
|
|
Assert.Equal(count, objCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
runspace.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 21:43:17 +02:00
|
|
|
[Fact]
|
2015-12-01 19:45:20 +01:00
|
|
|
public void TestRunspaceWithPowerShell()
|
|
|
|
{
|
|
|
|
using (var runspace = RunspaceFactory.CreateRunspace())
|
|
|
|
{
|
|
|
|
runspace.Open();
|
|
|
|
|
|
|
|
using (PowerShell powerShell = PowerShell.Create())
|
|
|
|
{
|
|
|
|
powerShell.Runspace = runspace;
|
|
|
|
|
|
|
|
powerShell.AddScript(script);
|
|
|
|
|
|
|
|
int objCount = 0;
|
|
|
|
foreach (var result in powerShell.Invoke())
|
|
|
|
{
|
|
|
|
++objCount;
|
2015-12-01 21:21:20 +01:00
|
|
|
Assert.NotNull(result);
|
|
|
|
}
|
2018-12-18 17:11:21 +01:00
|
|
|
|
2015-12-01 21:21:20 +01:00
|
|
|
Assert.Equal(count, objCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
runspace.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:44:41 +01:00
|
|
|
[Fact]
|
2015-12-01 19:45:20 +01:00
|
|
|
public void TestRunspaceWithPowerShellAndInitialSessionState()
|
2015-09-03 21:43:17 +02:00
|
|
|
{
|
2018-12-13 23:22:07 +01:00
|
|
|
// CreateDefault2 is intentional.
|
|
|
|
InitialSessionState iss = InitialSessionState.CreateDefault();
|
2015-09-03 21:43:17 +02:00
|
|
|
|
|
|
|
// NOTE: instantiate custom host myHost for the next line to capture stdout and stderr output
|
|
|
|
// in addition to just the PSObjects
|
2015-12-01 19:45:20 +01:00
|
|
|
using (Runspace runspace = RunspaceFactory.CreateRunspace(/*myHost,*/iss))
|
2015-09-03 21:43:17 +02:00
|
|
|
{
|
2015-12-01 19:45:20 +01:00
|
|
|
runspace.Open();
|
|
|
|
using (PowerShell powerShell = PowerShell.Create())
|
2015-09-03 21:43:17 +02:00
|
|
|
{
|
2015-12-01 19:45:20 +01:00
|
|
|
powerShell.Runspace = runspace;
|
2017-11-30 23:44:41 +01:00
|
|
|
powerShell.AddScript("Import-Module Microsoft.PowerShell.Utility -Force");
|
2015-12-01 19:45:20 +01:00
|
|
|
powerShell.AddScript(script);
|
2015-09-03 21:43:17 +02:00
|
|
|
|
|
|
|
int objCount = 0;
|
2017-11-30 23:44:41 +01:00
|
|
|
|
|
|
|
var results = powerShell.Invoke();
|
|
|
|
|
|
|
|
foreach (var result in results)
|
2015-09-03 21:43:17 +02:00
|
|
|
{
|
|
|
|
// this is how an object would be captured here and looked at,
|
|
|
|
// each result is a PSObject with the data from the pipeline
|
|
|
|
++objCount;
|
|
|
|
Assert.NotNull(result);
|
|
|
|
}
|
2018-12-13 23:22:07 +01:00
|
|
|
|
2015-12-01 19:45:20 +01:00
|
|
|
Assert.Equal(count, objCount);
|
2015-09-03 21:43:17 +02:00
|
|
|
}
|
2018-12-13 23:22:07 +01:00
|
|
|
|
|
|
|
runspace.Close();
|
2015-09-03 21:43:17 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-14 04:14:53 +02:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestRunspaceSetBreakpoints()
|
|
|
|
{
|
|
|
|
using (var runspace = RunspaceFactory.CreateRunspace())
|
|
|
|
{
|
|
|
|
var expectedBreakpoints = new Breakpoint[] {
|
|
|
|
new LineBreakpoint(@"./path/to/some/file.ps1", 1),
|
|
|
|
new CommandBreakpoint(@"./path/to/some/file.ps1", new WildcardPattern("Write-Host"), "Write-Host"),
|
|
|
|
};
|
|
|
|
|
|
|
|
runspace.Open();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
runspace.Debugger.SetBreakpoints(expectedBreakpoints);
|
|
|
|
List<Breakpoint> actualBreakpoints = runspace.Debugger.GetBreakpoints();
|
|
|
|
Assert.Equal(expectedBreakpoints.Length, actualBreakpoints.Count);
|
|
|
|
Assert.Equal(expectedBreakpoints, actualBreakpoints);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
runspace.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 21:43:17 +02:00
|
|
|
}
|
|
|
|
}
|