internal static ScriptOutput ExecuteAndOutput(PowerShell script) { if (script == null) { throw new ArgumentNullException(nameof(script)); } var scriptOutput = new ScriptOutput(); var output = script.Invoke(); scriptOutput.Pipeline = output.Select(o => o.ToString()).ToList(); scriptOutput.Console = script.Streams.Information.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Info, s.TimeGenerated)) .Union(script.Streams.Verbose.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Verbose))) .Union(script.Streams.Progress.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Progress))) .Union(script.Streams.Debug.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Debug))) .Union(script.Streams.Warning.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Warning))) .Union(script.Streams.Error.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Error))) .ToList(); return(scriptOutput); }
internal async static Task <ScriptOutput> ExecuteAndOutputAsync(PowerShell script) { if (script == null) { throw new ArgumentNullException(nameof(script)); } var scriptOutput = new ScriptOutput(); //var output = await Task.Factory.FromAsync(script.BeginInvoke(), (ar) => script.EndInvoke(ar)); var output = await Task.Run(() => script.Invoke()); scriptOutput.Pipeline = output.Select(o => o?.ToString()).ToList(); scriptOutput.Console = script.Streams.Information.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Info, s.TimeGenerated)) .Union(script.Streams.Verbose.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Verbose))) .Union(script.Streams.Progress.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Progress))) .Union(script.Streams.Debug.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Debug))) .Union(script.Streams.Warning.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Warning))) .Union(script.Streams.Error.Select(s => new ScriptStreamInfo(s.ToString(), PowerShellStreamType.Error))) .ToList(); return(scriptOutput); }