MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Management.Automation;6using System.Management.Automation.Runspaces;7using System.Reflection;8using Xunit;9 10namespace PSTests.Sequential11{12 // NOTE: do not call AddCommand("out-host") after invoking or MergeMyResults,13 // otherwise Invoke will not return any objects14 public class RunspaceTests15 {16 private static readonly int count = 1;17 private static readonly string script = string.Format($"get-command get-command");18 19 [Fact]20 public void TestRunspaceWithPipeline()21 {22 using (Runspace runspace = RunspaceFactory.CreateRunspace())23 {24 runspace.Open();25 26 using (var pipeline = runspace.CreatePipeline(script))27 {28 int objCount = 0;29 foreach (var result in pipeline.Invoke())30 {31 ++objCount;32 Assert.NotNull(result);33 }34 35 Assert.Equal(count, objCount);36 }37 38 runspace.Close();39 }40 }41 42 [Fact]43 public void TestRunspaceWithPowerShell()44 {45 using (var runspace = RunspaceFactory.CreateRunspace())46 {47 runspace.Open();48 49 using (PowerShell powerShell = PowerShell.Create())50 {51 powerShell.Runspace = runspace;52 53 powerShell.AddScript(script);54 55 int objCount = 0;56 foreach (var result in powerShell.Invoke())57 {58 ++objCount;59 Assert.NotNull(result);60 }61 62 Assert.Equal(count, objCount);63 }64 65 runspace.Close();66 }67 }68 69 [Fact]70 public void TestRunspaceWithPowerShellAndInitialSessionState()71 {72 // CreateDefault2 is intentional.73 InitialSessionState iss = InitialSessionState.CreateDefault();74 75 // NOTE: instantiate custom host myHost for the next line to capture stdout and stderr output76 // in addition to just the PSObjects77 using (Runspace runspace = RunspaceFactory.CreateRunspace(/*myHost,*/iss))78 {79 runspace.Open();80 using (PowerShell powerShell = PowerShell.Create())81 {82 powerShell.Runspace = runspace;83 powerShell.AddScript("Import-Module Microsoft.PowerShell.Utility -Force");84 powerShell.AddScript(script);85 86 int objCount = 0;87 88 var results = powerShell.Invoke();89 90 foreach (var result in results)91 {92 // this is how an object would be captured here and looked at,93 // each result is a PSObject with the data from the pipeline94 ++objCount;95 Assert.NotNull(result);96 }97 98 Assert.Equal(count, objCount);99 }100 101 runspace.Close();102 }103 }104 105 [SkippableFact]106 public void TestAppDomainProcessExitEvenHandlerNotLeaking()107 {108 // Skip this flaky test for now.109 Skip.IfNot(false);110 111 Skip.IfNot(Platform.IsWindows);112 113 EventHandler eventHandler;114 Delegate[] delegates;115 FieldInfo field = typeof(AppContext).GetField("ProcessExit", BindingFlags.NonPublic | BindingFlags.Static);116 117 // Open runspace and invoke script.118 using (var ps = PowerShell.Create())119 {120 ps.AddScript("1").Invoke();121 eventHandler = (EventHandler)field.GetValue(null);122 delegates = eventHandler.GetInvocationList();123 Assert.Contains(delegates, d => d.Method.Name == "CurrentDomain_ProcessExit");124 }125 126 // Handler registered by PowerShell should be unregistered.127 eventHandler = (EventHandler)field.GetValue(null);128 delegates = eventHandler.GetInvocationList();129 Assert.DoesNotContain(delegates, d => d.Method.Name == "CurrentDomain_ProcessExit");130 }131 }132}133 