public static Task <PSDataCollection <PSObject> > InvokeAsync(this Runspace runspace, string script, IDictionary scriptParameters = null, Action <PSDataStreams> psDataStreamAction = null) { if (script == null) { throw new ArgumentOutOfRangeException("script"); } if (runspace == null) { throw new ArgumentOutOfRangeException("runspace"); } using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create()) { ps.Runspace = runspace; ps.AddScript(script); if (scriptParameters != null && scriptParameters.Count > 0) { ps.AddParameters(scriptParameters); } psDataStreamAction?.Invoke(ps.Streams); return(ps.InvokeAsync()); } }