public IEnumerable <Execution> ExecuteCommand(long commandId, IEnumerable <long> computers) { var command = _commandRepository.GetById(1).GetAwaiter().GetResult(); var execucoes = computers.ToList() .Aggregate(new List <Execution>(), (acc, computerId) => { var computer = _computerRepository.GetById(computerId).GetAwaiter().GetResult(); try { using (var client = new SshClient(computer.Ip, "linux3", "linux3")) { client.Connect(); var cmd = client.RunCommand(command.Value); client.Disconnect(); acc.Add(new Execution { Command = command, CommandId = command.Id, Computer = computer, ComputerId = computerId, Output = cmd.Result, Status = ExecutionStatusType.Processed, DateExecution = DateTime.Now }); return(acc); } } catch (Exception ex) { acc.Add(new Execution { Command = command, CommandId = command.Id, Computer = computer, ComputerId = computerId, Output = ex.Message, Status = ExecutionStatusType.Error, DateExecution = DateTime.Now }); return(acc); } }); _executionRepository.Create(execucoes); return(execucoes); }