using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Runspaces; namespace mypwsh { internal class Program { static void Main(string[] args) { using (var ps = PowerShell.Create()) { // 避免使用 AddScript,改用 AddCommand ps.AddCommand("Get-Service"); var results = ps.Invoke(); foreach (var result in results) { var service = result.BaseObject; } } Console.WriteLine("Hello, World!"); } } }