forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_Runspace.cs
More file actions
99 lines (84 loc) · 3.17 KB
/
test_Runspace.cs
File metadata and controls
99 lines (84 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Xunit;
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace PSTests.Parallel
{
// NOTE: do not call AddCommand("out-host") after invoking or MergeMyResults,
// otherwise Invoke will not return any objects
public class RunspaceTests
{
private static int count = 1;
private static string script = String.Format($"get-command get-command");
[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);
}
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;
Assert.NotNull(result);
}
Assert.Equal(count, objCount);
}
runspace.Close();
}
}
[Fact]
public void TestRunspaceWithPowerShellAndInitialSessionState()
{
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
using (Runspace runspace = RunspaceFactory.CreateRunspace(/*myHost,*/iss))
{
runspace.Open();
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.Runspace = runspace;
powerShell.AddScript("Import-Module Microsoft.PowerShell.Utility -Force");
powerShell.AddScript(script);
int objCount = 0;
var results = powerShell.Invoke();
foreach (var result in results)
{
// 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);
}
Assert.Equal(count, objCount);
powerShell.Dispose();
}
}
}
}
}