public void UpdateCommandResult(Guid requestId, byte[] data, ServerCommandResult result) { if (_serverCommands.TryGetValue(requestId, out var tcs)) { tcs.SetResult(new ServerCommandRequestResult(data, (ServerCommandResult)(int)result)); } }
public void NotifyCommandResult(Guid requestId, byte[] data, ServerCommandResult result) { }
public void NotifyCommandResult(Guid requestId, byte[] data, ServerCommandResult result) { throw new NotImplementedException(); }
public ServerCommandResult ExecuteCommand(ServerCommand serverCommand) { if (!IsConnected) { throw new ServerConnectionException("Server connection is not established"); } if (serverCommand == null) { throw new ArgumentNullException(nameof(serverCommand)); } if (string.IsNullOrEmpty(serverCommand.Text)) { throw new CommandNotSpecifiedException($"Command was not defined"); } using (var command = Client.CreateCommand(serverCommand.Text)) { if (serverCommand.Timeout.HasValue) { command.CommandTimeout = serverCommand.Timeout.Value; } Stopwatch stopwatch = Stopwatch.StartNew(); var result = new ServerCommandResult(); try { command.Execute(); } catch (SshOperationTimeoutException exception) { if (serverCommand.ThrowError) { throw new CommandTimeoutException(exception.Message); } else { result.Error = exception.Message; } } catch (SshConnectionException exception) { if (serverCommand.ThrowError) { throw new ServerConnectionException(exception.Message); } else { result.Error = exception.Message; } } finally { if (stopwatch.IsRunning) { stopwatch.Stop(); } } result.Result = command.Result; result.ElapsedTime = stopwatch.Elapsed; result.Error = command.Error; result.ExitStatus = command.ExitStatus; if (serverCommand.ThrowError && !string.IsNullOrEmpty(command.Error)) { if (result.Error.Contains("command not found")) { throw new CommandNotFoundException($"Command {command.CommandText} is not found"); } throw new CommandResultException($"Command {command.CommandText} returned the following error: {command.Error}"); } return(result); } }
public void NotifyCommandResult(Guid requestId, byte[] data, ServerCommandResult result) { _listener.UpdateCommandResult(requestId, data, result); }
/// <summary> /// /// </summary> /// <param name="data"></param> /// <param name="result"></param> public ServerCommandRequestResult(byte[] data, ServerCommandResult result) { Data = data; Result = result; }
public ServerCommandResult ExecuteCommand(ServerCommand serverCommand) { if (!IsConnected) { throw new ServerConnectionException("Server connection is not established"); } if (serverCommand == null) { throw new ArgumentNullException(nameof(serverCommand)); } if (string.IsNullOrEmpty(serverCommand.Text)) { throw new CommandNotSpecifiedException($"Command was not defined"); } var result = new ServerCommandResult(); ObjectQuery query; try { try { query = new ObjectQuery(serverCommand.Text); } catch (Exception exception) { if (serverCommand.ThrowError) { throw new CommandNotFoundException($"Command {serverCommand.Text} is not found", exception); } else { result.ExitStatus = -1; result.Error = exception.Message; return(result); } } var commandOptions = new EnumerationOptions(); if (serverCommand.Timeout.HasValue) { commandOptions.Timeout = serverCommand.Timeout.Value; } var searcher = new ManagementObjectSearcher(ManagementScope, query, commandOptions); Stopwatch stopWatch = Stopwatch.StartNew(); using (var managementObjectCollection = searcher.Get()) { if (stopWatch.IsRunning) { stopWatch.Stop(); } var list = new List <Dictionary <string, object> >(); foreach (var managementObject in managementObjectCollection) { var dictionary = new Dictionary <string, object>(); foreach (var property in managementObject.Properties) { if (dictionary.TryGetValue(property.Name, out object value)) { dictionary.Add(property.Name, value); } } list.Add(dictionary); } result.Result = JsonConvert.SerializeObject(list); result.ElapsedTime = stopWatch.Elapsed; return(result); } } catch (TimeoutException exception) { throw new CommandTimeoutException(exception.Message); } catch (Exception exception) { if (serverCommand.ThrowError) { throw new CommandResultException( $"Command {serverCommand.Text} returned the following error: {exception.Message}"); } else { result.ExitStatus = -1; result.Error = exception.Message; return(result); } } }
public ServerCommandResult ExecuteCommand(ServerCommand serverCommand) { if (!IsConnected) { throw new ServerConnectionException("Server connection is not established"); } if (serverCommand == null) { throw new ArgumentNullException(nameof(serverCommand)); } if (string.IsNullOrEmpty(serverCommand.Text)) { throw new CommandNotSpecifiedException($"Command was not defined"); } serverCommand.Timeout = serverCommand.Timeout ?? TimeSpan.FromMinutes(1); var result = new ServerCommandResult() { Result = "", Error = "" }; using (var powershell = PowerShell.Create()) { powershell.Runspace = RunspaceInstance; powershell.AddScript(serverCommand.Text); PSDataCollection <PSObject> output = new PSDataCollection <PSObject>(); #region Subscribe to streams powershell.Streams.Debug.DataAdded += (sender, e) => result.Result += Debug_DataAdded(sender, e); powershell.Streams.Verbose.DataAdded += (sender, e) => result.Result += Verbose_DataAdded(sender, e); powershell.Streams.Progress.DataAdded += (sender, e) => result.Result += Progress_DataAdded(sender, e); powershell.Streams.Warning.DataAdded += (sender, e) => result.Result += Warning_DataAdded(sender, e); powershell.Streams.Error.DataAdded += (sender, e) => result.Error += Error_DataAdded(sender, e); #endregion IAsyncResult powershellResult = powershell.BeginInvoke(); Stopwatch stopWatch = Stopwatch.StartNew(); while (!powershellResult.IsCompleted) { Thread.Sleep(500); if (stopWatch.Elapsed > serverCommand.Timeout) { if (stopWatch.IsRunning) { stopWatch.Stop(); } if (powershell.InvocationStateInfo.State == PSInvocationState.Running) { powershell.Stop(); } powershell.Dispose(); Dispose(); throw new CommandTimeoutException($"Command exceeded timeout {serverCommand.Timeout}"); } } if (stopWatch.IsRunning) { stopWatch.Stop(); } if (powershell.InvocationStateInfo.State == PSInvocationState.Running) { powershell.EndInvoke(powershellResult); } result.ElapsedTime = stopWatch.Elapsed; if (powershell.InvocationStateInfo.State == PSInvocationState.Failed) { result.Error = powershell.InvocationStateInfo.Reason.GetType().Name + ": " + powershell.InvocationStateInfo.Reason.Message; result.ExitStatus = -1; } if (!string.IsNullOrEmpty(result.Error)) { if (serverCommand.ThrowError) { throw new CommandResultException( $"Command {serverCommand.Text} returned the following error(s): {result.Error}"); } result.ExitStatus = -1; } } return(result); }