private static async Task <ScriptOutput> ExecuteAsync(string scriptBlock, IDictionary <string, object> parameters = null, string workingDirectory = null) { if (string.IsNullOrWhiteSpace(scriptBlock)) { throw new ArgumentNullException(nameof(scriptBlock)); } using (var shell = PowerShell.Create()) { if (!string.IsNullOrEmpty(workingDirectory)) { shell.AddScript($"{Set_Location} \"{workingDirectory}\""); } var script = shell.AddScript(scriptBlock); if (parameters?.Count > 0) { script.AddParameters(parameters.ToDictionary(k => k.Key, v => v.Value)); } var output = await ScriptOutput.ExecuteAndOutputAsync(script); if (shell.Streams.Error.Count > 0) { throw new AggregateException(shell.Streams.Error.Select(e => e.Exception)); } return(output); } }