From 9d3ca4542795be112e2d21cfee20d823e40366b6 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Tue, 18 Jan 2022 18:23:53 +0200 Subject: [PATCH] CmdService fix --- notes/generic.txt | 1 + .../Services/Windows/CmdService.cs | 19 +++++++++++++------ .../NetworkResurrector.Api.Application.csproj | 2 +- .../Repositories/NetworkRepository.cs | 4 +++- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/notes/generic.txt b/notes/generic.txt index d8b5d70..2e3155d 100644 --- a/notes/generic.txt +++ b/notes/generic.txt @@ -4,6 +4,7 @@ dotnet publish --configuration Release --runtime win7-x64 Create windows service: sc create NetworkResurrector.Server binPath= "" +sc create NetworkResurrector.Agent binPath= "" ####################################################################################################################################################### Resources: https://github.com/nikeee/wake-on-lan diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs index d1ab390..370461f 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs @@ -7,10 +7,10 @@ namespace NetworkResurrector.Agent.Application.Services.Windows { internal class CmdService : ICliService { - public (bool success, string message) Execute(string command) => Run("cmd.exe", command); - public (bool success, string message) Shutdown(string arguments) => Run("shutdown.exe", arguments); + public (bool success, string message) Execute(string command) => Run("cmd.exe", command, null); + public (bool success, string message) Shutdown(string arguments) => Run("shutdown.exe", null, arguments); - private (bool success, string message) Run(string fileName, string arguments) + private (bool success, string message) Run(string fileName, string command, string arguments) { var commandOutput = new StringBuilder(); var commandErrors = new StringBuilder(); @@ -23,6 +23,9 @@ namespace NetworkResurrector.Agent.Application.Services.Windows process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = fileName; + if (arguments != null) + process.StartInfo.Arguments = arguments; + commandOutput.Clear(); commandErrors.Clear(); @@ -39,9 +42,13 @@ namespace NetworkResurrector.Agent.Application.Services.Windows }); process.Start(); - process.StandardInput.WriteLine(arguments); - process.StandardInput.Flush(); - process.StandardInput.Close(); + + if (command != null) + { + process.StandardInput.WriteLine(command); + process.StandardInput.Flush(); + process.StandardInput.Close(); + } process.BeginOutputReadLine(); process.BeginErrorReadLine(); diff --git a/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj b/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj index 5d6d1b1..96739f9 100644 --- a/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj +++ b/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs b/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs index f211415..300ecf7 100644 --- a/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs +++ b/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs @@ -24,7 +24,9 @@ namespace NetworkResurrector.Api.Domain.Data.Repositories public async Task GetMachine(int machineId) { - var machine = await _dbContext.Machines.FirstOrDefaultAsync(z => z.MachineId == machineId); + var machine = await _dbContext.Machines + .Include(z => z.Agent) + .FirstOrDefaultAsync(z => z.MachineId == machineId); if (machine == null) throw new Exception($"Machine with id {machineId} not found");