Я пытаюсь создать «настраиваемую» оболочку PowerShell для некоторых наших разработок и пользовательских модулей Azure.
Я хочу получить список модулей, выполнив следующее, но outputCollection пуст и событие DataAdded не сработает.
Что, черт возьми, мне здесь не хватает?
PS: это PowerShell 5, и я использовал пакет Nuget Microsoft.PowerShell.5.ReferenceAssemblies
using System;
using System.Management.Automation;
using System.Threading;
namespace TestShell
{
class Program
{
static void Main(string[] args)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("Get-Module");
PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
outputCollection.DataAdded += outputCollection_DataAdded;
PowerShellInstance.Streams.Error.DataAdded += Error_DataAdded;
IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject, PSObject>(null, outputCollection);
while (result.IsCompleted == false)
{
Console.WriteLine("Waiting for pipeline to finish...");
Thread.Sleep(1000);
}
Console.WriteLine("Execution has stopped. The pipeline state: " + PowerShellInstance.InvocationStateInfo.State);
foreach (PSObject outputItem in outputCollection)
{
Console.WriteLine(outputItem.BaseObject.ToString());
}
}
}
static void outputCollection_DataAdded(object sender, DataAddedEventArgs e)
{
Console.WriteLine("Object added to output.");
}
static void Error_DataAdded(object sender, DataAddedEventArgs e)
{
Console.WriteLine("An error was written to the Error stream!");
}
}
}