2018-02-14 01:23:53 +08:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Licensed under the MIT License.
|
2018-12-19 00:11:21 +08:00
|
|
|
|
2015-09-04 03:43:17 +08:00
|
|
|
using System;
|
|
|
|
using System.Management.Automation;
|
|
|
|
using System.Management.Automation.Runspaces;
|
2018-12-19 00:11:21 +08:00
|
|
|
using Xunit;
|
2015-09-04 03:43:17 +08:00
|
|
|
|
2019-01-31 06:12:26 +08:00
|
|
|
namespace PSTests.Sequential
|
2015-09-04 03:43:17 +08:00
|
|
|
{
|
2015-12-02 02:45:20 +08:00
|
|
|
// NOTE: do not call AddCommand("out-host") after invoking or MergeMyResults,
|
|
|
|
// otherwise Invoke will not return any objects
|
2015-12-16 06:21:33 +08:00
|
|
|
public class RunspaceTests
|
2015-09-04 03:43:17 +08:00
|
|
|
{
|
2017-12-01 06:44:41 +08:00
|
|
|
private static int count = 1;
|
2018-12-19 00:11:21 +08:00
|
|
|
private static string script = string.Format($"get-command get-command");
|
2015-12-02 02:45:20 +08: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-19 00:11:21 +08:00
|
|
|
|
2015-12-02 02:45:20 +08:00
|
|
|
Assert.Equal(count, objCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
runspace.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 03:43:17 +08:00
|
|
|
[Fact]
|
2015-12-02 02:45:20 +08: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-02 04:21:20 +08:00
|
|
|
Assert.NotNull(result);
|
|
|
|
}
|
2018-12-19 00:11:21 +08:00
|
|
|
|
2015-12-02 04:21:20 +08:00
|
|
|
Assert.Equal(count, objCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
runspace.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 06:44:41 +08:00
|
|
|
[Fact]
|
2015-12-02 02:45:20 +08:00
|
|
|
public void TestRunspaceWithPowerShellAndInitialSessionState()
|
2015-09-04 03:43:17 +08:00
|
|
|
{
|
2018-12-14 06:22:07 +08:00
|
|
|
// CreateDefault2 is intentional.
|
|
|
|
InitialSessionState iss = InitialSessionState.CreateDefault();
|
2015-09-04 03:43:17 +08:00
|
|
|
|
|
|
|
// NOTE: instantiate custom host myHost for the next line to capture stdout and stderr output
|
|
|
|
// in addition to just the PSObjects
|
2015-12-02 02:45:20 +08:00
|
|
|
using (Runspace runspace = RunspaceFactory.CreateRunspace(/*myHost,*/iss))
|
2015-09-04 03:43:17 +08:00
|
|
|
{
|
2015-12-02 02:45:20 +08:00
|
|
|
runspace.Open();
|
|
|
|
using (PowerShell powerShell = PowerShell.Create())
|
2015-09-04 03:43:17 +08:00
|
|
|
{
|
2015-12-02 02:45:20 +08:00
|
|
|
powerShell.Runspace = runspace;
|
2017-12-01 06:44:41 +08:00
|
|
|
powerShell.AddScript("Import-Module Microsoft.PowerShell.Utility -Force");
|
2015-12-02 02:45:20 +08:00
|
|
|
powerShell.AddScript(script);
|
2015-09-04 03:43:17 +08:00
|
|
|
|
|
|
|
int objCount = 0;
|
2017-12-01 06:44:41 +08:00
|
|
|
|
|
|
|
var results = powerShell.Invoke();
|
|
|
|
|
|
|
|
foreach (var result in results)
|
2015-09-04 03:43:17 +08: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-14 06:22:07 +08:00
|
|
|
|
2015-12-02 02:45:20 +08:00
|
|
|
Assert.Equal(count, objCount);
|
2015-09-04 03:43:17 +08:00
|
|
|
}
|
2018-12-14 06:22:07 +08:00
|
|
|
|
|
|
|
runspace.Close();
|
2015-09-04 03:43:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|