public static Collection<PSObject> Exec(Runspace runspace, Command command) { PowerShell shell = PowerShell.Create(); shell.Runspace = runspace; Command import = new Command("Import-Module"); import.Parameters.Add("Name", "CoApp"); shell.Commands.AddCommand(import); shell.Invoke(); shell.Commands.Clear(); shell.Commands.AddScript(command.ToString()); Collection<PSObject> ret = shell.Invoke(); try { shell.Dispose(); runspace.Close(); } catch (Exception) { } return ret; }