2015-09-03 21:43:17 +02:00
|
|
|
using Xunit;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Management.Automation;
|
|
|
|
|
using System.Management.Automation.Runspaces;
|
|
|
|
|
|
|
|
|
|
namespace PSTests
|
|
|
|
|
{
|
2015-12-01 10:45:20 -08:00
|
|
|
// NOTE: do not call AddCommand("out-host") after invoking or MergeMyResults,
|
|
|
|
|
// otherwise Invoke will not return any objects
|
|
|
|
|
|
|
|
|
|
public class RunspaceTests: IDisposable
|
2015-09-03 21:43:17 +02:00
|
|
|
{
|
2015-12-01 10:45:20 -08:00
|
|
|
private static int count = 3;
|
|
|
|
|
private static string script = String.Format($"get-process | select-object -first {count}");
|
|
|
|
|
|
|
|
|
|
public RunspaceTests()
|
|
|
|
|
{
|
|
|
|
|
TypeCatalog.GenerateTypeCatalog();
|
|
|
|
|
Assert.NotNull(System.Management.Automation.ClrHost.TypeCatalog);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-03 21:43:17 +02:00
|
|
|
[Fact]
|
2015-12-01 10:45:20 -08:00
|
|
|
public void TestRunspaceWithPipeline()
|
|
|
|
|
{
|
|
|
|
|
Assert.NotNull(System.Management.Automation.ClrHost.TypeCatalog);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
Assert.Equal(count, objCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runspace.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
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 12:21:20 -08:00
|
|
|
Assert.NotNull(result);
|
|
|
|
|
}
|
|
|
|
|
Assert.Equal(count, objCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runspace.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void TestRunspaceWithPowerShellAndHost()
|
|
|
|
|
{
|
|
|
|
|
MyHost myHost = new MyHost(this);
|
|
|
|
|
using (var runspace = RunspaceFactory.CreateRunspace(myHost))
|
|
|
|
|
{
|
|
|
|
|
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 10:45:20 -08:00
|
|
|
Assert.NotNull(result);
|
|
|
|
|
}
|
|
|
|
|
Assert.Equal(count, objCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runspace.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-01 15:17:21 -08:00
|
|
|
[Fact]
|
|
|
|
|
public void TestRunspaceWithFunction()
|
|
|
|
|
{
|
|
|
|
|
MyHost myHost = new MyHost(this);
|
|
|
|
|
using (var runspace = RunspaceFactory.CreateRunspace(myHost))
|
|
|
|
|
{
|
|
|
|
|
runspace.Open();
|
|
|
|
|
|
|
|
|
|
using (PowerShell powerShell = PowerShell.Create())
|
|
|
|
|
{
|
|
|
|
|
powerShell.Runspace = runspace;
|
|
|
|
|
|
|
|
|
|
powerShell.AddScript("{1}.Invoke()");
|
|
|
|
|
powerShell.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runspace.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-01 10:45:20 -08:00
|
|
|
[Fact]
|
|
|
|
|
public void TestRunspaceWithPowerShellAndInitialSessionState()
|
2015-09-03 21:43:17 +02:00
|
|
|
{
|
|
|
|
|
InitialSessionState iss = InitialSessionState.CreateDefault2();
|
|
|
|
|
|
|
|
|
|
// NOTE: instantiate custom host myHost for the next line to capture stdout and stderr output
|
|
|
|
|
// in addition to just the PSObjects
|
2015-12-01 10:45:20 -08:00
|
|
|
using (Runspace runspace = RunspaceFactory.CreateRunspace(/*myHost,*/iss))
|
2015-09-03 21:43:17 +02:00
|
|
|
{
|
2015-12-01 10:45:20 -08:00
|
|
|
runspace.Open();
|
|
|
|
|
using (PowerShell powerShell = PowerShell.Create())
|
2015-09-03 21:43:17 +02:00
|
|
|
{
|
2015-12-01 10:45:20 -08:00
|
|
|
powerShell.Runspace = runspace;
|
2015-09-03 21:43:17 +02:00
|
|
|
|
2015-12-01 10:45:20 -08:00
|
|
|
powerShell.AddScript(script);
|
2015-09-03 21:43:17 +02:00
|
|
|
|
|
|
|
|
int objCount = 0;
|
2015-12-01 10:45:20 -08:00
|
|
|
foreach (var result in powerShell.Invoke())
|
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);
|
|
|
|
|
}
|
2015-12-01 10:45:20 -08:00
|
|
|
Assert.Equal(count, objCount);
|
|
|
|
|
powerShell.Dispose();
|
2015-09-03 21:43:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|